How to change app name per Gradle build type

AndroidAndroid Gradle-Plugin

Android Problem Overview


I am trying to figure out a way to be able to change my application's app name per build type in gradle.

For instance, I would like the debug version to have <APP_NAME>-debug and the qa version to have <APP-NAME>-QA.

I am familiar with:

debug {
        applicationIdSuffix '.debug'
        versionNameSuffix '-DEBUG'
}

However, I can't seem to find a gradle command to apply the change of the app when in the launcher.

Android Solutions


Solution 1 - Android

If by "app name", you mean android:label on <application>, the simplest solution is to have that point at a string resource (e.g., android:label="@string/app_name"), then have a different version of that string resource in a src/debug/ sourceset.

You can see that in this sample project, where I have a replacement for app_name in src/debug/res/values/strings.xml, which will be applied for debug builds. release builds will use the version of app_name in src/main/.

Solution 2 - Android

You can use something like this

 buildTypes {
    debug {
        applicationIdSuffix '.debug'
        versionNameSuffix '-DEBUG'
        resValue "string", "app_name", "AppName debug"
    }
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
        zipAlignEnabled true
        resValue "string", "app_name", "AppName"
    }
}

You can use @string/app_name in AndroidManifest.xml files.

Make sure you remove app_name from values/ folder (no entry by this name).

Solution 3 - Android

You can do this with gradle:

android {
    buildTypes {
        release {
            manifestPlaceholders = [appName: "My Standard App Name"]
        }
        debug {
            manifestPlaceholders = [appName: "Debug"]
        }
    }
}

Then in your AndroidManifest.xml put:

<application
    android:label="${appName}"/>
    <activity
        android:label="${appName}">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Note: it also works with productFlavors.

Solution 4 - Android

To support translations make this:

1. remove string "app_name"

2. add to gradle

 buildTypes {
    admin {
       resValue "string", "app_name", "@string/app_name_admin"
    }
    release {
        resValue "string", "app_name", "@string/app_name_release"
    }
    debug {
        resValue "string", "app_name", "@string/app_name_debug"
    }
}

3. Set app name in Manifest as "@string/app_name"

4. Add to strings.xml values

<string name="app_name_admin">App Admin</string>
<string name="app_name_release">App  release</string>
<string name="app_name_debug">App debug</string>

Solution 5 - Android

The app name is user-visible, and that's why Google encourages you to keep it in your strings.xml file. You can define a separate string resource file that contains strings that are specific to your buildTypes. It sounds like you might have a custom qa buildType. If that's not true, ignore the qa part below.

└── src
    ├── debug
    │   └── res
    │       └── buildtype_strings.xml
    ├── release
    │   └── res
    │       └── buildtype_strings.xml
    └── qa
        └── res
            └── buildtype_strings.xml

Solution 6 - Android

We need a solution to support app name with localization (for multi language). I have tested with @Nick Unuchek solution, but building is failed (not found @string/) . a little bit change to fix this bug: build.gradle file:

android {
    ext{
        APP_NAME = "@string/app_name_default"
        APP_NAME_DEV = "@string/app_name_dev"
    }

    productFlavors{

        prod{
            manifestPlaceholders = [ applicationLabel: APP_NAME]
        }

        dev{
            manifestPlaceholders = [ applicationLabel: APP_NAME_DEV ]
        }

    }

values\strings.xml:

<resources>
    <string name="app_name_default">AAA prod</string>
    <string name="app_name_dev">AAA dev</string>

</resources>

values-en\strings.xml:

<resources>
    <string name="app_name_default">AAA prod en</string>
    <string name="app_name_dev">AAA dev en</string>

</resources>

Manifest.xml:

<application
    android:label="${applicationLabel}" >
</application>

Solution 7 - Android

For a more dynamic gradle based solution (e.g. set a base Application name in main's strings.xml once, and avoid repeating yourself in each flavor / build type combination's strings.xml), see my answer here: https://stackoverflow.com/a/32220436/1128600

Solution 8 - Android

You can use strings.xml in different folders, see https://stackoverflow.com/questions/36039848/android-separate-string-values-for-release-and-debug-builds.

So, create this file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Your app name</string>
</resources>

Then paste it to app\src\debug\res\values\ and app\src\release\res\values\ folders. Replace "Your app name" in debug and release files. Remove app_name item from strings.xml in app\src\main\res\values\ folder.

In AndroidManifest you will have the same

<application
    android:label="@string/app_name"
    ...

No changes at all. Even if you added a library with it's AndroidManifest file and strings.xml.

Solution 9 - Android

There are multiple ways you can do.

you can create manifestPlaceholders OR resValue in app level build.gradle. e.g.

buildTypes {
     release {
          ...
          manifestPlaceholders = [appLabel: "My App"]
          //resValue "string", "appLabel", '"My App"'
     }
     debug {
          ...
          manifestPlaceholders = [appLabel: "My App - Debug"]
          //resValue "string", "appLabel", '"My App - Debug"'
     }
}

OR

If you have productFlavors, you can create there

flavorDimensions "env"
productFlavors {
    dev {
        dimension "env"
        ...
        manifestPlaceholders = [appLabel: "My App - Development"]
        //resValue "string", "appLabel", '"My App - Development"'
    }
    prod {
        dimension "env"
        ...
        manifestPlaceholders = [appLabel: "My Awesome App"]
        //resValue "string", "appLabel", '"My Awesome App"'
    }
}

Then in AndroidManifest.xml if you are using manifestPlaceholders, just change android:label="${appLabel}" as below OR if you are using resValue, just change android:label=@string/appLabel

<application
    ...
    android:label="${appLabel}"> //OR `android:label=@string/appLabel`
    
    <activity
        ...
        android:label="${appLable}">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

NOTE: Make sure to change android:lable as well in <activity> of LAUNCHER category. If it doesn't require to use android:label in <activity>, just remove this.


If you do not want to add in build.gradle directly, you can add in values/string.xml of selected ProductFlavors. e.g.

Add

<string name="appLabel">My App - Development</string> 

in app/src/dev/res/values/string.xml

and

<string name="appLabel">My Awesome App</string> 

in app/src/prod/res/values/string.xml

Solution 10 - Android

As author asks to do this in Gradle, we can assume he want to do it in the script and not in the configuration files. Since both Android Studio and Gradle has been heavily updated and modified in the last year (~2018) all other answers above, seem overly contorted. The easy-peasy way, is to add the following to your app/build.gradle:

android {
    ...
    buildTypes {
        ...
        // Rename/Set default APK name prefix (app*.apk --> AwesomeApp*.apk)
        android.applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def appName = "AwesomeApp"
                outputFileName = appName+"-${output.baseName}-${variant.versionName}.apk"
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndre PerkinsView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidirscompView Answer on Stackoverflow
Solution 3 - AndroidAlbert Vila CalvoView Answer on Stackoverflow
Solution 4 - AndroidNickUnuchekView Answer on Stackoverflow
Solution 5 - AndroidKrylezView Answer on Stackoverflow
Solution 6 - AndroidHungNM2View Answer on Stackoverflow
Solution 7 - AndroidSteffen FunkeView Answer on Stackoverflow
Solution 8 - AndroidCoolMindView Answer on Stackoverflow
Solution 9 - AndroidAmir RazaView Answer on Stackoverflow
Solution 10 - Androidnot2qubitView Answer on Stackoverflow