Android Gradle - Add packageNameSuffix on specific productFlavor - android

I have a Gradle Android Project with this product Groups and Flavors configuration:
/*
* Define different flavor groups
*/
flavorGroups 'market', 'version'
/*
* Defile product flavors
*/
productFlavors {
amazon {
flavorGroup 'market'
}
google {
flavorGroup 'market'
}
flav1 {
flavorGroup 'version'
packageName 'com.company.flav1'
}
flav2 {
flavorGroup 'version'
packageName 'com.company.flav2'
}
flav3 {
flavorGroup 'version'
packageName 'com.company.flav3'
}
}
// .. Other stuff
It works great. All sources and resources are merged correctly.
But for specific reasons I need the package suffix to be .amz for the amazon product flavor. How can I achieve that?
I tried this way:
amazon {
flavorGroup 'market'
packageNameSuffix '.amz'
}
but gradle throws an exception.

This is not possible at the moment.
packageNameSuffix is strictly a property of Build Type.
I'd like to offer a way to customize the ProductFlavor's values for a given variant (ie a given combination of all flavor dimensions and of build type) but it's not possible at the moment.
Instead of a new dimension of flavor, you could do a "amzRelease" buildtype that extends the existing release buildtype and add a suffix.
If your current "amazon" flavor does more (configure versionCode/Name/etc...), it won't work though. You could then use both your amazon flavor and a amzRelease build type. It'll create a lot more variants than you need but it'd work until we have some better.

You could use a variant of the solution I've written about here: https://stackoverflow.com/a/26585241/4177090
In short, you can find the combined variants using variantFilter and then update the appliciationId from there:
android.variantFilter { variant ->
def flavorString = ""
def flavors = variant.getFlavors()
for (int i = 0; i < flavors.size(); i++) {
flavorString += flavors[i].name;
}
if(flavorString.contains("amazon")) {
variant.getDefaultConfig().applicationId variant.getDefaultConfig().applicationId + ".amz"
}
}

Working solution for gradle 1.0.0+:
android.applicationVariants.all { variant ->
def flavorName = variant.getVariantData().getVariantConfiguration().getFlavorName()
def mergedFlavour = variant.getVariantData().getVariantConfiguration().getMergedFlavor();
if (flavorName.toLowerCase().contains("amazon")) {
mergedFlavour.setApplicationId(mergedFlavour.getApplicationId() + ".amz")
}
}

Related

Android studio flavor dimensions processed all, not selected one

I have a flavor dimensions in module build.gradle file, and gradle build process runs through all build variants whatever actual build variant is. Here is module build.gradle:
flavorDimensions 'type', 'jnitype'
productFlavors {
demo {
dimension 'type'
versionNameSuffix '.demo'
}
production {
dimension 'type'
versionNameSuffix '.production'
}
usejni {
dimension 'jnitype'
versionNameSuffix '.usejni'
copy {
from('../jnilib/data') {
include 'sdk_data.gpu'
....
}
into 'src/main/assets/data'
}
}
nojni {
dimension 'jnitype'
versionNameSuffix '.nojni'
delete('src/main/assets/data/*.*')
packagingOptions {
exclude 'lib/arm64-v8a/sdk.so'
...
}
}
}
So no matter what build variant selected, demoUsejni or demoNojni, gradle runs through 'usejni' and then 'nojni' variants - it copies files and libraries, and then deletes them. I used gradle debug to confirm this.
How can I tell gradle to use just a selected build flavor?
AS 3.5.2, gradle plugin 5.4.1, android build tools 3.5.2.
Finally, the solution is found: use gradle task name to check for required flavor. For an action at compile type gradle task name will be ":module_name:assembleFlavor1Flavor2...FlavornBuildtype". So i will check for "assemble" and "Nojni"/"Usejni".
String taskName = "";
if (gradle.startParameter.taskNames.size > 0)
taskName = gradle.startParameter.taskNames.get(0)
usejni {
dimension 'jnitype'
versionNameSuffix '.usejni'
if (taskName.contains("Usejni") && taskName.contains("assemble")) {
copy {
from('../jnilib/data') {
include 'sdk_data.gpu'
....
}
into 'src/main/assets/data'
}
}
}
nojni {
dimension 'jnitype'
versionNameSuffix '.nojni'
if (taskName.contains("Nojni") && taskName.contains("assemble")) {
delete('src/main/assets/data/*.*')
packagingOptions {
exclude 'lib/arm64-v8a/sdk.so'
...
}
}
}
Like a charm!
Hint for solution found in this answer: How to get current buildType in Android Gradle configuration

Gradle: android.variantFilter cause build fail after upgrade to Gradle 2.2.1

