How to use build types (debug vs release) to set different styles and app names?

混江龙づ霸主 提交于 2019-11-29 09:19:41

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.

In File|Project Structure|app|Flavors we have:

Version Name: 1.3

In Strings resource file we have:

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

In "onCreate" of MainActivity class:

...
//add version to application title
int versionCode = BuildConfig.VERSION_CODE; // unused in my application
String versionName = BuildConfig.VERSION_NAME;
this.setTitle(this.getTitle() + " v"+  versionName);
...

The result is "MyAppTitle v1.3"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!