如何使用构建类型(调试与发布)来设置不同的样式和应用程序名称? [英] How to use build types (debug vs release) to set different styles and app names?

查看:26
本文介绍了如何使用构建类型(调试与发布)来设置不同的样式和应用程序名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Studio上,你可以有不同的构建类型,每个都有自己的配置,类似于product-flavors(如图所示此处)

On Android Studio, you can have different build types, each has its own configuration, similar to product-flavors (as shown here)

我希望每次我在某处安装我的应用程序时,我都会立即知道它是哪种类型 - 发布或调试,只需查看它.

I wish that each time I have my app installed somewhere, I would immediatly know which type it was - release or debug, just by looking at it.

为此,我想我可以使用 build.gradle 文件:

For this, I think I can use the build.gradle file :

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
    }
}

问题是,我不知道该放什么.我希望应用程序名称不同(并且字符串文件中有字符串,因为它是翻译的),并且我想将应用程序中某些内容的样式设置为不同(例如,操作栏的颜色).

Thing is, I don't know what to put there. I want the app name to be different (and yet have the string in the strings files, as it's translated), and I want to set the style of something in the app to be different (for example, the color of the action bar).

我发现我可以使用resValue"(在此处),但由于某种原因,无论我做什么,它都不会编译:

I've found that I can use "resValue" (found about it here), but for some reason, no matter what I do, it won't compile:

  • 如果资源已经被声明(比如在 app-name 中,它被翻译了),它说资源是重复的
  • 如果未声明资源,我将无法通过代码/xml 访问它.

如何为构建类型使用不同的资源值,即使它们已经存在?

How do I use different resource values for the build types, even if they already exist?

推荐答案

如何为构建类型使用不同的资源值,即使它们已经存在?

How do I use different resource values for the build types, even if they already exist?

它们已经存在于 main 源集中.为您感兴趣的其他构建类型添加其他源集,您可以在其中覆盖所需的资源.

They already exist in the main sourceset. Add other sourcesets for your other build types of interest, where you override the resources you want.

例如,在这个示例项目中我有一个 main 源集和一个 debug 源集.两者在 res/values/strings.xml 中都有 app_name 字符串资源,但值不同.在 debug 构建中,将使用资源的 debug sourceset 版本;在任何其他构建(例如,release)中,debug 源集将被完全忽略,并使用资源的 main 源集版本.

For example, in this sample project I have a main sourceset and a debug sourceset. Both have the app_name string resource in res/values/strings.xml, but with different values. In a debug build, the debug sourceset version of the resource will be used; in any other build (e.g., release), the debug sourceset is ignored entirely, and the main sourceset version of the resource is used.

请注意,我没有release 源集.尤其是在覆盖资源时,这完全没问题——当您想要更改某个构建类型的某些内容时,您只需要该构建类型的源集,而不是针对您正在使用的每个构建类型.

Note that I do not have a release sourceset. Particularly when overriding resources, this is perfectly fine -- you only need a sourceset for a build type when you want to change something for that build type, not for every build type that you are using.

这篇关于如何使用构建类型(调试与发布)来设置不同的样式和应用程序名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