Fix flavors change

pull/458/head
mlykotom 2 years ago
parent 7187abb813
commit 6c1f8394d9

@ -1,3 +1,5 @@
import com.google.samples.apps.nowinandroid.NiABuildType
/* /*
* Copyright 2021 The Android Open Source Project * Copyright 2021 The Android Open Source Project
* *
@ -38,10 +40,11 @@ android {
buildTypes { buildTypes {
val debug by getting { val debug by getting {
applicationIdSuffix = ".debug" applicationIdSuffix = NiABuildType.DEBUG.applicationIdSuffix
} }
val release by getting { val release by getting {
isMinifyEnabled = true isMinifyEnabled = true
applicationIdSuffix = NiABuildType.RELEASE.applicationIdSuffix
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") 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 // 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") proguardFiles("benchmark-rules.pro")
// FIXME enabling minification breaks access to demo backend. // FIXME enabling minification breaks access to demo backend.
isMinifyEnabled = false isMinifyEnabled = false
applicationIdSuffix = ".benchmark" applicationIdSuffix = NiABuildType.BENCHMARK.applicationIdSuffix
} }
} }

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
import com.android.build.api.dsl.ManagedVirtualDevice import com.android.build.api.dsl.ManagedVirtualDevice
import com.google.samples.apps.nowinandroid.NiABuildType
import com.google.samples.apps.nowinandroid.configureFlavors import com.google.samples.apps.nowinandroid.configureFlavors
plugins { plugins {
@ -26,6 +27,8 @@ android {
defaultConfig { defaultConfig {
minSdk = 23 minSdk = 23
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
buildConfigField("String", "APP_BUILD_TYPE_SUFFIX", "\"\"")
} }
buildFeatures { buildFeatures {
@ -41,13 +44,24 @@ android {
isDebuggable = true isDebuggable = true
signingConfig = signingConfigs.getByName("debug") signingConfig = signingConfigs.getByName("debug")
matchingFallbacks.add("release") 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, // 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 // 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. // benchmarks on demo, so we benchmark on stable data.
configureFlavors(this) configureFlavors(this) { flavor ->
buildConfigField(
"String",
"APP_FLAVOR_SUFFIX",
"\"${flavor.applicationIdSuffix ?: ""}\""
)
}
targetProjectPath = ":app" targetProjectPath = ":app"
experimentalProperties["android.experimental.self-instrumenting"] = true experimentalProperties["android.experimental.self-instrumenting"] = true

@ -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. * 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 { val PACKAGE_NAME = buildString {
if (BuildConfig.FLAVOR != "prod") { append("com.google.samples.apps.nowinandroid")
append(".${BuildConfig.FLAVOR}") append(BuildConfig.APP_FLAVOR_SUFFIX)
} append(BuildConfig.APP_BUILD_TYPE_SUFFIX)
if (BuildConfig.BUILD_TYPE != "release") { }
append(".${BuildConfig.BUILD_TYPE}")
}
}.toString()

@ -2,8 +2,8 @@ package com.google.samples.apps.nowinandroid
import com.android.build.api.dsl.ApplicationExtension import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.ApplicationProductFlavor 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.CommonExtension
import com.android.build.api.dsl.ProductFlavor
import org.gradle.api.Project import org.gradle.api.Project
enum class FlavorDimension { 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 // 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. // purposes, or from a production backend server which supplies up-to-date, real content.
// These two product flavors reflect this behaviour. // 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), demo(FlavorDimension.contentType),
prod(FlavorDimension.contentType, ".prod") prod(FlavorDimension.contentType, ".prod")
} }
fun Project.configureFlavors( fun Project.configureFlavors(
commonExtension: CommonExtension<*, *, *, *> commonExtension: CommonExtension<*, *, *, *>,
flavorConfigurationBlock: ProductFlavor.(flavor: Flavor) -> Unit = {}
) { ) {
commonExtension.apply { commonExtension.apply {
flavorDimensions += FlavorDimension.contentType.name flavorDimensions += FlavorDimension.contentType.name
productFlavors { productFlavors {
Flavor.values().forEach{ Flavor.values().forEach {
create(it.name) { create(it.name) {
dimension = it.dimension.name dimension = it.dimension.name
flavorConfigurationBlock(this, it)
if (this@apply is ApplicationExtension && this is ApplicationProductFlavor) { if (this@apply is ApplicationExtension && this is ApplicationProductFlavor) {
if (it.applicationIdSuffix != null) { if (it.applicationIdSuffix != null) {
this.applicationIdSuffix = it.applicationIdSuffix this.applicationIdSuffix = it.applicationIdSuffix

@ -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")
}
Loading…
Cancel
Save