From 0a21bb38f755360ac80383f77bec34f65a12a328 Mon Sep 17 00:00:00 2001 From: mlykotom Date: Mon, 21 Nov 2022 10:35:10 +0100 Subject: [PATCH] Fix flavors change --- app/build.gradle.kts | 7 +++-- benchmarks/build.gradle.kts | 16 ++++++++++- .../google/samples/apps/nowinandroid/Utils.kt | 13 ++++----- .../samples/apps/nowinandroid/Flavor.kt | 12 +++++---- .../samples/apps/nowinandroid/NiABuildType.kt | 27 +++++++++++++++++++ 5 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/NiABuildType.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b73bdef64..0ab61a669 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,3 +1,5 @@ +import com.google.samples.apps.nowinandroid.NiABuildType + /* * Copyright 2021 The Android Open Source Project * @@ -38,10 +40,11 @@ android { buildTypes { val debug by getting { - applicationIdSuffix = ".debug" + applicationIdSuffix = NiABuildType.DEBUG.applicationIdSuffix } val release by getting { isMinifyEnabled = true + applicationIdSuffix = NiABuildType.RELEASE.applicationIdSuffix proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") // To publish on the Play store a private signing key is required, but to allow anyone @@ -59,7 +62,7 @@ android { proguardFiles("benchmark-rules.pro") // FIXME enabling minification breaks access to demo backend. isMinifyEnabled = false - applicationIdSuffix = ".benchmark" + applicationIdSuffix = NiABuildType.BENCHMARK.applicationIdSuffix } } diff --git a/benchmarks/build.gradle.kts b/benchmarks/build.gradle.kts index 15b2422bf..ab7f6ac0c 100644 --- a/benchmarks/build.gradle.kts +++ b/benchmarks/build.gradle.kts @@ -14,6 +14,7 @@ * limitations under the License. */ import com.android.build.api.dsl.ManagedVirtualDevice +import com.google.samples.apps.nowinandroid.NiABuildType import com.google.samples.apps.nowinandroid.configureFlavors plugins { @@ -26,6 +27,8 @@ android { defaultConfig { minSdk = 23 testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + + buildConfigField("String", "APP_BUILD_TYPE_SUFFIX", "\"\"") } buildFeatures { @@ -41,13 +44,24 @@ android { isDebuggable = true signingConfig = signingConfigs.getByName("debug") matchingFallbacks.add("release") + buildConfigField( + "String", + "APP_BUILD_TYPE_SUFFIX", + "\"${NiABuildType.BENCHMARK.applicationIdSuffix ?: ""}\"" + ) } } // Use the same flavor dimensions as the application to allow generating Baseline Profiles on prod, // which is more close to what will be shipped to users (no fake data), but has ability to run the // benchmarks on demo, so we benchmark on stable data. - configureFlavors(this) + configureFlavors(this) { flavor -> + buildConfigField( + "String", + "APP_FLAVOR_SUFFIX", + "\"${flavor.applicationIdSuffix ?: ""}\"" + ) + } targetProjectPath = ":app" experimentalProperties["android.experimental.self-instrumenting"] = true diff --git a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/Utils.kt b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/Utils.kt index ffb3da23c..dc353ccb8 100644 --- a/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/Utils.kt +++ b/benchmarks/src/main/java/com/google/samples/apps/nowinandroid/Utils.kt @@ -21,11 +21,8 @@ import com.google.samples.apps.nowinandroid.benchmarks.BuildConfig /** * Convenience parameter to use proper package name with regards to build type and build flavor. */ -val PACKAGE_NAME = StringBuilder("com.google.samples.apps.nowinandroid").apply { - if (BuildConfig.FLAVOR != "prod") { - append(".${BuildConfig.FLAVOR}") - } - if (BuildConfig.BUILD_TYPE != "release") { - append(".${BuildConfig.BUILD_TYPE}") - } -}.toString() +val PACKAGE_NAME = buildString { + append("com.google.samples.apps.nowinandroid") + append(BuildConfig.APP_FLAVOR_SUFFIX) + append(BuildConfig.APP_BUILD_TYPE_SUFFIX) +} diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Flavor.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Flavor.kt index e251267ce..0367b2c43 100644 --- a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Flavor.kt +++ b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/Flavor.kt @@ -2,8 +2,8 @@ package com.google.samples.apps.nowinandroid import com.android.build.api.dsl.ApplicationExtension import com.android.build.api.dsl.ApplicationProductFlavor -import com.android.build.api.dsl.ApplicationVariantDimension import com.android.build.api.dsl.CommonExtension +import com.android.build.api.dsl.ProductFlavor import org.gradle.api.Project enum class FlavorDimension { @@ -13,20 +13,22 @@ enum class FlavorDimension { // The content for the app can either come from local static data which is useful for demo // purposes, or from a production backend server which supplies up-to-date, real content. // These two product flavors reflect this behaviour. -enum class Flavor (val dimension : FlavorDimension, val applicationIdSuffix : String? = null) { +enum class Flavor(val dimension: FlavorDimension, val applicationIdSuffix: String? = null) { demo(FlavorDimension.contentType), prod(FlavorDimension.contentType, ".prod") } fun Project.configureFlavors( - commonExtension: CommonExtension<*, *, *, *> + commonExtension: CommonExtension<*, *, *, *>, + flavorConfigurationBlock: ProductFlavor.(flavor: Flavor) -> Unit = {} ) { commonExtension.apply { flavorDimensions += FlavorDimension.contentType.name productFlavors { - Flavor.values().forEach{ + Flavor.values().forEach { create(it.name) { dimension = it.dimension.name + flavorConfigurationBlock(this, it) if (this@apply is ApplicationExtension && this is ApplicationProductFlavor) { if (it.applicationIdSuffix != null) { this.applicationIdSuffix = it.applicationIdSuffix @@ -36,4 +38,4 @@ fun Project.configureFlavors( } } } -} \ No newline at end of file +} diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/NiABuildType.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/NiABuildType.kt new file mode 100644 index 000000000..4b721a56a --- /dev/null +++ b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/NiABuildType.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid + +/** + * This is shared between :app and :benchmarks module to provide configurations type safety. + */ +@Suppress("unused") +enum class NiABuildType(val applicationIdSuffix: String? = null) { + DEBUG(".debug"), + RELEASE, + BENCHMARK(".benchmark") +}