diff options
Diffstat (limited to 'build-logic/android-plugins')
6 files changed, 183 insertions, 1 deletions
diff --git a/build-logic/android-plugins/build.gradle.kts b/build-logic/android-plugins/build.gradle.kts index 8e2d31cc..53e1ce26 100644 --- a/build-logic/android-plugins/build.gradle.kts +++ b/build-logic/android-plugins/build.gradle.kts @@ -1,3 +1,11 @@ -plugins { `kotlin-dsl` } +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +plugins { + `kotlin-dsl` + `kotlin-dsl-precompiled-script-plugins` +} dependencies { implementation(libs.build.agp) } 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<BaseAppModuleExtension> { + 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<TestedExtension> { + 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<LibraryAndroidComponentsExtension>()?.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<ApplicationAndroidComponentsExtension>()?.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" |