如何使用生成类型(调试VS版本)来设置不同的风格和应用程序的名称? [英] How to use build types (debug vs release) to set different styles and app names?

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

问题描述

背景

在Android的工作室,你可以有不同的版本类型,每个都有自己的配置,相似的产品口味(如图所示的 此处

问题

我想,我已经安装了我的应用程序的地方,每次,我会立刻知道哪些类型是 - 释放或调试,只要看看它

对于这一点,我想我可以使用build.gradle文件:

  buildTypes {
    推出 {
        minifyEnabled真
        proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'proguard-rules.pro
    }
    调试{
        调试的真
    }
}
 

事情是,我不知道放在那里。我想要的应用程序名称是不同的(尚未有字符串中的字符串的文件,因为它的翻译),我想设置的东西风格的应用程序是不同的(例如,操作栏的颜色)

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

  • 如果资源已经宣布(如在应用程序名,这是翻译),它说,资源是重复
  • 如果资源没有宣布,我无法通过code / XML实现它。

问题

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

解决方案
  

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

它们已存在于 sourceset。添加其他sourcesets感兴趣,在那里你覆盖你想要的资源,你的其他版本的类型。

例如,在此示例项目,我有一个 sourceset和调试 sourceset。两人都在 RES /价值/ strings.xml中,但具有不同的值 APP_NAME 字符串资源。在调试订做,调试 sourceset版本的资源将被使用;在任何其他版本(例如,发布)的调试 sourceset被完全忽略,而 sourceset版本的资源的使用。

请注意,我做的没有的有发布 sourceset。特别是压倒一切的资源时,这是完全没有问题 - 当你想改变一些东西为构建类型,不适合您使用的是每个构建类型,您只需要一个sourceset一个构建类型

Background

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

The problem

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.

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).

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:

  • If the resource was already declared (like in the app-name, which is translated), it says the resource is duplicated
  • If the resource wasn't declared, I can't reach it via code/xml.

The question

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?

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

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.

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.

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

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