繁体   English   中英

如何更改每个 Gradle 构建类型的应用程序名称

[英]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 StudioGradle在去年(~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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM