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.
If by "app name", you mean android:label
on <application>
, the simplest solution is to have that point at a string resource (eg, 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/
.
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).
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
.
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>
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
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>
对于更动态的基于 gradle 的解决方案(例如,在 main 的strings.xml
设置一个基本应用程序名称,并避免在每个风格/构建类型组合的strings.xml
重复自己),请参阅我的答案: https : //stackoverflow.com/ a/32220436/1128600
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
.
There are multiple ways you can do.
you can create manifestPlaceholders
OR resValue
in app level build.gradle
. eg
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
. eg
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
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"
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.