34

The resValue method (or whatever it's called) allows you to set a resource value in buildTypes or productFlavors. Is there a corresponding way to get a resource value that was set by resValue?

It appears that productFlavors is evaluated before buildTypes, so a resValue set in buildTypes takes precedence. I want to append "Debug" to the app name in debug builds, but I need to get the value that was set in the product flavor in order to append to it.

Edit: I tried Marcin Koziński's suggestion to use a variable, but all product flavors are evaluated before any build type. Therefore, this does not work:

android {
    String appName = ""

    productFlavors {
        Foo {
            appName = "Foo"
        }

        Bar {
            appName = "Bar"
        }
    }

    buildTypes {
        release {
            resValue "string", "app_name", appName 
        }

        debug {
            resValue "string", "app_name", appName + " Debug"
        }
    }
}

In buildTypes, appName always has the value from the last product flavor. So in this example, all builds receive the name "Bar" or "Bar Debug".

Basically, I need a resValueSuffix analogous to applicationIdSuffix. Apparently no such animal exists. Does the com.android.application plugin expose anything that I could use to achieve this?

4
  • stackoverflow.com/questions/17275461/…
    – Tim
    Jun 1, 2016 at 21:59
  • @TimCastelijns That Q&A is about resources from strings.xml. My resource is created with resValue. Jun 1, 2016 at 22:16
  • Is app_name used in your app anywhere besides the manifest?
    – cyroxis
    Jun 2, 2016 at 19:28
  • There is a way you can do it with manifest placeholders, see my answer below.
    – cyroxis
    Jun 3, 2016 at 14:32

3 Answers 3

45
+50

If you are only trying to set the App Label (or other manifest values) you can solve this with manifest placeholders.

android {

    productFlavors {
        Foo {
             applicationId "com.myexample.foo"
             manifestPlaceholders.appName = "Foo"
        }

        Bar {
             applicationId "com.myexample.bar"
             manifestPlaceholders.appName = "Bar"
        }
    }

    buildTypes {
        release {
            manifestPlaceholders.appNameSuffix =""
        }

        debug {
            manifestPlaceholders.appNameSuffix =".Debug"
            applicationIdSuffix ".debug"
        }
    }
}

Then in your Android Manifest you simply use both placeholders for your app name (or other values)

 <application
        android:label="${appName}${appNameSuffix}"
        ...
 </application>

This allow you to install all 4 variants side by side on a single device as well as give them different names in the app drawer / launcher.

EDIT 11/22/2019

Updated how placeholders values are set based on feedback from @javaxian

5
  • Excellent! Thank you. Jun 3, 2016 at 18:01
  • 13
    I would still be curious how the OPs question can be answered in regards to getting resource value in gradle... Aug 2, 2016 at 21:19
  • I don't know if it is possible, if it is I have never been able to figure it out.
    – cyroxis
    Aug 2, 2016 at 23:17
  • maybe this link can help you for reading from resource file using gradle: veltema.jp/2014/08/27/…
    – Ultimo_m
    Nov 9, 2016 at 10:56
  • 3
    I ran into some dire conflicts when using this otherwise great method due to the fact that one of my libs, namely Localytics, also used manifestPlaceholders. All I needed to change is set only the placeholder I wanted, i.e. manifestPlaceholders.appNameSuffix = ".Debug" rather than manifestPlaceholders = [ appNameSuffix:".Debug"].
    – javaxian
    Jul 12, 2017 at 9:22
8

You can check the build variants like this

Define values in gradle

buildTypes {
    debug{
        buildConfigField "String", "Your_string_key", '"yourkeyvalue"'
        buildConfigField "String", "SOCKET_URL", '"some text"'
        buildConfigField "Boolean", "LOG", 'true'
    }
    release {
        buildConfigField "String", "Your_string_key", '"release text"'
        buildConfigField "String", "SOCKET_URL", '"release text"'
        buildConfigField "Boolean", "LOG", 'false'

    }
}

And to access those values using build variants:

 if(!BuildConfig.LOG)
      // do something with the boolean value

Or

view.setText(BuildConfig.yourkeyvalue);
2
  • 1
    The resource in question is the app name, so it has to be a resource. Jun 2, 2016 at 18:51
  • This was not asked by OP May 17 at 22:46
1

To have an alternative version of a resource in debug builds you can use the debug source set.

strings.xml can be found under following path src/main/res/values, which means it's in the main source set. If you create a new directory src/debug/res/values you can put a new strings.xml file in there with values that should be overridden in debug builds. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My Application Debug</string>
</resources>

This will replace whatever value app_name has in your main file. You don't have to duplicate all the strings in here - ones you don't include here are simply taken from the main file.

3
  • 1
    The resource is not in strings.xml. I'm creating it with resValue because I anticipate the number of build variants * product flavors to grow, and I'd like to avoid the need to create a strings.xml for every combination. Jun 1, 2016 at 22:11
  • If you're creating the original value using resValue then you don't have to read it back from resources. You already have the value in your build.gradle script. Just assign it to a local variable and use it to create a resource for each variant. Jun 2, 2016 at 7:21
  • I tried putting the variable in the android scope. All product flavors run before any build type, so every build type sees the value assigned in the last product flavor. Jun 2, 2016 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.