build: refactor for configuration cache compatibility (#1208)

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-11-15 14:04:14 +05:30 committed by GitHub
parent 02c853c3b1
commit d3c1a73050
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 45 deletions

View file

@ -3,7 +3,6 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.android.build.gradle.internal.api.BaseVariantOutputImpl
import java.util.Properties
plugins {
id("com.android.application")
@ -11,12 +10,6 @@ plugins {
`aps-plugin`
}
val keystorePropertiesFile = rootProject.file("keystore.properties")
fun isSnapshot(): Boolean {
return System.getenv("GITHUB_WORKFLOW") != null && System.getenv("SNAPSHOT") != null
}
android {
if (isSnapshot()) {
applicationVariants.all {
@ -26,13 +19,6 @@ android {
}
}
adbOptions.installOptions("--user 0")
buildFeatures {
viewBinding = true
buildConfig = true
}
defaultConfig {
applicationId = "dev.msfjarvis.aps"
versionCode = 2_00_00
@ -46,36 +32,6 @@ android {
disable("MissingTranslation", "PluralsCandidate", "ImpliedQuantity")
}
buildTypes {
named("release") {
isMinifyEnabled = true
setProguardFiles(listOf("proguard-android-optimize.txt", "proguard-rules.pro"))
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", if (isSnapshot()) "true" else "false")
}
named("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
isMinifyEnabled = false
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "true")
}
}
if (keystorePropertiesFile.exists()) {
val keystoreProperties = Properties()
keystoreProperties.load(keystorePropertiesFile.inputStream())
signingConfigs {
register("release") {
keyAlias = keystoreProperties["keyAlias"] as String
keyPassword = keystoreProperties["keyPassword"] as String
storeFile = rootProject.file(keystoreProperties["storeFile"] as String)
storePassword = keystoreProperties["storePassword"] as String
}
}
listOf("release", "debug").map {
buildTypes.getByName(it).signingConfig = signingConfigs.getByName(it)
}
}
flavorDimensions("free")
productFlavors {
create("free") {

View file

@ -9,6 +9,7 @@ import org.gradle.api.Project
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestLogEvent
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.gradle.api.tasks.wrapper.Wrapper
import org.gradle.kotlin.dsl.repositories
import org.gradle.kotlin.dsl.withType
@ -55,6 +56,47 @@ internal fun Project.configureForAllProjects() {
}
}
/**
* Checks if we're building a snapshot
*/
@Suppress("UnstableApiUsage")
fun Project.isSnapshot(): Boolean {
with(project.providers) {
val workflow = environmentVariable("GITHUB_WORKFLOW").forUseAtConfigurationTime()
val snapshot = environmentVariable("SNAPSHOT").forUseAtConfigurationTime()
return workflow.isPresent && snapshot.isPresent
}
}
/**
* Apply configurations for app module
*/
@Suppress("UnstableApiUsage")
internal fun BaseAppModuleExtension.configureAndroidApplicationOptions(project: Project) {
val minifySwitch = project.providers.environmentVariable("DISABLE_MINIFY").forUseAtConfigurationTime()
adbOptions.installOptions("--user 0")
buildFeatures {
viewBinding = true
buildConfig = true
}
buildTypes {
named("release") {
isMinifyEnabled = !minifySwitch.isPresent
setProguardFiles(listOf("proguard-android-optimize.txt", "proguard-rules.pro"))
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "${project.isSnapshot()}")
}
named("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
isMinifyEnabled = false
buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "true")
}
}
}
/**
* Apply baseline configurations for all Android projects (Application and Library).
*/

View file

@ -4,6 +4,7 @@
*/
import com.android.build.gradle.TestedExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.android.build.gradle.internal.plugins.AppPlugin
import com.android.build.gradle.internal.plugins.LibraryPlugin
import org.gradle.api.Plugin
@ -32,8 +33,12 @@ class PasswordStorePlugin : Plugin<Project> {
options.isDeprecation = true
}
}
is LibraryPlugin,
is LibraryPlugin -> {
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
}
is AppPlugin -> {
project.extensions.getByType<BaseAppModuleExtension>().configureAndroidApplicationOptions(project)
project.extensions.getByType<BaseAppModuleExtension>().configureBuildSigning(project)
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import java.util.Properties
import org.gradle.api.Project
private const val KEYSTORE_CONFIG_PATH = "keystore.properties"
/**
* Configure signing for all build types.
*/
@Suppress("UnstableApiUsage")
internal fun BaseAppModuleExtension.configureBuildSigning(project: Project) {
with(project) {
val keystoreConfigFile = rootProject.layout.projectDirectory.file(KEYSTORE_CONFIG_PATH)
if (!keystoreConfigFile.asFile.exists()) return
val contents = providers.fileContents(keystoreConfigFile).asText.forUseAtConfigurationTime()
val keystoreProperties = Properties()
keystoreProperties.load(contents.get().byteInputStream())
signingConfigs {
register("release") {
keyAlias = keystoreProperties["keyAlias"] as String
keyPassword = keystoreProperties["keyPassword"] as String
storeFile = rootProject.file(keystoreProperties["storeFile"] as String)
storePassword = keystoreProperties["storePassword"] as String
}
}
val signingConfig = signingConfigs.getByName("release")
buildTypes.all { setSigningConfig(signingConfig) }
}
}