We have three environments for our app - DEV, QA and PROD.
Now we have created three AndroidManifest files for those three envs(like AndroidManifest_QA.xml), and we have to change the file name EVERY TIME when we want to release a new version, which is not unfailing.
So, I am wondering if there is an easier way to handle this situation?
Thanks in advance.
I would go for Gradle Flavours:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
or you can make it more separated, by using build variants. I think this is what you are really looking for in your case:
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
a very good tutorial:
http://www.techotopia.com/index.php/An_Android_Studio_Gradle_Build_Variants_Example
article:
http://developer.android.com/tools/building/configuring-gradle.html
Let's consider the following example from the techotopia.com
productFlavors {
phone {
applicationId
"com.ebookfrenzy.buildexample.app.phone"
versionName "1.0-phone"
}
tablet {
applicationId
"com.ebookfrenzy.buildexample.app.tablet"
versionName "1.0-tablet"
}
}
if you need to separate the falvours on code level, than please see Xavier Ducrohet stackoverflow link below:
Using Build Flavors - Structuring source folders and build.gradle correctly
Related
Is it possible to change the package name of an Android application using Gradle?
I need to compile two copies of the same app, having a unique package name (so I can publish to the market twice).
As a simpler alternative to using product flavours as in Ethan's answer, you can also customise build types.
How to choose between the approaches:
If you need different package names to be able to have both debug and release apks installed on a device, then use the build type approach below, as Gradle plugin docs agree. In this case flavours are an overkill. (I think all projects should by default do this, as it will make life easier especially after you've published to the store and are developing new features.)
There are valid uses for product flavours, the typical example being an app with free and paid versions. In such case, check Ethan's answer and read the documentation too: Configuring Gradle Builds and Gradle Plugin User Guide.
(You can also combine the two approaches, which results in every build variant having distinct package name.)
Build type configuration
For debug build type, and all other non-release types, define applicationIdSuffix which will be added to the default package name.
(Prior to Android Gradle plugin version 0.11 this setting was known as packageNameSuffix.)
android {
buildTypes {
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
}
beta {
applicationIdSuffix '.beta'
versionNameSuffix '-BETA'
// NB: If you want to use the default debug key for a (non-debug)
// build type, you need to specify it:
signingConfig signingConfigs.debug
}
release {
// signingConfig signingConfigs.release
// runProguard true
// ...
}
}
}
Above, debug and release are default build types whose some aspects are configured, while beta is a completely custom build type. To build the different types, use assembleDebug, assembleBeta, etc, as usual.
Similarly, you can use versionNameSuffix to override the default version name from AndroidManifest (which I find very useful!). E.g. "0.8" → "0.8-BETA", as configured above.
Resources:
This example is straight from Xavier Ducrohet's "Google I/O 2013: The New Android SDK Build System" presentation.
Build Types in the User Guide.
Myself I've been using productFlavors so far for this exact purpose, but it seems build type customisation may be closer to my needs, plus it keeps the build config simpler.
Update (2016): I've since used this approach in all my projects, and I think it definitely is the way to go. I also got it included in Android Best Practices guide by Futurice.
You could so something like this
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
flavorDimensions "flavor1", "flavor2"
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}
You can also change the field android.defaultConfig.applicationId if you want to do one-off builds.
Taken from: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-Flavor-Configuration
With the gradle plugin version of 1.0.0+ you have to use applicationId as stated in the migration guide
Renamed Properties in ProductFlavors
packageName => applicationId
Thus in your build.gradle you would now use:
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
}
flavor2 {
applicationId "com.example.flavor2"
}
}
From Ethan's answer, both flavorGroups and packageName both are not available anymore. Below works as of March 2015.
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
flavorDimensions "flavor"
productFlavors {
flavor1 {
flavorDimension "flavor"
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
flavorDimension "flavor"
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}
I did not want to use Flavors, so I found a way to do so with buildTypes. I did this by changing my app/build.gradle file as follows:
defaultConfig {
applicationId "com" // See buildTypes.type.applicationIdSuffix
...
}
...
buildTypes {
debug {
applicationIdSuffix ".domain.name.debug"
...
}
releaseStaging {
applicationIdSuffix ".compagny.staging"
...
}
release {
applicationIdSuffix ".domain.name"
...
}
}
This allows me to have 3 apps next to each other on my devices.
I hope this helps others.
Below are the buildTypes and flavors parts of my build.gradle:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
}
}
flavorDimensions "default"
productFlavors {
free {
android.sourceSets.free.setRoot('src/main')
dimension "default"
}
plus {
applicationIdSuffix '.plus'
versionName '1.0'
android.sourceSets.plus.setRoot('src/plus')
dimension "default"
}
}
Android Studio only shows two build variants (freeDebug and freeRelease) in the Build variants window. It does not show plusDebug or plusRelease. I have another project with a similar build.gradle and I can clearly see four build variants. Any ideas where I should look?
plus is a default method in groovy. It's not a bug in Android Studio or anything else. You are executing this function in DefaultGroovyMethods
public static <T> Set<T> plus(Set<T> left, T right) {
return (Set)plus((Collection)left, (Object)right);
}
This is because the delegate passed into productFlavors implements Set.
See productFlavors definition
This appears to be a bug/limitation in Gradle. I have filed an issue for it.
Use something else, other than plus. I tried quoting it ("plus"), thinking that perhaps it's a conflict with a keyword, but that had no effect. But Plus and plussss and phat all work.
I am trying to configure some SDK inside application and I am facing this gradle problem, that just gives me a headache. What I have to do is set below value
manifestPlaceholders = [manifestApplicationId: "$applicationId"]
to be something like com.flavorA.debug for debug builds and honestly I have no idea how am I supposed to set this. If I put this section to defaultConfig then I get null inside my manifest file. If I put it to productFlavors section then I only get com.flavorA, so I'm missing buildType suffix.
I was also trying different things like
applicationVariants.each{ variant->
manifestPlaceholders = [manifestApplicationId: "test"]
}
but above doesn't even change the name to test, not mentioning other things.
How am I supposed to solve this with gradle?
You don't need manifestPlaceHolders or the applicationVariants block to achieve this. The applicationId and the package name in the manifest is decoupled, so you simply write:
productFlavors {
pro {
applicationId = "com.example.my.pkg.pro"
}
free {
applicationId = "com.example.my.pkg.free"
}
}
and if you want to change it for the build type:
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
More details here:
applicationid-vs-packagename
I have used build.gradle(app) to create different flavors of apk.
But installing different flavors of same apk overrides the previous one.
I want to create different apks to run on same device simultaneously.
I want to create different apk with different appicon which can be installed on same device and run simultaneously.
Any link or tutorial or direct help is appreciated.
Thanks in advance.
Change the PackageName of the flavor
Sample Gradle File
apply plugin: 'com.android.application'
android {
lintOptions {
abortOnError false
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
buildTypes {
debug {
minifyEnabled false
zipAlignEnabled true
}
release {
minifyEnabled true
zipAlignEnabled true
}
}
productFlavors {
Flavor1 {
applicationId "com.falvor.one" //This is where you change the package name
}
Flavor2 {
applicationId "com.falvor.two"
}
}
}
Flavor Hierarchy in Android
- src/main/java
- src/flavor1
--------------Java
----------------Your java files
--------------res
----------------Drawable
src/flavor2/java
For more understanding, follow this link
You need to create new productFlavors in your gradle file, like this;
productFlavors {
Flavor1 {
applicationId 'com.project.fl1'
signingConfig signingConfigs.xx
versionCode 1
}
Flavor2 {
applicationId 'com.project.fl2'
signingConfig signingConfigs.xx
versionCode 1
}
Flavor3 {
applicationId 'com.project.fl3'
signingConfig signingConfigs.xx
versionCode 1
}
}
The important thing here is to give each one a unique applicationId, they can then be installed on the same phone.
This post explains exactly how to achieve what you want step by step.
Most importantly:
add the product flavours container to the app build.gradle file
productFlavors {
free {
applicationId "antoniocappiello.com.buildvariantsexample.free"
}
paid {
applicationId "antoniocappiello.com.buildvariantsexample.paid"
}
}
create inside src a directory with the exact name of the product flavour that you want to look different from the main variant, for example with the configuration at step 1 the directory name could be paid or free . And inside that directory create the subfolder res/drawable where you are going to place your new app launcher icon.
Directory structure example
Is it possible to change the package name of an Android application using Gradle?
I need to compile two copies of the same app, having a unique package name (so I can publish to the market twice).
As a simpler alternative to using product flavours as in Ethan's answer, you can also customise build types.
How to choose between the approaches:
If you need different package names to be able to have both debug and release apks installed on a device, then use the build type approach below, as Gradle plugin docs agree. In this case flavours are an overkill. (I think all projects should by default do this, as it will make life easier especially after you've published to the store and are developing new features.)
There are valid uses for product flavours, the typical example being an app with free and paid versions. In such case, check Ethan's answer and read the documentation too: Configuring Gradle Builds and Gradle Plugin User Guide.
(You can also combine the two approaches, which results in every build variant having distinct package name.)
Build type configuration
For debug build type, and all other non-release types, define applicationIdSuffix which will be added to the default package name.
(Prior to Android Gradle plugin version 0.11 this setting was known as packageNameSuffix.)
android {
buildTypes {
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
}
beta {
applicationIdSuffix '.beta'
versionNameSuffix '-BETA'
// NB: If you want to use the default debug key for a (non-debug)
// build type, you need to specify it:
signingConfig signingConfigs.debug
}
release {
// signingConfig signingConfigs.release
// runProguard true
// ...
}
}
}
Above, debug and release are default build types whose some aspects are configured, while beta is a completely custom build type. To build the different types, use assembleDebug, assembleBeta, etc, as usual.
Similarly, you can use versionNameSuffix to override the default version name from AndroidManifest (which I find very useful!). E.g. "0.8" → "0.8-BETA", as configured above.
Resources:
This example is straight from Xavier Ducrohet's "Google I/O 2013: The New Android SDK Build System" presentation.
Build Types in the User Guide.
Myself I've been using productFlavors so far for this exact purpose, but it seems build type customisation may be closer to my needs, plus it keeps the build config simpler.
Update (2016): I've since used this approach in all my projects, and I think it definitely is the way to go. I also got it included in Android Best Practices guide by Futurice.
You could so something like this
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
flavorDimensions "flavor1", "flavor2"
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}
You can also change the field android.defaultConfig.applicationId if you want to do one-off builds.
Taken from: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-Flavor-Configuration
With the gradle plugin version of 1.0.0+ you have to use applicationId as stated in the migration guide
Renamed Properties in ProductFlavors
packageName => applicationId
Thus in your build.gradle you would now use:
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
}
flavor2 {
applicationId "com.example.flavor2"
}
}
From Ethan's answer, both flavorGroups and packageName both are not available anymore. Below works as of March 2015.
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
flavorDimensions "flavor"
productFlavors {
flavor1 {
flavorDimension "flavor"
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
flavorDimension "flavor"
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}
I did not want to use Flavors, so I found a way to do so with buildTypes. I did this by changing my app/build.gradle file as follows:
defaultConfig {
applicationId "com" // See buildTypes.type.applicationIdSuffix
...
}
...
buildTypes {
debug {
applicationIdSuffix ".domain.name.debug"
...
}
releaseStaging {
applicationIdSuffix ".compagny.staging"
...
}
release {
applicationIdSuffix ".domain.name"
...
}
}
This allows me to have 3 apps next to each other on my devices.
I hope this helps others.