You are here: Home . Android, Code, Featured Code . How to customize Android product flavour app name, strings, styles and the manifest

How to customize Android product flavour app name, strings, styles and the manifest

Android development has a powerful feature that allows you to modify an Android application and make it possible for it to be customized and deployed using a different look and functionality. Some examples where this might be useful:

  • White-labelling: This is where you have an application that you customise for each customer. For example, you have an eCommerce app but have different customers who would like to have their own version of that app. Also, you could have a ride-sharing app that you customise for your various customers
  • More commonly, you have a free version of your app but you’d like to create a paid version with more features

Just to re-iterate, for you who already has an existing app, depending on your strategy, Android flavours give you the option of increasing your income streams by customising the same app, and making it available for use by other customers.

That said, while this tutorial gives a really good introduction to what flavours are and how to get started with them, here, I’ll focus on the specifics of how to actually go about customising them in what I feel is a robust way.

Google’s documentation gives instructions on how to modify various aspects of the different flavours but I’ll get into specifically how to customise the following:

  1. The AndroidManifest.xml
  2. Specific Strings [Instead of feeling pressed to have to customise the entire strings.xml file]
  3. Styles

I’ll focus on the scenario where you’d like to customise just a subset – such as a subset of the strings you have in strings.xml – as opposed to the entire strings.xml file. One disadvantage of copying an entire AndroidManifest.xml or strings.xml is the repetition it introduces; if you need to change something, you might need to change it in multiple files. One of the principles of software engineering is Do Not Repeat Yourself (DRY). Let’s see how to do that.

Android has something called manifest placeholders, a pretty cool feature that will allow you to modify AndroidManifest.xml without needing to re-write the entire file. Here’s an example where we modify the app name per flavour.

Customize the AndroidManifest.xml per flavour

Change this in AndroidManifest.xml:

    <application
        android:label="@string/appLabel"

to:

<application
    android:label="${appLabel}"
    tools:replace="android:label"

${appLabel} is a manifest placeholder. Then, in your .build.gradle file, in the android section, modify the defaultConfig section to define the appLabel placeholder.

defaultConfig {
        manifestPlaceholders = [appLabel:"@string/app_name"]

In your flavours, you can define the manifest placeholder as well:

Specific strings

  flavorDimensions 'default'
    productFlavors {
        shopTillYouDrop {
            dimension = 'default'
            versionNameSuffix = '-styd'
            manifestPlaceholders = [appLabel:"@string/app_name_styd"]

This way, you can define app_name and app_name_styd in strings.xml and be able to translate it into various languages if need be. You can use the same technique to customise other aspects of the AndroidManifest.xml. Here’s how to use that technique to specify different themes.

 
    defaultConfig {
        manifestPlaceholders = [appLabel:"@string/app_name", appTheme:"@style/ShoppingTheme"]
}
appTheme:"@style/KanzuBankingTheme"]
 flavorDimensions 'default'
    productFlavors {
        shopTillYouDrop {
            dimension = 'default'
            versionNameSuffix = '-styd'
            manifestPlaceholders = [appLabel:"@string/app_name", appTheme:"@style/ShopTillYouDropAppTheme"]

You can see that with this technique, you won’t need multiple AndroidManifest.xml files.

Customize specific strings per flavour

Sometimes you need to have just a few strings in the entire strings.xml changed in your flavour. Android scans the res folder and picks strings from the files present. Because of this, move every string you’d like to customise into a different file e.g. strings_flavours.xml. This technique is mainly useful if you don’t think you’ll modify very many strings in strings.xml. Create strings_flavours.xml in src/main/res/values/. Make sure you remove all these strings from strings.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="flavour_string_one">String Value</string>
    <string name="flavour_string_two">String value two</string>
</resources>

Now, you can simply create strings_flavours.xml in your flavour directories .e.g src/shopTillYouDrop/res/values and update it.

Customize styles

Lastly, you can create custom themes for each flavour. As I pointed out while customising AndroidManifest.xml, you can change the theme per flavour using the technique I showed above. Now, knowing that you have specified a different theme, all you need to do is create the new theme, make the default theme its parent theme then go ahead and customise any aspect of the custom theme you’ve created.

<resources>

    <!-- Shop till you drop Product Flavour custom theme. -->
    <style name="ShopTillYouDropAppTheme" parent="DefaultAppTheme">
        <item name="android:fontFamily">@font/opensans</item>
        <item name="buttonStyle">@style/ShopTillYouDroAppTheme.Button</item>
    </style>

    <!-- Base application theme. -->
    <style name="DefaultAppTheme" parent="MaterialAppTheme">
        <!-- Customize your theme here. -->
    </style>

Using diligent minions, I bring these tutorials & tips to your inbox every week. Give a minion work today; add your email address here