aboutsummaryrefslogtreecommitdiff
path: root/build-logic/android-plugins/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-11-29 02:23:06 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2021-12-03 12:59:57 +0530
commit4d327646a4f508920b653fac112914473ec25954 (patch)
tree72b416f75d679ea5ab869a7dd199808c2eb00635 /build-logic/android-plugins/src/main
parent4fb066dff5593a802ac53b681748da549d774f36 (diff)
build-logic: add android plugins
Diffstat (limited to 'build-logic/android-plugins/src/main')
-rw-r--r--build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts2
-rw-r--r--build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts12
-rw-r--r--build-logic/android-plugins/src/main/kotlin/signing/AppSigning.kt37
-rw-r--r--build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt108
4 files changed, 158 insertions, 1 deletions
diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts
index 9e38476d..ce0058a0 100644
--- a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts
+++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts
@@ -8,6 +8,7 @@ import flavors.FlavorDimensions
import flavors.ProductFlavors
import flavors.configureSlimTests
import org.gradle.kotlin.dsl.configure
+import signing.configureBuildSigning
plugins {
id("com.android.application")
@@ -60,4 +61,5 @@ extensions.configure<BaseAppModuleExtension> {
}
project.configureSlimTests()
+ project.configureBuildSigning()
}
diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts
index 6cac2cf8..9a84d0f9 100644
--- a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts
+++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts
@@ -4,10 +4,20 @@
*/
import flavors.configureSlimTests
+import org.gradle.kotlin.dsl.provideDelegate
+import org.gradle.plugins.signing.SigningExtension
plugins {
id("com.android.library")
id("com.github.android-password-store.android-common")
}
-project.configureSlimTests()
+afterEvaluate {
+ project.configureSlimTests()
+
+ extensions.configure<SigningExtension> {
+ val signingKey: String? by project
+ val signingPassword: String? by project
+ useInMemoryPgpKeys(signingKey, signingPassword)
+ }
+}
diff --git a/build-logic/android-plugins/src/main/kotlin/signing/AppSigning.kt b/build-logic/android-plugins/src/main/kotlin/signing/AppSigning.kt
new file mode 100644
index 00000000..3618b6a5
--- /dev/null
+++ b/build-logic/android-plugins/src/main/kotlin/signing/AppSigning.kt
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package signing
+
+import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
+import java.util.Properties
+import org.gradle.api.Project
+import org.gradle.kotlin.dsl.configure
+import org.gradle.kotlin.dsl.provideDelegate
+
+private const val KEYSTORE_CONFIG_PATH = "keystore.properties"
+
+/** Configure signing for all build types. */
+@Suppress("UnstableApiUsage")
+internal fun Project.configureBuildSigning() {
+ val keystoreConfigFile = rootProject.layout.projectDirectory.file(KEYSTORE_CONFIG_PATH)
+ if (keystoreConfigFile.asFile.exists()) {
+ extensions.configure<BaseAppModuleExtension> {
+ 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) }
+ }
+ }
+}
diff --git a/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt b/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt
new file mode 100644
index 00000000..671e9e5b
--- /dev/null
+++ b/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt
@@ -0,0 +1,108 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package versioning
+
+import com.android.build.gradle.internal.plugins.AppPlugin
+import com.vdurmont.semver4j.Semver
+import java.io.OutputStream
+import java.util.Properties
+import org.gradle.api.Plugin
+import org.gradle.api.Project
+
+private const val VERSIONING_PROP_FILE = "version.properties"
+private const val VERSIONING_PROP_VERSION_NAME = "versioning-plugin.versionName"
+private const val VERSIONING_PROP_VERSION_CODE = "versioning-plugin.versionCode"
+private const val VERSIONING_PROP_COMMENT =
+ """
+This file was automatically generated by 'versioning-plugin'. DO NOT EDIT MANUALLY.
+"""
+
+/**
+ * A Gradle [Plugin] that takes a [Project] with the [AppPlugin] applied and dynamically sets the
+ * versionCode and versionName properties based on values read from a [VERSIONING_PROP_FILE] file in
+ * the [Project.getBuildDir] directory. It also adds Gradle tasks to bump the major, minor, and
+ * patch versions along with one to prepare the next snapshot.
+ */
+@Suppress("UnstableApiUsage", "NAME_SHADOWING")
+class VersioningPlugin : Plugin<Project> {
+
+ /** Generate the Android 'versionCode' property */
+ private fun Semver.androidCode(): Int {
+ return major * 1_00_00 + minor * 1_00 + patch
+ }
+
+ /** Write an Android-specific variant of [this] to [stream] */
+ private fun Semver.writeForAndroid(stream: OutputStream) {
+ val newVersionCode = androidCode()
+ val props = Properties()
+ props.setProperty(VERSIONING_PROP_VERSION_CODE, "$newVersionCode")
+ props.setProperty(VERSIONING_PROP_VERSION_NAME, toString())
+ props.store(stream, VERSIONING_PROP_COMMENT)
+ }
+
+ override fun apply(project: Project) {
+ with(project) {
+ val appPlugin =
+ requireNotNull(plugins.findPlugin(AppPlugin::class.java)) {
+ "Plugin 'com.android.application' must be applied to use this plugin"
+ }
+ val propFile = layout.projectDirectory.file(VERSIONING_PROP_FILE)
+ require(propFile.asFile.exists()) {
+ "A 'version.properties' file must exist in the project subdirectory to use this plugin"
+ }
+ val contents = providers.fileContents(propFile).asText.forUseAtConfigurationTime()
+ val versionProps = Properties().also { it.load(contents.get().byteInputStream()) }
+ val versionName =
+ requireNotNull(versionProps.getProperty(VERSIONING_PROP_VERSION_NAME)) {
+ "version.properties must contain a '$VERSIONING_PROP_VERSION_NAME' property"
+ }
+ val versionCode =
+ requireNotNull(versionProps.getProperty(VERSIONING_PROP_VERSION_CODE).toInt()) {
+ "version.properties must contain a '$VERSIONING_PROP_VERSION_CODE' property"
+ }
+ appPlugin.extension.defaultConfig.versionName = versionName
+ appPlugin.extension.defaultConfig.versionCode = versionCode
+ afterEvaluate {
+ val version = Semver(versionName)
+ tasks.register("clearPreRelease") {
+ doLast { version.withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) }
+ }
+ tasks.register("bumpMajor") {
+ doLast {
+ version
+ .withIncMajor()
+ .withClearedSuffix()
+ .writeForAndroid(propFile.asFile.outputStream())
+ }
+ }
+ tasks.register("bumpMinor") {
+ doLast {
+ version
+ .withIncMinor()
+ .withClearedSuffix()
+ .writeForAndroid(propFile.asFile.outputStream())
+ }
+ }
+ tasks.register("bumpPatch") {
+ doLast {
+ version
+ .withIncPatch()
+ .withClearedSuffix()
+ .writeForAndroid(propFile.asFile.outputStream())
+ }
+ }
+ tasks.register("bumpSnapshot") {
+ doLast {
+ version
+ .withIncMinor()
+ .withSuffix("SNAPSHOT")
+ .writeForAndroid(propFile.asFile.outputStream())
+ }
+ }
+ }
+ }
+ }
+}