25

I would like to define a build config filed where I can use a variable defined in the gradle script it self:

def someVar = 512
android {
...
    buildConfigField 'int', 'SOME_INT_FIELD',  someVar
}

But this produces following error:

Error:(25, 0) Gradle DSL method not found: 'buildConfigField()'

Possible causes:

  • The project 'PROJECT' may be using a version of Gradle that does not contain the method.
  • The build file may be missing a Gradle plugin.

  • I could use quotes like:

    def someVar = 0
    android {
    ...
        buildConfigField 'int', 'SOME_INT_FIELD',  '"' + someVar + '"'
    }
    

    But this comes to a compiler error in BuildConfig

    // Fields from default config.
    public static final int SOME_INT_FILED = "512";
    

    So for now I stay with:

    def someVar = 0
    android {
    ...
        buildConfigField 'String', 'SOME_INT_FIELD',  '"' + someVar + '"'
    }
    

    and use it like:

    final int value = Integer.valueOf(BuildConfig.SOME_INT_FIELD);
    

    Does anybody has better solution or do I use buildConfigField wrong?

    (I also tried using parentheses in combination with any of possibility above.)

    salcosand
    • 2,022
    • 4
    • 24
    • 27

    5 Answers5

    56

    I found a solution, so maybe this answer will help somebody in future.

    def String globalVersionCode
    
    defaultConfig {
        applicationId "com.test.gradle.build"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "0.1"
    
        globalVersionCode = versionCode
    }
    
    buildTypes {
        release {
             buildConfigField ("int", "DatabaseVersion", globalVersionCode)
        }
    }
    

    And now in java I can get DatabaseVersion variable:

    public static final int DB_VERSION = BuildConfig.DatabaseVersion;
    
    Beyka
    • 1,372
    • 1
    • 10
    • 15
    • 3
      Thanks, the key is to define the variable as type of 'String', but use 'int' for buildConfigField. That makes any casting and parsing unnecessary. That's what I was looking for. – salcosand Jul 28 '16 at 11:52
    • don't forget the commas guys – karuhanga Dec 26 '18 at 11:25
    12

    Declaring an int field in build.gradle should not and does not require parsing at java side.The only mistake in your code was use of double quotes. Correct way is given below-

    buildConfigField 'int', 'SOME_INT_FIELD', '512'
    

    With above in your build.gradle file, you can use it simply as an int in java code-

    public static final int SOME_INT_FIELD = BuildConfig.SOME_INT_FIELD;
    
    Sachin Gupta
    • 444
    • 3
    • 10
    8

    Try this:

    int val = 10
    buildConfigField 'int', 'SOME_INT_FIELD', val.toString()
    
    Ashwin
    • 7,277
    • 1
    • 48
    • 70
    5

    You may get some reason to use same gradle file and also use in java files.

    def boolean isMyFlag
    
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.test.gradle.build"
        minSdkVersion 21
        targetSdkVersion 22
        buildConfigField 'int', 'Version_code', "3"
        buildConfigField 'String', 'Version_Name', "'0.1'"
        buildConfigField "boolean", "myFlag", "${isMyFlag}"
    
    }
    

    And now you can use in java as well as in gradle file too.

    // Use for Java
    if(BuildConfig.myFlag){
        // My logic here;
    }
    

    and

    // Use for gradle
    if (isMyFlag) {
        implementation 'com.my.dependency.classes:my-module:2.3'
    }
    

    My solution is works for both place Java and build.gradle files as you may get some logic like this.

    AndyBoy
    • 564
    • 6
    • 29
    • Note that you can also override the default values in `buildTypes` and `productFlavors` blocks. Combined with properties files, this allows you to have consistent build- and run-time configuration. – cji Nov 08 '22 at 13:59
    2
    def someVar = 100
    
    buildConfigField 'int', 'SOME_INT_FIELD',  String.valueOf(someVar)
    

    I use the above to resolve the question, hope to help you .

    peerless2012
    • 179
    • 1
    • 7