aboutsummaryrefslogtreecommitdiff
path: root/build-logic/android-plugins/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-02-23 14:25:03 +0530
committerGitHub <noreply@github.com>2022-02-23 14:25:03 +0530
commitacc448ce740b3756c622daf35d7be8372e76cebb (patch)
treed826cfae39eea5c71104e5a13cd94eea1783e36b /build-logic/android-plugins/src/main
parentf08ad35d2e26b3f222cca4f973a865bdd815eeb3 (diff)
Use explicit Gradle tasks to handle build artifact collection (#1745)
Diffstat (limited to 'build-logic/android-plugins/src/main')
-rw-r--r--build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt39
-rw-r--r--build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt30
-rw-r--r--build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts1
-rw-r--r--build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts1
-rw-r--r--build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts22
5 files changed, 93 insertions, 0 deletions
diff --git a/build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt
new file mode 100644
index 00000000..23f94f22
--- /dev/null
+++ b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt
@@ -0,0 +1,39 @@
+package artifacts
+
+import com.android.build.api.variant.BuiltArtifactsLoader
+import java.io.File
+import org.gradle.api.DefaultTask
+import org.gradle.api.file.DirectoryProperty
+import org.gradle.api.provider.Property
+import org.gradle.api.tasks.CacheableTask
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.InputFiles
+import org.gradle.api.tasks.Internal
+import org.gradle.api.tasks.OutputDirectory
+import org.gradle.api.tasks.PathSensitive
+import org.gradle.api.tasks.PathSensitivity
+import org.gradle.api.tasks.TaskAction
+
+/** Task to collect APKs in a given [outputDirectory]. */
+@CacheableTask
+abstract class CollectApksTask : DefaultTask() {
+ @get:InputFiles @get:PathSensitive(PathSensitivity.NONE) abstract val apkFolder: DirectoryProperty
+
+ @get:Input abstract val variantName: Property<String>
+
+ @get:Internal abstract val builtArtifactsLoader: Property<BuiltArtifactsLoader>
+
+ @get:OutputDirectory abstract val outputDirectory: DirectoryProperty
+
+ @TaskAction
+ fun run() {
+ val outputDir = outputDirectory.asFile.get()
+ outputDir.mkdirs()
+ val builtArtifacts =
+ builtArtifactsLoader.get().load(apkFolder.get()) ?: throw RuntimeException("Cannot load APKs")
+ builtArtifacts.elements.forEach { artifact ->
+ File(artifact.outputFile)
+ .renameTo(outputDir.resolve("APS-${variantName.get()}-${artifact.versionName}.apk"))
+ }
+ }
+}
diff --git a/build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt
new file mode 100644
index 00000000..a735fb7b
--- /dev/null
+++ b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt
@@ -0,0 +1,30 @@
+package artifacts
+
+import org.gradle.api.DefaultTask
+import org.gradle.api.file.DirectoryProperty
+import org.gradle.api.file.RegularFileProperty
+import org.gradle.api.provider.Property
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.InputFile
+import org.gradle.api.tasks.OutputDirectory
+import org.gradle.api.tasks.TaskAction
+
+abstract class CollectBundleTask : DefaultTask() {
+ @get:InputFile abstract val bundleFile: RegularFileProperty
+
+ @get:Input abstract val variantName: Property<String>
+
+ @get:Input abstract val versionName: Property<String>
+
+ @get:OutputDirectory abstract val outputDirectory: DirectoryProperty
+
+ @TaskAction
+ fun taskAction() {
+ val outputDir = outputDirectory.asFile.get()
+ outputDir.mkdirs()
+ bundleFile
+ .get()
+ .asFile
+ .renameTo(outputDir.resolve("APS-${variantName.get()}-${versionName.get()}.aab"))
+ }
+}
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 859203a9..e56d400b 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
@@ -2,6 +2,7 @@
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
+@file:Suppress("UnstableApiUsage")
import flavors.FlavorDimensions
import flavors.ProductFlavors
diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts
index 4f76ba19..9400acc2 100644
--- a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts
+++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts
@@ -2,6 +2,7 @@
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
+@file:Suppress("UnstableApiUsage")
import com.android.build.gradle.TestedExtension
import flavors.configureSlimTests
diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts
new file mode 100644
index 00000000..9c74c360
--- /dev/null
+++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts
@@ -0,0 +1,22 @@
+import artifacts.CollectApksTask
+import artifacts.CollectBundleTask
+import com.android.build.api.artifact.SingleArtifact
+
+plugins { id("com.android.application") }
+
+androidComponents {
+ onVariants { variant ->
+ project.tasks.register<CollectApksTask>("collect${variant.name.capitalize()}Apks") {
+ variantName.set(variant.name)
+ apkFolder.set(variant.artifacts.get(SingleArtifact.APK))
+ builtArtifactsLoader.set(variant.artifacts.getBuiltArtifactsLoader())
+ outputDirectory.set(project.layout.projectDirectory.dir("outputs"))
+ }
+ project.tasks.register<CollectBundleTask>("collect${variant.name.capitalize()}Bundle") {
+ variantName.set(variant.name)
+ versionName.set(android.defaultConfig.versionName)
+ bundleFile.set(variant.artifacts.get(SingleArtifact.BUNDLE))
+ outputDirectory.set(project.layout.projectDirectory.dir("outputs"))
+ }
+ }
+}