Search code examples
androidgradleandroid-gradle-pluginbuild.gradleandroid-productflavors

How to define android:label for different productFlavors in build.gradle


Is there a way to specify different app label (name) for different productFlavors in build.gradle? For example something like this:

productFlavors {
    stage {
        app name with "-stage" suffix
    }
    preprod {
        app name with "-preprod" suffix
    }
    prod {
        app name without any  suffix
    }
}

Solution

  • A friend of mine suggested a very nice solution based on injecting build variables into the manifest. It even allows to use localized app labels. So here it is:

    1) Specify android:label in AndroidManifest.xml as follows:

    android:label="${appLabel}"

    2) Specify a default value in app level build.gradle:

    manifestPlaceholders = [appLabel:"@string/appName"]

    3) Override the value for needed product flavours:

    productFlavors {
        stage {
            manifestPlaceholders = [appLabel:"@string/appNameStage"]
        }
        preprod {
            manifestPlaceholders = [appLabel:"@string/appNamePreprod"]
        }
        prod {
            // Just let it use a default value
        }
    }
    

    4) Add string resources which you are referring to (appName, appNameStage, appNamePreprod). Localize them if needed.