Home » Android » android – How to change app name per Gradle build type

android – How to change app name per Gradle build type

Posted by: admin March 11, 2020 Leave a comment

Questions:

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.

How to&Answers:

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/.

Answer:

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).

Answer:

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.

Answer:

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>

Answer:

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

Answer:

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>

Answer:

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

Answer:

You can use strings.xml in different folders, see 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.

Answer:

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"
        }
    }
}