Gradle buildConfigField with integer variable
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;
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;