Android Studio 3.0 Flavor Dimension Issue - android
Upgraded to Studio Canary build. My previous project of Telegram Messenger is giving following error.
Error:All flavors must now belong to a named flavor dimension. The flavor 'armv7' is not assigned to a flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
What should I do? I have already seen that link but couldn't understand what to do. I have 3 build variants now, release,debug and foss.
If you don't really need the mechanism, just specify a random flavor dimension in your build.gradle or build.gradle.kts:
android {
...
flavorDimensions("default")
...
}
For more information, check the migration guide
After trying and reading carefully, I solved it myself.
Solution is to add the following line in build.gradle.
flavorDimensions "versionCode"
android {
compileSdkVersion 24
.....
flavorDimensions "versionCode"
}
Here you can resolve this issue, you need to add flavorDimension with productFlavors's name and need to define dimension as well, see below example and for more information see here https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html
flavorDimensions 'yourAppName' //here defined dimensions
productFlavors {
production {
dimension 'yourAppName' //you just need to add this line
//here you no need to write applicationIdSuffix because by default it will point to your app package which is also available inside manifest.xml file.
}
staging {
dimension 'yourAppName' //added here also
applicationIdSuffix ".staging"//(.staging) will be added after your default package name.
//or you can also use applicationId="your_package_name.staging" instead of applicationIdSuffix but remember if you are using applicationId then You have to mention full package name.
//versionNameSuffix "-staging"
}
develop {
dimension 'yourAppName' //add here too
applicationIdSuffix ".develop"
//versionNameSuffix "-develop"
}
If you want not to use dimensions you should use this line
android {
compileSdkVersion 24
...
flavorDimensions "default"
...
}
but if you want ti use dimensions you should declare your dimension name first and then use this name after
THIS example is from the documentations:
android {
...
buildTypes {
debug {...}
release {...}
}
// Specifies the flavor dimensions you want to use. The order in which you
// list each dimension determines its priority, from highest to lowest,
// when Gradle merges variant sources and configurations. You must assign
// each product flavor you configure to one of the flavor dimensions.
flavorDimensions "api", "mode"
productFlavors {
demo {
// Assigns this product flavor to the "mode" flavor dimension.
dimension "mode"
...
}
full {
dimension "mode"
...
}
// Configurations in the "api" product flavors override those in "mode"
// flavors and the defaultConfig block. Gradle determines the priority
// between flavor dimensions based on the order in which they appear next
// to the flavorDimensions property above--the first dimension has a higher
// priority than the second, and so on.
minApi24 {
dimension "api"
minSdkVersion 24
// To ensure the target device receives the version of the app with
// the highest compatible API level, assign version codes in increasing
// value with API level. To learn more about assigning version codes to
// support app updates and uploading to Google Play, read Multiple APK Support
versionCode 30000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi24"
...
}
minApi23 {
dimension "api"
minSdkVersion 23
versionCode 20000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi23"
...
}
minApi21 {
dimension "api"
minSdkVersion 21
versionCode 10000 + android.defaultConfig.versionCode
versionNameSuffix "-minApi21"
...
}
}
}
...
I have used flavorDimensions for my application in build.gradle (Module: app)
flavorDimensions "tier"
productFlavors {
production {
flavorDimensions "tier"
//manifestPlaceholders = [appName: APP_NAME]
//signingConfig signingConfigs.config
}
staging {
flavorDimensions "tier"
//manifestPlaceholders = [appName: APP_NAME_STAGING]
//applicationIdSuffix ".staging"
//versionNameSuffix "-staging"
//signingConfig signingConfigs.config
}
}
Check this link for more info
// Specifies two flavor dimensions.
flavorDimensions "tier", "minApi"
productFlavors {
free {
// Assigns this product flavor to the "tier" flavor dimension. Specifying
// this property is optional if you are using only one dimension.
dimension "tier"
...
}
paid {
dimension "tier"
...
}
minApi23 {
dimension "minApi"
...
}
minApi18 {
dimension "minApi"
...
}
}
in KotlinDSL you can use like this :
flavorDimensions ("PlaceApp")
productFlavors {
create("tapsi") {
setDimension("PlaceApp")
buildConfigField("String", "API_BASE_URL", "https://xxx/x/x/")
}
}
If you have simple flavors (free/pro, demo/full etc.) then add to build.gradle file:
android {
...
flavorDimensions "version"
productFlavors {
free{
dimension "version"
...
}
pro{
dimension "version"
...
}
}
By dimensions you can create "flavors in flavors". Read more.
Related
Why more than two android flavor dimensions don't work properly?
Here is a problem, for example, I need the next three dimensions for my app: "brand", "environment", "market". The configuration will look like this: flavorDimensions "brand", "environment", "market" productFlavors { // brand nike{ dimension "brand" applicationId "com.android.nike" } adidas{ dimension "brand" applicationId "com.android.adidas" } // environment test{ dimension "environment" applicationIdSuffix ".test" } prod { dimension "environment" } // market google { dimension "market" } amazon { dimension "market" } } As a result, I can build the next 8 applications (I ignoring build types in this example): nikeTestGoogle nikeTestAmazon nikeProdGoogle nikeProdAmazon adidasTestGoogle adidasTestAmazon adidasProdGoogle adidasProdAmazon As I understand documentation about flavor dimensions (https://developer.android.com/studio/build/build-variants#flavor-dimensions), application nikeTestGoogle use files from the next folders: main, nike, test, google, nikeTest, nikeTestGoogle. But it doesn't work, Android studio sees only files in main, nike, test, google, and nikeTestGoogle. Files in nikeTest ignored and even doesn't mark as currently active. Does anyone know why it happened?
How to include a dependency in an Android gradle file based on flavorDimension?
I have an Android project with 5 flavors and 2 flavorDimensions. See Below: flavorDimensions "env", "device" productFlavors { dev { applicationIdSuffix ".dev" dimension "env" } staging { applicationIdSuffix ".staging" dimension "env" } prod { applicationIdSuffix "" dimension "env" } amazon { dimension "device" } google { dimension "device" } } Is there a way to only include a dependency to one flavorDimension instead of just one flavor? Basically I have a dependency I only want included in googleDev, googleStaging, and googleProd (but not the amazon dimension) Is there an easy way to do this without repeating each dependency 3 times?
App run failed after adding multi flavor configuration
I add multi flavor configuration and after running the project I got below error. How to fixe it please ?
Please make this change to your build.gradle, resync, and then clean the project before rebuilding: flavorDimensions "tier" productFlavors { free { applicationId "capstone.nanodegree.udacity.com.mypodcast.free" dimension "tier" } paid { applicationId "capstone.nanodegree.udacity.com.mypodcast.paid" dimension "tier" } }
In the documentation they say // If you have different applicationIds for buildTypes or productFlavors uncomment this block. //javaCompileOptions { // annotationProcessorOptions { // arguments = ["resourcePackageName": android.defaultConfig.applicationId] // } //} So I just add and uncomment these lines in gradle build script and then its works. Thanks for your answers.
Empty test suite and custom package name [duplicate]
This question already has answers here: How do I set my Android app package name based on two different flavor dimensions? (3 answers) Closed 6 years ago. I have an app with flavorDimensions and productFlavors and I am customising the package name of Trial variants with the build.gradle script as follows: defaultConfig { applicationVariants.all { variant -> def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName(); if(flavorString.endsWith("Trial")) { def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor(); mergedFlavour.setApplicationId(mergedFlavour.getApplicationId().concat("trial")) } } } Everything works fine but when I try to run my test cases against Trial variants I get the message: Running tests Test running startedFinish Empty test suite. I can run the tests against other variants perfectly, but I would like to know how to fix this situation. Before anyone suggest to use applicationIdSuffix in buildType, I don't think that suits this case, I need to add a suffix to variants. This is the build.gradle script for flavorDimensions and productFlavors: flavorDimensions "merchant", "version" productFlavors { merchant1 { flavorDimension "merchant" applicationId "com.merchant1" } merchant2 { flavorDimension "merchant" applicationId "com.merchant2" } trial { flavorDimension "version" } prod { flavorDimension "version" } }
You can now set applicationIdSuffix for productFlavors: android { productFlavors { free { applicationIdSuffix = ".free" } prod { } } } Source: http://android-developers.blogspot.ie/2015/12/leveraging-product-flavors-in-android.html
Android using Gradle Build flavors in the code like an if case
I'm trying to work with build flavors. In my build.gradle I've defined 2 flavors, a normal flavor and an admin flavor. Basicly, the admin flavor has an extra button on the main activity. I understand that I can define different packages/classes for different flavors. But is there a way to make a sort of if case to add/remove a piece of code depending on the flavor? Basicly, I would need two versions of an Activity. But I don't want two entire different versions of the activity and maintain them. So in my activity I would like to do => gradle check if flavor is 'admin' => if yes, add this code of the button Is this possible? Or would you need two different physical activities and thus maintain both of them when you add functionality afterwards.
BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig.FLAVOR.equals("admin")) { ... } But if you have multiple flavor dimensions: flavorDimensions "access", "color" productFlavors { normal { dimension "access" } admin { dimension "access" } red { dimension "color" } blue { dimension "color" } } there are also BuildConfig.FLAVOR_access and BuildConfig.FLAVOR_color fields so you should check it like this: if (BuildConfig.FLAVOR_access.equals("admin")) { ... } And BuildConfig.FLAVOR contains full flavor name. For example, adminBlue.
To avoid plain string in the condition, you can define a boolean property: productFlavors { normal { flavorDimension "access" buildConfigField 'boolean', 'IS_ADMIN', 'false' } admin { flavorDimension "access" buildConfigField 'boolean', 'IS_ADMIN', 'true' } } Then you can use it like this: if (BuildConfig.IS_ADMIN) { ... }
You can define either different build configuration fields or different resource values (like string values) per flavor, e.g. (as per Google's gradle tips and recipes), e.g., android { ... buildTypes { release { // These values are defined only for the release build, which // is typically used for full builds and continuous builds. buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"") resValue("string", "build_time", "${minutesSinceEpoch}") ... } debug { // Use static values for incremental builds to ensure that // resource files and BuildConfig aren't rebuilt with each run. // If they were dynamic, they would prevent certain benefits of // Instant Run as well as Gradle UP-TO-DATE checks. buildConfigField("String", "BUILD_TIME", "\"0\"") resValue("string", "build_time", "0") } } } So in this case, something like productFlavors { normal { dimension "access" buildConfigField("boolean", "IS_ADMIN", "false") } admin { dimension "access" buildConfigField("boolean", "IS_ADMIN", "true") } } and then use it like if (BuildConfig.IS_ADMIN) { ... } else { ... } or if it is just to have different string values for different flavors, it can be done with different resValues and then you don't even need the if/then
You can try this way productFlavors { def app_name = "you app name" development { versionCode 1 versionName "1.0.1" buildConfigField 'String', 'varibalename', "" } release { versionCode 1 versionName "1.0.1" buildConfigField 'String', 'varibalename', "" } } if(BuildConfig.varibalename){}