【发布时间】:2014-05-01 16:03:30
【问题描述】:
我正在使用 gradle 为 2 个小型 android 应用程序创建不同的风格,我想知道我是否可以在 build.gradle 中的 xml 文件上编辑应用程序名称,以适应我的不同风格。
【问题讨论】:
-
我已经回答了here,你可以看一下我的回答,可能对你有帮助。
标签: gradle android-gradle-plugin build.gradle
我正在使用 gradle 为 2 个小型 android 应用程序创建不同的风格,我想知道我是否可以在 build.gradle 中的 xml 文件上编辑应用程序名称,以适应我的不同风格。
【问题讨论】:
标签: gradle android-gradle-plugin build.gradle
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myapp">
<application
tools:replace="android:label"
android:label="${appName}"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="${appName}"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
分级
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.release
// manifestPlaceholders = [appName: appConfig.appName]
manifestPlaceholders = [appName: "Your app Name"]
}
debug {
signingConfig signingConfigs.release
// manifestPlaceholders = [appName: appConfig.appName]
manifestPlaceholders = [appName: "Your app Name"]
}
}
【讨论】:
此答案基于 Tom 的,效果最好,您可以使用 gradle.properties 以允许在构建过程中进一步制作动画。
在build.gradle:
debug {
resValue 'string', 'app_name', APP_NAME
}
在gradle.properties:
APP_NAME="Template 1"
【讨论】:
其实……求更明确的解释;
在 main build.gradle 中:
ext {
APP_NAME = "My Fabulous App"
APP_NAME_DEBUG = "My Fabulous App debug"
}
在 app build.gradle 中:
android {
buildTypes {
debug {
manifestPlaceholders = [appName: APP_NAME_DEBUG]
}
release {
manifestPlaceholders = [appName: APP_NAME]
}
}
}
所以在 AndroidManifest.xml 中
<application
...
android:label="${appName}"
>
是可能的。瞧!您有不同的应用程序名称用于发布和调试。
【讨论】:
manifestPlaceholders = [appName: "@string/app_name"]) 使用本地化的应用名称,并为调试版本使用非本地化名称。
您可以使用resValue,例如。
debug {
resValue 'string', 'app_name', '"MyApp (Debug)"'`
}
release {
resValue 'string', 'app_name', '"MyApp"'
}
确保您的 AndroidManifest 为应用程序使用 android:label="@string/app_name",并从 strings.xml 中删除 app_name,因为它会在尝试合并它们时与 gradle 生成的 strings.xml 冲突。
【讨论】:
strings.xml 的内容来投票,但 resValue 应该是正确的方法,因为您可以将实际值放入 gradle.properties。
app name 是什么意思?清单中的应用程序包名称或启动器中显示的应用程序名称?
如果是前者,那么:
android {
productFlavors {
flavor1 {
packageName 'com.example.flavor1'
}
flavor2 {
packageName 'com.example.flavor2'
}
}
}
也可以覆盖应用名称,但您必须提供风味叠加资源。
所以创建以下文件:
src/flavor1/res/values/strings.xmlsrc/flavor2/res/values/strings.xml在它们中,只需覆盖包含您的应用名称的字符串资源(您的清单通过@string/app_name 之类的东西用于主要活动标签的资源)。您还可以根据需要提供不同的翻译。
【讨论】: