简体   繁体   English

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

[英]How to change app name per Gradle build type

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 命令来应用应用程序的更改。

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.如果通过“应用程序名称”,您的意思是<application>上的android:label ,最简单的解决方案是将那个点放在字符串资源上(例如, 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.您可以在这个示例项目中看到,我在src/debug/res/values/strings.xml替换了app_name ,它将应用于debug版本。 release builds will use the version of app_name in src/main/ . release版本将使用src/main/app_name版本。

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.您可以在 AndroidManifest.xml 文件中使用@string/app_name

Make sure you remove app_name from values/ folder (no entry by this name).确保从 values/ 文件夹中删除app_name (没有这个名称的条目)。

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

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>

The app name is user-visible, and that's why Google encourages you to keep it in your strings.xml file.应用名称是用户可见的,这就是 Google 鼓励您将其保留在 strings.xml 文件中的原因。 You can define a separate string resource file that contains strings that are specific to your buildTypes.您可以定义一个单独的字符串资源文件,其中包含特定于您的 buildType 的字符串。 It sounds like you might have a custom qa buildType.听起来您可能有一个自定义的qa buildType。 If that's not true, ignore the qa part below.如果这不是真的,请忽略下面的 qa 部分。

└── 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/) .我已经使用 @Nick Unuchek 解决方案进行了测试,但构建失败(未找到 @string/)。 a little bit change to fix this bug: build.gradle file:稍作修改以修复此错误: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>

对于更动态的基于 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 .您可以在不同的文件夹中使用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.然后将其粘贴到app\\src\\debug\\res\\values\\app\\src\\release\\res\\values\\文件夹中。 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\\main\\res\\values\\文件夹中的strings.xml中删除app_name项。

In AndroidManifest you will have the sameAndroidManifest您将拥有相同的

<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的库。

There are multiple ways you can do.有多种方法可以做到。

you can create manifestPlaceholders OR resValue in app level build.gradle .你可以创建manifestPlaceholders OR resValue在应用级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如果你有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"'
    }
}

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然后在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>

NOTE: Make sure to change android:lable as well in <activity> of LAUNCHER category.注意:确保在LAUNCHER类别的<activity>中也更改android:lable If it doesn't require to use android:label in <activity> , just remove this.如果不需要在<activity>使用android:label ,只需删除它。


If you do not want to add in build.gradle directly, you can add in values/string.xml of selected ProductFlavors .如果不想直接在build.gradle添加,可以在选择的ProductFlavors values/string.xml中添加。 eg例如

Add添加

<string name="appLabel">My App - Development</string> 

in app/src/dev/res/values/string.xmlapp/src/dev/res/values/string.xml

and

<string name="appLabel">My Awesome App</string> 

in app/src/prod/res/values/string.xmlapp/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.由于作者要求在Gradle 中执行此操作,我们可以假设他想在脚本中而不是在配置文件中执行此操作。 Since both Android Studio and Gradle has been heavily updated and modified in the last year (~2018) all other answers above, seem overly contorted.由于Android StudioGradle在去年(~2018 年)都进行了大量更新和修改,因此上述所有其他答案似乎都过于扭曲了。 The easy-peasy way, is to add the following to your app/build.gradle :简单的方法是将以下内容添加到您的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.

相关问题 在Android gradle构建中,如何通过构建类型更改软件包名称? - In a Android gradle build, how to change the package name by build type? Android 每个构建类型和风格的应用程序名称 - Android App Name Per Build Type AND Flavour Android / Gradle:基于构建类型*和*产品风格的应用程序名称 - Android/Gradle: App name based on build type *and* product flavor 如何基于构建类型设置app_name并使用Gradle构建风味 - How to set app_name based on built type and build flavor using gradle 如何更改 Android 应用程序包名称 (app.aab) 以反映应用程序版本和构建类型 - How To change Android App Bundles name (app.aab) to reflect App version and build type 如何使用Gradle更改Android风味应用程序的包名称? - How to change the package name of an Android flavor app with Gradle? 使用Gradle组装时如何更改Android应用程序包名称? - How to change the Android app package name when assembling with Gradle? Android NDK和Gradle:每种构建类型不同的Android.mk - Android NDK and Gradle: Different Android.mk per build type 如何使用组,名称,版本,类型的格式在build.gradle中添加@jar类型的依赖项? - How to add dependency of type @jar in build.gradle using format of group,name,version,type? 如何更改使用 Flutter 构建的应用程序显示名称? - How can I change the app display name build with Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM