Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 930
android developer Avatar asked May 16 '15 18:05

android developer


People also ask

What is the difference between Release and debug build?

The Debug configuration of your program is compiled with full symbolic debug information which help the debugger figure out where it is in the source code. Is Release mode is faster than Debug mode ? The Release mode enables optimizations and generates without any debug data, so it is fully optimized. .

What is debug build and Release build?

Debug build and release build are just names. They don't mean anything. Depending on your application, you may build it in one, two or more different ways, using different combinations of compiler and linker options.

What is the difference between debug mode and Release mode in Visual Studio?

Visual Studio projects have separate release and debug configurations for your program. You build the debug version for debugging and the release version for the final release distribution. In debug configuration, your program compiles with full symbolic debug information and no optimization.

What is the difference between release build and debug build configurations?

The size of the EXE file prepared with using Release Build Configurations will be less in compared to EXE file prepared with using Debug Build Configurations because when we active release then Compiler does not produce symbolic debugging information the code is not generated for TRACE and ASSERT calls, meaning the size of executable is reduced.

What does the debug build type do?

This allows you to debug the app on secure Android devices and configures app signing with a generic debug keystore. You can add the debug build type to your configuration if you want to add or change certain settings.

What is the application ID for the free debug build variant?

Because Gradle applies the build type configuration after the product flavor, the application ID for the "free debug" build variant is now "com.example.myapp.free.debug". This is useful when you want to have both the debug and the release build on the same device, because no two apps can have the same application ID.

Should I use two different builds for client and debug?

In some cases, it may be more practical to use two different builds: overall, client code needs optimization, for performance reasons, but you don't want optimization when debugging.


1 Answers

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.

like image 92
CommonsWare Avatar answered Oct 22 '22 09:10

CommonsWare