Android 如何更改每个 Gradle 构建类型的应用程序名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24785270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change app name per Gradle build type
提问by Andre Perkins
I am trying to figure out a way to be able to change my application's app name per build type in gradle.
我试图找出一种方法,能够在 gradle 中根据构建类型更改我的应用程序的应用程序名称。
For instance, I would like the debug version to have <APP_NAME>-debug
and the qa version to have <APP-NAME>-QA
.
例如,我希望调试版本具有<APP_NAME>-debug
,而 qa 版本具有<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.
但是,我似乎无法在启动器中找到一个 gradle 命令来应用应用程序的更改。
回答by CommonsWare
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.
如果“应用程序名称”是指android:label
on <application>
,那么最简单的解决方案是在字符串资源(例如android:label="@string/app_name"
)上设置该点,然后在源集中使用该字符串资源的不同版本src/debug/
。
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/
.
您可以在这个示例项目中看到,我在其中替换了app_name
in src/debug/res/values/strings.xml
,它将应用于debug
构建。release
builds 将使用app_name
in的版本src/main/
。
回答by irscomp
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_namein AndroidManifest.xml files.
您可以在 AndroidManifest.xml 文件中使用@string/app_name。
Make sure you remove app_namefrom values/ folder (no entry by this name).
确保从 values/ 文件夹中删除app_name(没有这个名称的条目)。
回答by Albert Vila Calvo
You can do this with gradle:
你可以用 gradle 做到这一点:
android {
buildTypes {
release {
manifestPlaceholders = [appName: "My Standard App Name"]
}
debug {
manifestPlaceholders = [appName: "Debug"]
}
}
}
Then in your AndroidManifest.xml
put:
然后在你的AndroidManifest.xml
投入:
<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
.
注意:它也适用于productFlavors
.
回答by NickUnuchek
To support translations make this:
为了支持翻译,请这样做:
1.remove string "app_name"
1.删除字符串“app_name”
2.add to gradle
2.添加到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"
3.在Manifest中设置应用名称为“@string/app_name”
4.Add to strings.xml values
4.添加到strings.xml 值
<string name="app_name_admin">App Admin</string>
<string name="app_name_release">App release</string>
<string name="app_name_debug">App debug</string>
回答by Krylez
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.
应用程序名称是用户可见的,这就是 Google 鼓励您将其保留在 strings.xml 文件中的原因。您可以定义一个单独的字符串资源文件,其中包含特定于您的 buildType 的字符串。听起来您可能有一个自定义的qa
buildType。如果这不是真的,请忽略下面的 qa 部分。
└── src
├── debug
│?? └── res
│?? └── buildtype_strings.xml
├── release
│?? └── res
│?? └── buildtype_strings.xml
└── qa
?? └── res
?? └── buildtype_strings.xml
回答by HungNM2
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:
我们需要一个解决方案来支持本地化的应用程序名称(多语言)。我已经使用 @Nick Unuchek 解决方案进行了测试,但构建失败(未找到 @string/)。稍作修改以修复此错误:build.gradle 文件:
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:
值\strings.xml:
<resources>
<string name="app_name_default">AAA prod</string>
<string name="app_name_dev">AAA dev</string>
</resources>
values-en\strings.xml:
值-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:
清单.xml:
<application
android:label="${applicationLabel}" >
</application>
回答by Steffen Funke
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
有关更动态的基于 gradle 的解决方案(例如,在主应用程序中设置一次基本应用程序名称strings.xml
,并避免在每种风格/构建类型组合中重复自己strings.xml
),请在此处查看我的答案:https: //stackoverflow.com/a/32220436/1128600
回答by CoolMind
You can use strings.xml
in different folders, see Android separate string values for release and debug builds.
您可以strings.xml
在不同的文件夹中使用,请参阅Android 单独的用于发布和调试版本的字符串值。
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.
然后将其粘贴到app\src\debug\res\values\
和app\src\release\res\values\
文件夹中。替换调试和发布文件中的“您的应用程序名称”。删除app_name
从项目strings.xml
的app\src\main\res\values\
文件夹中。
In AndroidManifest
you will have the same
在AndroidManifest
你会有同样的
<application
android:label="@string/app_name"
...
No changes at all. Even if you added a library with it's AndroidManifest
file and strings.xml
.
根本没有变化。即使您添加了一个带有AndroidManifest
文件和strings.xml
.
回答by not2qubit
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 Studioand Gradlehas 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
:
由于作者要求在Gradle 中执行此操作,我们可以假设他想在脚本中而不是在配置文件中执行此操作。由于Android Studio和Gradle在去年(~2018 年)都进行了大量更新和修改,因此上述所有其他答案似乎都过于扭曲了。简单的方法是将以下内容添加到您的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"
}
}
}