![](/img/trans.png)
[英]In a Android gradle build, how to change the package name by build type?
[英]How to change app name per Gradle build type
我試圖找出一種方法,能夠在 gradle 中根據構建類型更改我的應用程序的應用程序名稱。
例如,我希望調試版本具有<APP_NAME>-debug
,而 qa 版本具有<APP-NAME>-QA
。
我熟悉:
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-DEBUG'
}
但是,我似乎無法在啟動器中找到一個 gradle 命令來應用應用程序的更改。
如果通過“應用程序名稱”,您的意思是<application>
上的android:label
,最簡單的解決方案是將那個點放在字符串資源上(例如, android:label="@string/app_name"
),然后使用不同版本的src/debug/
源集中的那個字符串資源。
您可以在這個示例項目中看到,我在src/debug/res/values/strings.xml
替換了app_name
,它將應用於debug
版本。 release
版本將使用src/main/
的app_name
版本。
你可以使用這樣的東西
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"
}
}
您可以在 AndroidManifest.xml 文件中使用@string/app_name 。
確保從 values/ 文件夾中刪除app_name (沒有這個名稱的條目)。
你可以用 gradle 做到這一點:
android {
buildTypes {
release {
manifestPlaceholders = [appName: "My Standard App Name"]
}
debug {
manifestPlaceholders = [appName: "Debug"]
}
}
}
然后在你的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>
注意:它也適用於productFlavors
。
為了支持翻譯,請這樣做:
1.刪除字符串“app_name”
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.在Manifest中設置應用名稱為“@string/app_name”
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>
應用名稱是用戶可見的,這就是 Google 鼓勵您將其保留在 strings.xml 文件中的原因。 您可以定義一個單獨的字符串資源文件,其中包含特定於您的 buildType 的字符串。 聽起來您可能有一個自定義的qa
buildType。 如果這不是真的,請忽略下面的 qa 部分。
└── src
├── debug
│ └── res
│ └── buildtype_strings.xml
├── release
│ └── res
│ └── buildtype_strings.xml
└── qa
└── res
└── buildtype_strings.xml
我們需要一個解決方案來支持本地化的應用程序名稱(多語言)。 我已經使用 @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 ]
}
}
值\\strings.xml:
<resources>
<string name="app_name_default">AAA prod</string>
<string name="app_name_dev">AAA dev</string>
</resources>
值-en\\strings.xml:
<resources>
<string name="app_name_default">AAA prod en</string>
<string name="app_name_dev">AAA dev en</string>
</resources>
清單.xml:
<application
android:label="${applicationLabel}" >
</application>
對於更動態的基於 gradle 的解決方案(例如,在 main 的strings.xml
設置一個基本應用程序名稱,並避免在每個風格/構建類型組合的strings.xml
重復自己),請參閱我的答案: https : //stackoverflow.com/ a/32220436/1128600
您可以在不同的文件夾中使用strings.xml
,請參閱Android 單獨的用於發布和調試版本的字符串值。
所以,創建這個文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Your app name</string>
</resources>
然后將其粘貼到app\\src\\debug\\res\\values\\
和app\\src\\release\\res\\values\\
文件夾中。 替換調試和發布文件中的“您的應用程序名稱”。 從app\\src\\main\\res\\values\\
文件夾中的strings.xml
中刪除app_name
項。
在AndroidManifest
您將擁有相同的
<application
android:label="@string/app_name"
...
根本沒有變化。 即使您添加了一個帶有AndroidManifest
文件和strings.xml
的庫。
有多種方法可以做到。
你可以創建manifestPlaceholders
OR resValue
在應用級build.gradle
。 例如
buildTypes {
release {
...
manifestPlaceholders = [appLabel: "My App"]
//resValue "string", "appLabel", '"My App"'
}
debug {
...
manifestPlaceholders = [appLabel: "My App - Debug"]
//resValue "string", "appLabel", '"My App - Debug"'
}
}
或者
如果你有productFlavors
,你可以在那里創建
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"'
}
}
然后在AndroidManifest.xml
如果您使用manifestPlaceholders
,只需更改android:label="${appLabel}"
如下,或者如果您使用resValue
,只需更改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>
注意:確保在LAUNCHER
類別的<activity>
中也更改android:lable
。 如果不需要在<activity>
使用android:label
,只需刪除它。
如果不想直接在build.gradle
添加,可以在選擇的ProductFlavors
values/string.xml
中添加。 例如
添加
<string name="appLabel">My App - Development</string>
在app/src/dev/res/values/string.xml
和
<string name="appLabel">My Awesome App</string>
在app/src/prod/res/values/string.xml
由於作者要求在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"
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.