How to use build types (debug vs release) to set different styles and app names?
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.
Related videos on Youtube
android developer
Really like to develop Android apps & libraries on my spare time. Github website: https://github.com/AndroidDeveloperLB/ My spare time apps: https://play.google.com/store/apps/developer?id=AndroidDeveloperLB
Updated on January 17, 2020Comments
-
android developer over 4 years
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?
-
Zingam about 7 yearsWhere are these build types defined "debug", "release". I cannot find any description in the DSL docs.
-
android developer about 7 years@Zingam You will see "buildTypes" in the gradle file whenever you create a new project.
-
android developer almost 9 yearsSo you mean I don't need to use "buildTypes" in the "build.gradle" file for this task, but just put the resource files in the folders of each build type ? In "src" folder, one on "main", and one on "debug" folders ?
-
CommonsWare almost 9 years@androiddeveloper: For the built-in build types of
debug
andrelease
, yes. You are welcome to invent your own build types too, if needed, and for that you will need to usebuild.gradle
(to define the types) in addition to sourcesets (for supplying alternative inputs to the build, like replacement versions of resources). -
android developer almost 9 yearsI see. Even though I don't need it (well now, at least), would you please tell me how to set another folder for another build type that you create yourself? Also, does the same operation (of putting files into those folders) work for java files too (my guess is that it won't) ?
-
android developer almost 9 yearsAlso, what exactly does "resValue" used for?
-
CommonsWare almost 9 years@androiddeveloper: "would you please tell me how to set another folder for another build type that you create yourself?" -- see
mezzanine
in thebuild.gradle
of the sample app I linked to. "does the same operation (of putting files into those folders) work for java files too" -- yes, except that you cannot replace Java classes defined in another sourceset (e.g.,main
). "what exactly does "resValue" used for?" -- it would be for cases where you wanted to define (not replace) the resource based on calculations made inbuild.gradle
. -
android developer almost 9 yearsAbout "mezzanine", I don't see a folder that was created for it, so maybe I wasn't clear enough: I meant how you prepare the folder for the new build type? About Java, this was what I thought- overriding of classes (exact class-path) isn't possible. Makes sense. About "resValue", this seems like a rare thing to do, maybe for version code (like one that's based on the current time) ?
-
CommonsWare almost 9 years@androiddeveloper: "I meant how you prepare the folder for the new build type?" -- um,
mkdir
? I mean, you create a directory namedmezzanine
the same way that you create a directory nameddebug
. The only difference is that you have to tell Gradle about the existence ofmezzanine
, which is done inbuild.gradle
. "this seems like a rare thing to do" -- agreed. -
android developer almost 9 yearsAbout "mezzanine", ok, I thought you need to specify it in the build.gradle file, as the others were the default ones (there isn't a folder called "release", for example).
-
CommonsWare almost 9 years
-
android developer almost 9 yearsok, sorry. probably missed the part that the additional folders are called "sourcesets" (right?).
-
CommonsWare almost 9 years@androiddeveloper: In the original discussion,
main
anddebug
are called sourcesets. That's Gradle's term (though I think Maven might use it too for their build system). -
android developer almost 9 yearsThank you again. I've now ticked your answer. You are great and have a good patience !
-
android developer almost 9 yearsI think Android-Studio has a bug with this feature. At first it worked as expected, but when I changed the values (of a single boolean resource) and then back, it acts as if debug build is like release build. Will have to check it further and maybe create a POC. May I show you this issue in case I can reproduce it ?
-
CommonsWare almost 9 years@androiddeveloper: If you file an issue for it on b.android.com, consider linking to it here.
-
android developer almost 9 yearsCan't reproduce on POC. Maybe I just did something wrong. Anyway, thank you.
-
android developer over 7 yearsThis doesn't change the resource value for debug&release. Not what I asked about
-
sziraqui over 6 yearshow is this related? OP wants to set different buildTypes and the app name is to be set differently for each buildType
-
Aman Verma over 5 yearsI have set my URL in the build file for the debug build type, is it possible to reverse engineer the build file somehow and expose my URL which is not what I want or does my URL which is defined in debug build type be exposed if I upload the release apk to playstore.
-
CommonsWare over 5 years@AmanVerma: "does my URL which is defined in debug build type be exposed if I upload the release apk to playstore." -- if you have done this correctly, that URL will not exist in the release APK.
-
Aman Verma over 5 yearsdebug { debuggable true applicationIdSuffix ".debug" resValue "string", "BASE_URL", '"Mytesting URL"' resValue "string", "app_name", "UAT APP" }
-
CommonsWare over 5 years@AmanVerma: That URL should not exist in a
release
APK, if this is the only place that you have defined that URL. -
Aman Verma over 5 yearsYeah. This is the only place where i have set the URL. However if i search this particular URL i find it in one more place which is debug\..\generated. I think it is auto generated and will not be visible in release APK or is it?
-
CommonsWare over 5 years@AmanVerma: The contents of a
debug
directory under thebuild
directory are not included in arelease
APK. If you have additional concerns, please ask a separate Stack Overflow question. -
Aman Verma over 5 years@CommonsWare i have already asked this question but no one replied...this is the link... stackoverflow.com/questions/53232057/…