I am working in an Android project which has many flavors, I used this code in build.gradle to set the package name for each flavor:
flavorDimensions "type", "feature"
productFlavors {
abc { flavorDimension "type" }
def { flavorDimension "type" }
ABC { flavorDimension "feature" }
DEF { flavorDimension "feature" }
}
android.variantFilter { variant ->
def flavorString = ""
def flavors = variant.getFlavors()
for (int i = 0; i < flavors.size(); i++) {
flavorString += flavors[i].name;
}
if(flavorString.equalsIgnoreCase("abcABC")) {
variant.getDefaultConfig().applicationId "com.my.app.abc.abc"
}
if(flavorString.equalsIgnoreCase("abcDEF")) {
variant.getDefaultConfig().applicationId "com.my.app.abc.def"
}
if(flavorString.equalsIgnoreCase("defABC")) {
variant.getDefaultConfig().applicationId "com.my.app.def.abc"
}
if(flavorString.equalsIgnoreCase("defDEF")) {
variant.getDefaultConfig().applicationId "com.my.app.def.def"
}
}
Everything was fine until I updated my gradle from 1.10.0 to 2.2.1, the build is failed and I get this error message:
Error:(63, 0) No signature of method: com.android.build.gradle.internal.api.ReadOnlyProductFlavor.applicationId() is applicable for argument types: (java.lang.String) values: [com.hac.apps.megahd.acc]
Possible solutions: getApplicationId()
It seems like the variant.getDefaultConfig().applicationId is working different now. I searched the internet for the document for android.variantFilter but it seems not to be exist.
Anyone can tell me how do I get this code work in gradle 2.2.1? Thanks a lot.
The variantFilter is meant to be used only to allow or disallow builds of certain combinations. The defaultConfig object is shared and read-only. If you want to assign applicationId's to variants, I think you should something more similar to this:
buildTypes {
applicationVariants.all { variant ->
def projectFlavorNames = []
variant.productFlavors.each() { flavor ->
projectFlavorNames.add(flavor.name)
}
project.logger.debug('Application variant ' + variant.name + '. Flavor names list: ' + projectFlavorNames)
if (projectFlavorNames.contains('customer1') && projectFlavorNames.contains('variant1')) {
variant.mergedFlavor.applicationId = 'com.customer1.variant1'
} else if (projectFlavorNames.contains('customer2') && projectFlavorNames.contains('variant2')) {
variant.mergedFlavor.applicationId = 'com.customer2.variant2'
} // else use standard package name
project.logger.debug('Using project name: ' + variant.packageName)
}
// ...
}
From Dynamically generate package name for multi-flavors configuration

How to set different applicationId for each flavor combination using flavorDimensions?

