Different app names for different build flavors?

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.

Tags:

Android

Gradle