Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Gradle buildConfig multiple times

I'm trouble figuring out a way to add multiple lines to my BuildConfig using Gradle. It appears that when I call buildConfig a 2nd time, the first one disappears.

I was originally adding this buildConfig from a different spot, but was able to create a minimal reproducible test if I do this:

buildTypes {
    debug {
        versionNameSuffix "-DEBUG"
        buildConfig "public static final int THING_ONE = 1;"
        buildConfig "public static final int THING_TWO = 2;"
    }
    release {
        zipAlign true
        buildConfig "public static final int THING_ONE = 3;"
        buildConfig "public static final int THING_TWO = 4;"
    }
}

Then when I try to use it in code:

public class Thing {
    public static final int THING = com.example.BuildConfig.THING_ONE + com.example.BuildConfig.THING_TWO;
}

I will get this error:

/Example/src/main/java/com/example/Thing.java:2: cannot find symbol
symbol  : variable THING_ONE
location: class com.example.BuildConfig
public static final int THING = com.example.BuildConfig.THING_ONE + com.example.BuildConfig.THING_TWO;

Is there any way to add multiple different lines to the buildConfig for each productFlavor or buildType (using multiple calls to buildConfig -- instead of a multi-line string)?

like image 565
Paul Avatar asked Jan 13 '14 17:01

Paul


1 Answers

As @CommonsWare pointed out, since Gradle 1.9 (Android Studio 0.4.0) you have to declare your BuildConfig fields like that :

buildTypes {
    debug {
        versionNameSuffix "-DEBUG"
        buildConfigField "int", "THING_ONE", "1"
        buildConfigField "int", "THING_TWO", "2"
    }
}
like image 121
Andros Avatar answered Sep 18 '22 23:09

Andros