Different app names for different build flavors?
I have 2 build flavors, say, flavor1 and flavor2.
I would like my application to be named, say, "AppFlavor1" when I build for flavor1 and "AppFlavor2" when I build for flavor 2.
It is not the title of activities I want to change. I want to change the app name as it's displayed on the phone menu and elsewhere.
From build.gradle
I can set various parameters for my flavors but, it seems, not the app label. And I can not change the app label programmatically based on some variable, too.
So, how do people handle this?
Remove app_name
from strings.xml
(else gradle will complain of duplicate resources). Then modify build file like this:
productFlavors {
flavor1{
resValue "string", "app_name", "AppNameFlavor1"
}
flavor2{
resValue "string", "app_name", "AppNameFlavor2"
}
}
Also make sure @string/app_name
value is assigned for android:label
attribute in the manifest.
<application
...
android:label="@string/app_name"
...
This is less disruptive than creating new strings.xml
under different built sets or writing custom scripts.
Instead of changing your main strings.xml with a script and risk messing up your source control, why not rely on the standard merging behavior of the Android Gradle build?
My build.gradle
contains
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
release {
res.srcDir 'variants/release/res'
}
debug {
res.srcDir 'variants/debug/res'
}
}
So now I can define my app_name
string in the variants/[release|debug]/res/strings.xml
. And anything else I want to change, too!
If you want to maintain localization for the app name in different flavors, then you can achieve it as follows:
1) Specify android:label
in <application>
available in AndroidManifest.xml
as follows:
<application
...
android:label="${appLabel}"
...
>
2) Specify default value fo appLabel
in app level build.gradle
:
manifestPlaceholders = [appLabel:"@string/defaultName"]
3) Override the value for product flavors as follows:
productFlavors {
AppFlavor1 {
manifestPlaceholders = [appLabel:"@string/flavor1"]
}
AppFlavor2 {
manifestPlaceholders = [appLabel:"@string/flavor2"]
}
}
4) Add string resources for each of Strings (defaultName, flavor1, flavor2) in your strings.xml
. This will allow you to localize them.
You can add a strings resource file to each flavor, then use those resource files to change your app name.
For example in one of my apps, I have a free and paid version. To rename them "Lite" and "Pro", I created a meta_data.xml
file, and added my app_name
value to that XML and removed it from strings.xml
.
Next, in app/src
create a folder for each flavor (see below for example structure). Inside these directories, add res/values/<string resource file name>
. Now, when you build, this file will be copied into your build and your app will be renamed.
File structure:
app/src
/pro/res/values/meta_data.xml
/lite/res/values/meta_data.xml