I have 2 build flavors, say, flavor1 and flavor2.
I would like my application to be named, say, \"AppFlavor1\" when I build for flavor1 an
First of all, answer this question: "Can the user install both flavors of your application on the same device?"
I use a Python script that patches the source. It contains some reusable functions and, of course, knowledge what needs be patched in this particular project. So the script is application-specific.
There is a lot of patching, the data for patching are kept in a Python dictionary (including application package names, they BTW are different from the Java package name), one dictionary per flavor.
As to l10n, strings may point to other strings, e.g. in my code I have:
<string name="app_name">@string/x_app_name_xyz</string>
<string name="x_app_name_default">My Application</string>
<string name="x_app_name_xyz">My App</string>
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
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.