I have and old android app that I am trying to migrate to the android gradle build system. The app is currently built in a multi project setup and published as four different apps (two different data sets included and free/paid versions for both datasets). I have managed to get away from the multi project setup by using flavorDimensions (previously called flavorGroups), but I can not figure out how to set a different applicationId for each flavor combination.
Since the app versions are already published, I need to keep the same applicationid as they currently have. Because of how my original package naming was done, I can not simply use flavor-buildtype combination with "packageNameSuffix" (which would have been a great option if it was an unpublished app).
https://stackoverflow.com/a/20956353/4177090 is answering how to use different source folders for flavor combinations, but not how (if even possible) to set specific configuration for each combination in the build file.
Gradle build file snippet:
flavorDimensions "price", "dataset"
productFlavors {
free { flavorDimension "price" }
paid { flavorDimension "price" }
dataset1 { flavorDimension "dataset" }
dataset2 { flavorDimension "dataset" }
}
I would like to have something like the following in my gradle build file (notice how unlogic my naming is, which is why I cannot use packageNameSuffix):
freeDataset1 { applicationId "com.beansys.freeappdataset1" }
freeDataset2 { applicationId "com.beansys.freedataset2" }
paidDataset1 { applicationId "com.beansys.dataset1paid" }
paidDataset2 { applicationId "com.beansys.mypaiddataset2" }
The solution proposed by Fredrik stopped working after upgrading Android Studio to 1.0.2 (and gradle plugin to 1.0.0) so I had to add following changes, current as of gradle plugin 1.3.1:
flavorDimensions "price", "dataset"
productFlavors {
free { dimension "price" }
paid { dimension "price" }
dataset1 { dimension "dataset" }
dataset2 { dimension "dataset" }
}
android.applicationVariants.all { variant ->
def mergedFlavor = variant.mergedFlavor
switch (variant.flavorName) {
case "freeDataset1":
mergedFlavor.setApplicationId("com.beansys.freeappdataset1")
break
case "freeDataset2":
mergedFlavor.setApplicationId("com.beansys.freedataset2")
break
case "paidDataset1":
mergedFlavor.setApplicationId("com.beansys.dataset1paid")
break
case "paidDataset2":
mergedFlavor.setApplicationId("com.beansys.mypaiddataset2")
break
}
}
I finally managed to solve this. I think the solution is elegant (although the actual code could most likely be written a lot nicer by someone with groovy knowledge).
Solution for setting a specific applicationId for each combined flavor:
flavorDimensions "price", "dataset"
productFlavors {
free { flavorDimension "price" }
paid { flavorDimension "price" }
dataset1 { flavorDimension "dataset" }
dataset2 { flavorDimension "dataset" }
}
android.variantFilter { variant ->
def flavorString = ""
def flavors = variant.getFlavors()
for (int i = 0; i < flavors.size(); i++) {
flavorString += flavors[i].name;
}
if(flavorString.equalsIgnoreCase("freeDataset1")) {
variant.getDefaultConfig().applicationId "com.beansys.freeappdataset1"
}
if(flavorString.equalsIgnoreCase("freeDataset2")) {
variant.getDefaultConfig().applicationId "com.beansys.freedataset2"
}
if(flavorString.equalsIgnoreCase("paidDataset1")) {
variant.getDefaultConfig().applicationId "com.beansys.dataset1paid"
}
if(flavorString.equalsIgnoreCase("paidDataset2")) {
variant.getDefaultConfig().applicationId "com.beansys.mypaiddataset2"
}
}
I had problems converting the answers given to the gradle kotlin dsl. I found a solution and shared it here: https://stackoverflow.com/a/60103604/2793394
With AGP 7.0, you should use onVariants:
androidComponents {
onVariants(selector().all(), { variant ->
switch (variant.name) {
case "freeDataset1":
variant.applicationId = "com.beansys.freeappdataset1"
break
case "freeDataset2":
variant.setApplicationId = "com.beansys.freedataset2"
break
...
...
...
}
})
}
You can even pass your custom "selector" and do something for example for only a specific variant:
androidComponents {
onVariants(selector().withName("paidDataset1"), { variant ->
variant.applicationId = "com.beansys.dataset1paid"
})
}

Exclude specific build variants

