From dfee170bd813d267272d0d4a8c64a4cbef965619 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 29 Nov 2021 01:41:24 +0530 Subject: build-logic: add android convention plugins --- ...d-password-store.android-application.gradle.kts | 63 ++++++++++++++++++++++ ...ndroid-password-store.android-common.gradle.kts | 39 ++++++++++++++ ...droid-password-store.android-library.gradle.kts | 13 +++++ .../src/main/kotlin/flavors/ProductFlavors.kt | 15 ++++++ .../src/main/kotlin/flavors/SlimTests.kt | 44 +++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts create mode 100644 build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts create mode 100644 build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts create mode 100644 build-logic/android-plugins/src/main/kotlin/flavors/ProductFlavors.kt create mode 100644 build-logic/android-plugins/src/main/kotlin/flavors/SlimTests.kt (limited to 'build-logic/android-plugins/src/main/kotlin') 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 new file mode 100644 index 00000000..9e38476d --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts @@ -0,0 +1,63 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +import com.android.build.gradle.internal.dsl.BaseAppModuleExtension +import flavors.FlavorDimensions +import flavors.ProductFlavors +import flavors.configureSlimTests +import org.gradle.kotlin.dsl.configure + +plugins { + id("com.android.application") + id("com.github.android-password-store.android-common") +} + +fun Project.isSnapshot(): Boolean { + with(project.providers) { + val workflow = environmentVariable("GITHUB_WORKFLOW").forUseAtConfigurationTime() + val snapshot = environmentVariable("SNAPSHOT").forUseAtConfigurationTime() + return workflow.isPresent || snapshot.isPresent + } +} + +extensions.configure { + 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", + "proguard-rules-missing-classes.pro", + ) + ) + buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "${project.isSnapshot()}") + } + named("debug") { + applicationIdSuffix = ".debug" + versionNameSuffix = "-debug" + isMinifyEnabled = false + buildConfigField("boolean", "ENABLE_DEBUG_FEATURES", "true") + } + } + + flavorDimensions.add(FlavorDimensions.FREE) + productFlavors { + register(ProductFlavors.FREE) {} + register(ProductFlavors.NON_FREE) {} + } + + project.configureSlimTests() +} 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 new file mode 100644 index 00000000..0d873892 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts @@ -0,0 +1,39 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +import com.android.build.gradle.TestedExtension + +extensions.configure { + setCompileSdkVersion(31) + defaultConfig { + minSdk = 23 + targetSdk = 29 + } + + sourceSets { + named("main") { java.srcDirs("src/main/kotlin") } + named("test") { java.srcDirs("src/test/kotlin") } + named("androidTest") { java.srcDirs("src/androidTest/kotlin") } + } + + packagingOptions { + resources.excludes.add("**/*.version") + resources.excludes.add("**/*.txt") + resources.excludes.add("**/*.kotlin_module") + resources.excludes.add("**/plugin.properties") + resources.excludes.add("**/META-INF/AL2.0") + resources.excludes.add("**/META-INF/LGPL2.1") + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + testOptions { + animationsDisabled = true + unitTests.isReturnDefaultValues = true + } +} 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 new file mode 100644 index 00000000..6cac2cf8 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-library.gradle.kts @@ -0,0 +1,13 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +import flavors.configureSlimTests + +plugins { + id("com.android.library") + id("com.github.android-password-store.android-common") +} + +project.configureSlimTests() diff --git a/build-logic/android-plugins/src/main/kotlin/flavors/ProductFlavors.kt b/build-logic/android-plugins/src/main/kotlin/flavors/ProductFlavors.kt new file mode 100644 index 00000000..a3185f65 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/flavors/ProductFlavors.kt @@ -0,0 +1,15 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package flavors + +object FlavorDimensions { + const val FREE = "free" +} + +object ProductFlavors { + const val FREE = "free" + const val NON_FREE = "nonFree" +} diff --git a/build-logic/android-plugins/src/main/kotlin/flavors/SlimTests.kt b/build-logic/android-plugins/src/main/kotlin/flavors/SlimTests.kt new file mode 100644 index 00000000..ae14fdab --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/flavors/SlimTests.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package flavors + +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.android.build.api.variant.LibraryAndroidComponentsExtension +import org.gradle.api.Project +import org.gradle.kotlin.dsl.findByType +import org.gradle.language.nativeplatform.internal.BuildType + +/** + * When the "slimTests" project property is provided, disable the unit test tasks on `release` build + * type and `nonFree` product flavor to avoid running the same tests repeatedly in different build + * variants. + * + * Examples: `./gradlew test -PslimTests` will run unit tests for `nonFreeDebug` and `debug` build + * variants in Android App and Library projects, and all tests in JVM projects. + */ +internal fun Project.configureSlimTests() { + if (providers.gradleProperty(SLIM_TESTS_PROPERTY).forUseAtConfigurationTime().isPresent) { + // disable unit test tasks on the release build type for Android Library projects + extensions.findByType()?.run { + beforeVariants(selector().withBuildType(BuildType.RELEASE.name)) { + it.enableUnitTest = false + it.enableAndroidTest = false + } + } + + // disable unit test tasks on the release build type and free flavor for Android Application + // projects. + extensions.findByType()?.run { + beforeVariants(selector().withBuildType(BuildType.RELEASE.name)) { it.enableUnitTest = false } + beforeVariants(selector().withFlavor(FlavorDimensions.FREE to ProductFlavors.NON_FREE)) { + it.enableUnitTest = false + it.enableAndroidTest = false + } + } + } +} + +private const val SLIM_TESTS_PROPERTY = "slimTests" -- cgit v1.2.3