简体   繁体   English

Android 每个构建类型和风格的应用程序名称

[英]Android App Name Per Build Type AND Flavour

My application is having multiple build types and flavours gradle.我的应用程序具有多种构建类型风格gradle。

buildTypes {
    release {}
    test {}
    debug {}
}
productFlavors {
    europe {}
    asia {}
}

How can I name the app according to the combination of build type and flavor?如何根据构建类型和风格的组合命名应用程序?

Example:例子:
Flavour europe will have app name AppEurope Flavor europe的应用名称为AppEurope
BuildType test will add " Test " suffix behind the app name, AppEuropeTest BuildType测试会在 App 名称后面加上“ Test ”后缀, AppEuropeTest

I was facing the same problem within my watch face and tried to combine flavor dependent application names with build type dependent application label values. 我在表盘中遇到了同样的问题,并试图将依赖于风味的应用程序名称与依赖于构建类型的应用程序标签值结合在一起。 I ended up doing it as follows: 我最终这样做如下:

  1. Use manifestPlaceholder in the build.gradle to inject buildType specific string resource links: 在build.gradle中使用manifestPlaceholder注入特定于buildType的字符串资源链接:

In the build.gradle file: 在build.gradle文件中:

buildTypes {
  release {
    manifestPlaceholders = [ applicationLabel: "@string/app_name"]
  }
  debug {
     manifestPlaceholders = [ applicationLabel: "@string/app_name_dev" ]
  }
}

In the AndroidManifest.xml: 在AndroidManifest.xml中:

<application
   [..]
   android:label="${applicationLabel}">

In the strings.xml file: 在strings.xml文件中:

<resources>
  <string name="app_name">Classic &amp; Essential</string>
  <string name="app_name_dev">Classic &amp; Essential (DEV)</string>
</resources>
  1. Use flavor specific versions of string.xml resource files overriding the values for the flavor. 使用特定于字符串的风味版本的string.xml资源文件覆盖该风味值。

I also described this in one of my blog posts: https://www.journal.deviantdev.com/android-build-type-app-name-label/ 我也在我的一篇博客文章中对此进行了描述: https : //www.journal.deviantdev.com/android-build-type-app-name-label/

Your questions has been answered here - 您的问题已在这里回答-

How to change app name per Gradle build type 如何更改每种Gradle构建类型的应用程序名称

Create separate versions of string.xml for each build type for all the flavours. 为所有版本的每种构建类型创建单独的string.xml版本。

There is aa sample project here. 这里有一个示例项目。

https://github.com/commonsguy/cw-omnibus/tree/master/Gradle/HelloBuildType https://github.com/commonsguy/cw-omnibus/tree/master/Gradle/HelloBuildType

Edit 1 编辑1

android.sourceSets.europe {
    res.srcDirs = ['flavor_resources/europe/res']
}

android.sourceSets.europe.debug {
    res.srcDirs = ['flavor_resources/europe/debug/res']
}

Related link shows how to do this for flavors, and the concept is identical when expanding to include build types - we can think of both flavors and build types as combining to create "variants", and we can configure variants as easily as the flavors that make them.相关链接显示了如何为 flavor 执行此操作,并且在扩展以包含构建类型时概念是相同的 - 我们可以将 flavors 和构建类型视为组合以创建“变体”,并且我们可以像配置 flavors 一样轻松地配置变体使他们。

Assume:认为:

You have these build types:你有这些构建类型:

  • release发布
  • test测试
  • debug调试

You have these flavors:你有这些口味:

  • europe欧洲
  • asia亚洲

You have a default app_name string resource declared in the normal place您在正常位置声明了默认的app_name字符串资源

  • src/main/res/values/strings.xml

     <string name="app_name">App</string>

Manifest file:清单文件:

There is no need for manifest placeholders in this simple case.在这个简单的例子中不需要清单占位符。 If your case requires, you can configure them as per the other answers.如果您的情况需要,您可以根据其他答案配置它们。

Simply use app_name in the manifest directly.只需直接在清单中使用app_name即可。 Trust that the changes you make for the variants will reflect correctly.相信您对变体所做的更改会正确反映。

eg例如

<application
    android:label="@string/app_name"
    etc...

Different names for variants:变体的不同名称:

As standard, gradle allows for resource files to be declared at in variant source folders (like the flavor or build type source folders) and these will automatically override the defaults.作为标准,gradle 允许在变体源文件夹(如 flavor 或构建类型源文件夹)中声明资源文件,这些将自动覆盖默认值。

There is no need for source sets in this simple case.在这种简单的情况下不需要源集。

Simply add a new strings.xml file for each variant, redefining the app_name to match that variant.只需为每个变体添加一个新的strings.xml文件,重新定义app_name以匹配该变体。

eg For the Europe Test build, add your file at the variant folder named europeTest , and override the name:例如,对于 Europe Test 版本,将您的文件添加到名为europeTest的变体文件夹中,并覆盖名称:

  • src/europeTest/res/values/strings.xml

     <string name="app_name">AppEuropeTest</string>

Different variants will also get their own override strings files as required.不同的变体还将根据需要获得自己的覆盖字符串文件。

App name suffix per flavor:每个口味的应用程序名称后缀:

With app ID, each flavor can be configured to add a bit to the app ID.使用应用程序 ID,可以配置每种风格以向应用程序 ID 添加一点。

This is not currently possible with the normal build system, so you cannot currently configure the test build to append the name "Test" to the main name, and then configure the europe flavor to append "Europe".这对于普通构建系统目前是不可能的,因此您目前无法将test构建配置为 append 名称“Test”为主名称,然后将europe风味配置为 append“Europe”。

That would be really nice, but is not supported natively.那真的很好,但本机不支持。

This answer suggests a library that you can use for an alternative way of combining the app names, and could probably be used to make a more logical naming system, especially with more flavor dimensions (similar to how one would do it for application ID). This answer suggests a library that you can use for an alternative way to combining the app names, and could probably be used to make a more logical naming system, especially with more flavor dimensions(类似于应用程序 ID 的做法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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