From acc448ce740b3756c622daf35d7be8372e76cebb Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 23 Feb 2022 14:25:03 +0530 Subject: [PATCH] Use explicit Gradle tasks to handle build artifact collection (#1745) --- .github/workflows/deploy_snapshot.yml | 2 +- app/build.gradle.kts | 9 +---- .../main/kotlin/artifacts/CollectApksTask.kt | 39 +++++++++++++++++++ .../kotlin/artifacts/CollectBundleTask.kt | 30 ++++++++++++++ ...sword-store.android-application.gradle.kts | 1 + ...d-password-store.android-common.gradle.kts | 1 + ...password-store.rename-artifacts.gradle.kts | 22 +++++++++++ .../src/main/kotlin/crowdin/CrowdinPlugin.kt | 1 + .../main/kotlin/psl/PublicSuffixListPlugin.kt | 1 + scripts/deploy-snapshot.sh | 5 +-- 10 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt create mode 100644 build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt create mode 100644 build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts diff --git a/.github/workflows/deploy_snapshot.yml b/.github/workflows/deploy_snapshot.yml index a055c0b2..4e66245c 100644 --- a/.github/workflows/deploy_snapshot.yml +++ b/.github/workflows/deploy_snapshot.yml @@ -33,7 +33,7 @@ jobs: SNAPSHOT: "true" SENTRY_DSN: ${{ secrets.SENTRY_DSN }} with: - arguments: --no-configuration-cache :app:assembleFreeRelease :app:assembleNonFreeRelease + arguments: --no-configuration-cache collectFreeReleaseApks collectNonFreeReleaseApks - name: Clean secrets run: scripts/signing-cleanup.sh diff --git a/app/build.gradle.kts b/app/build.gradle.kts index ebf9a98c..b3414460 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -2,7 +2,6 @@ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. * SPDX-License-Identifier: GPL-3.0-only */ -import com.android.build.gradle.internal.api.BaseVariantOutputImpl plugins { id("com.github.android-password-store.android-application") @@ -11,6 +10,7 @@ plugins { id("com.github.android-password-store.kotlin-kapt") id("com.github.android-password-store.versioning-plugin") id("com.github.android-password-store.sentry") + id("com.github.android-password-store.rename-artifacts") id("dagger.hilt.android.plugin") } @@ -28,13 +28,6 @@ fun isSnapshot(): Boolean { } android { - if (isSnapshot()) { - applicationVariants.all { - outputs.all { - (this as BaseVariantOutputImpl).outputFileName = "aps-${flavorName}_$versionName.apk" - } - } - } compileOptions { isCoreLibraryDesugaringEnabled = true } defaultConfig { 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 + + @get:Internal abstract val builtArtifactsLoader: Property + + @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 + + @get:Input abstract val versionName: Property + + @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("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("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")) + } + } +} diff --git a/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt b/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt index 9152aff4..e147b42c 100644 --- a/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt +++ b/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt @@ -24,6 +24,7 @@ private const val EXCEPTION_MESSAGE = private const val CROWDIN_BUILD_API_URL = "https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s" +@Suppress("Unused") class CrowdinDownloadPlugin : Plugin { override fun apply(project: Project) { diff --git a/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt b/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt index bc503fde..9d4c3f63 100644 --- a/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt +++ b/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt @@ -20,6 +20,7 @@ import org.gradle.kotlin.dsl.register * Base on PublicSuffixListGenerator from OkHttp: * https://github.com/square/okhttp/blob/master/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixListGenerator.java */ +@Suppress("Unused") class PublicSuffixListPlugin : Plugin { override fun apply(project: Project) { project.tasks.register("updatePSL") { diff --git a/scripts/deploy-snapshot.sh b/scripts/deploy-snapshot.sh index 34acdc2e..0f10d9eb 100755 --- a/scripts/deploy-snapshot.sh +++ b/scripts/deploy-snapshot.sh @@ -11,8 +11,5 @@ mkdir -p "$SSHDIR" echo "$ACTIONS_DEPLOY_KEY" > "$SSHDIR/key" chmod 600 "$SSHDIR/key" export SERVER_DEPLOY_STRING="$SSH_USERNAME@$SERVER_ADDRESS:$SERVER_DESTINATION" -mkdir -p "$GITHUB_WORKSPACE/APS" -cp -v ./app/build/outputs/apk/free/release/*.apk "$GITHUB_WORKSPACE/APS/" -cp -v ./app/build/outputs/apk/nonFree/release/*.apk "$GITHUB_WORKSPACE/APS/" -cd "$GITHUB_WORKSPACE/APS" +cd "$GITHUB_WORKSPACE/app/outputs/" rsync -ahvcr --omit-dir-times --progress --delete --no-o --no-g -e "ssh -i $SSHDIR/key -o StrictHostKeyChecking=no -p $SSH_PORT" . "$SERVER_DEPLOY_STRING"