summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarsh Shandilya <msfjarvis@gmail.com>2020-11-15 14:04:14 +0530
committerGitHub <noreply@github.com>2020-11-15 09:34:14 +0100
commitd3c1a73050cad74460a7fedddb114d196e5703aa (patch)
treeecd9ee3270a2c38e8d385e20d6a214ecde7466ef
parent02c853c3b1124d8d37c357f7040abd579c51bf8a (diff)
build: refactor for configuration cache compatibility (#1208)
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--app/build.gradle.kts44
-rw-r--r--buildSrc/src/main/java/BaseProjectConfig.kt42
-rw-r--r--buildSrc/src/main/java/PasswordStorePlugin.kt7
-rw-r--r--buildSrc/src/main/java/SigningConfig.kt34
4 files changed, 82 insertions, 45 deletions
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 87e436a0..a668a080 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -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") {
diff --git a/buildSrc/src/main/java/BaseProjectConfig.kt b/buildSrc/src/main/java/BaseProjectConfig.kt
index e1d53dd8..2a7b81d9 100644
--- a/buildSrc/src/main/java/BaseProjectConfig.kt
+++ b/buildSrc/src/main/java/BaseProjectConfig.kt
@@ -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
@@ -56,6 +57,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).
*/
@Suppress("UnstableApiUsage")
diff --git a/buildSrc/src/main/java/PasswordStorePlugin.kt b/buildSrc/src/main/java/PasswordStorePlugin.kt
index 37a23b02..b8271f65 100644
--- a/buildSrc/src/main/java/PasswordStorePlugin.kt
+++ b/buildSrc/src/main/java/PasswordStorePlugin.kt
@@ -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()
}
}
diff --git a/buildSrc/src/main/java/SigningConfig.kt b/buildSrc/src/main/java/SigningConfig.kt
new file mode 100644
index 00000000..1f494d5f
--- /dev/null
+++ b/buildSrc/src/main/java/SigningConfig.kt
@@ -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) }
+ }
+}