I have the two default build types: debug / release and a couple of flavors: prod / dev.
Now I want to exclude the build variant dev-release, but keep all other possible combinations. Is there a way to achieve this?
Variant filter
Use the variantFilter of the gradle android plugin to mark certain combinations as ignored. Here is an example from the official documentation that works with flavor dimensions and shows how it can be used:
android {
...
buildTypes {...}
flavorDimensions "api", "mode"
productFlavors {
demo {...}
full {...}
minApi24 {...}
minApi23 {...}
minApi21 {...}
}
variantFilter { variant ->
def names = variant.flavors*.name
// To check for a certain build type, use variant.buildType.name == "<buildType>"
if (names.contains("minApi21") && names.contains("demo")) {
// Gradle ignores any variants that satisfy the conditions above.
setIgnore(true)
}
}
}
As the comment says, you can also check the buildType like so:
android {
variantFilter { variant ->
def names = variant.flavors*.name
if(variant.buildType.name == 'release' && names.contains("myforbiddenflavor")) {
setIgnore(true)
}
}
}
Using variant filters like others I found it was easiest to do this by comparing the variant name against a list of variants that I want to keep.
So in my app/build.gradle file I have something like:
android {
variantFilter { variant ->
def needed = variant.name in [
'stagingQuickDebug', // for development
'stagingFullDebug', // for debugging all configurations
'stagingFullCandidate', // for local builds before beta release
'stagingFullRelease', // for beta releases
'productionFullCandidate', // for local builds before going public
'productionFullRelease' // for public releases
]
variant.setIgnore(!needed)
}
buildTypes {
debug {
}
release {
}
candidate.initWith(release)
}
flavorDimensions "server", "build"
productFlavors {
staging {
dimension "server"
buildConfigField "String", "API_URL", '"https://example-preprod.com/"'
}
production {
dimension "server"
buildConfigField "String", "API_URL", '"https://example.com/"'
}
quick {
dimension "build"
minSdkVersion 21
resConfigs("en", "xxhdpi")
}
full {
dimension "build"
}
}
}
When working with flavor dimensions try this one
variantFilter { variant ->
def dim = variant.flavors.collectEntries {
[(it.productFlavor.dimension): it.productFlavor.name]
}
if (dim.dimensionOne == 'paid' && dim.dimensionSecond == 'someVal') {
variant.setIgnore(true);
}
}
If you use flavor dimensions do this:
flavorDimensions "device", "server"
productFlavors {
emulator {
dimension = "device"
}
phone {
dimension = "device"
}
staging {
dimension = "server"
}
production {
dimension = "server"
}
}
android.variantFilter { variant ->
def device = variant.getFlavors().get(0).name
def server = variant.getFlavors().get(1).name
def isRelease = variant.buildType.name.equals('release')
def isDebug = variant.buildType.name.equals('debug')
// Disable emulatorProductionRelease build variant
if (device.equals('emulator') && server.equals('production') && isRelease) {
variant.setIgnore(true)
}
}
It's easy to read and you can target specific build variants.
The solutions here didn't work for me - I run into this post and added this to build.gradle in my app and it solved the issue for me
gradle.taskGraph.whenReady { graph ->
graph.allTasks.findAll { it.name ==~ /.*MyVariant.*/ }*.enabled = false
}
This is what it does - waits for gradle to assemble the complete list of tasks to execute and then it marks all the tasks that match the name pattern as disabled
NOTE
The match is exact - the expression above lets you match any task that has "MyVariant" somewhere in it's name and it is case sensitive
One more simpler way
android.variantFilter { variant ->
if (variant.name == "qaDebug" || variant.name == "devRelease") {
setIgnore(true)
}
}
Or if you place this code inside android {} closure, android. can be omitted
android {
// Please always specify the reason for such filtering
variantFilter { variant ->
if (variant.name == "qaDebug" || variant.name == "devRelease") {
setIgnore(true)
}
}
}
Please always put a meaningful comment for things like this.
UPD: For Kotlin Gradle DSL there is another way:
android {
variantFilter {
ignore = listOf("qaDebug", "devRelease").contains(name)
}
}
The answer of #ade.se didn't work for me. But I've struggled a little, and written this, that works great:
android {
compileSdkVersion 22
buildToolsVersion '20.0.0'
variantFilter { variant ->
if (variant.buildType.name.equals('debug') || variant.buildType.name.equals('release')) {
variant.setIgnore(true);
}
}
defaultConfig {
applicationId "com.fewlaps.quitnow"
minSdkVersion 15
targetSdkVersion 22
versionCode 35
versionName "1.35"
}
The code you have to add is the variantFilter one, but I've pasted a little of the context to make it easy to understand.
See Variant filter answer above.
Old Answer:
It's not possible at the moment, but it's something we want to add. Probably soon.
In the meantime you could disable the assemble task I think. Something like this:
android.applicationVariants.all { variant ->
if ("devRelease".equals(variant.name)) {
variant.assembleTask.enabled = false
}
}
In Gradle's Kotlin DSL (i.e. build.gradle.kts), that would be:
variantFilter {
ignore = buildType.name == "release" &&
flavors.map { it.name }.contains("dev")
}

Letting multiple flavorGroups influence packageName in Android gradle build

I have a build configuration with 2 build types (debug/release) and 2 flavorGroups (locale/environment).
These are 3 different axes, and I want to determine the packageName of the build variant by all of them.
However, it seems I can only set the full packageName for a given flavor, and then have a packageNameSuffix for the build type - attempting to do a packageNameSuffix for a flavour leads to an Could not find method packageNameSuffix() for arguments [...] error.
Any way around that, so that I could get a package name for each of the resulting build variants, along the lines of: com.app.LOCALE.ENVIRONMENT.TYPE, without having to "unroll" one of the axes into build types (which would lead to duplication)?
Thanks in advance.
You could use the solution I've written about here: https://stackoverflow.com/a/26585241/4177090
In short, you can find out the combined variant using variantFilter and then update the configuration (e.g. the appliciationId) from there:
android.variantFilter { variant ->
def flavorString = ""
def flavors = variant.getFlavors()
for (int i = 0; i < flavors.size(); i++) {
flavorString += flavors[i].name;
}
if(flavorString.equalsIgnoreCase("fooBar")) {
variant.getDefaultConfig().applicationId "com.example.foobar"
}
}
You could use a combination of packageName and packageNameSuffix from productFlavors and buildTypes like the following:
android {
productFlavors {
foo {
packageName "com.example.foo"
versionName "Foo"
}
bar {
packageName "com.example.bar"
versionName "Bar"
}
}
buildTypes {
debug {
packageNameSuffix ".debug"
versionNameSuffix "-debug"
}
release {
packageNameSuffix ".release"
versionNameSuffix "-release"
}
}
}
But if you're using something like flavorGroups, then this may not work for you.
You can also check out this code sample config which adds new buildTypes https://github.com/bradmcmanus/Gradle-Build-Example/blob/master/GradleBuildExample/build.gradle

Categories

Resources