Reputation: 43683
In my build.gradle (Module: app) I specified one buildConfigField
and one resValue
variable.
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "APP_EXP_DATE", "\"DEC 31 23:59:59 EDT 2018\""
resValue "String", "app_exp_date", "\"DEC 31 23:59:59 EDT 2018\""
}
}
Then I expected them to use in my Java code like this:
BuildConfig.APP_EXP_DATE
R.string.app_exp_date
but unfortunately I am experiencing the following errors:
error: cannot find symbol variable APP_EXP_DATE
error: illegal start of type
How can I make it work to be able to access variables from gradle in my Java code?
Upvotes: 8
Views: 9088
Reputation: 1177
Well, you have some options:
Define your strings and values under defaultConfig as follow:
android {
// your code
defaultConfig {
// your code
resValue "string", "<key>", "<value>"
buildConfigField "string", "<key>", "<value>"
// ...
}
// your code
}
You can put your string in both Release and Debug type
buildTypes {
release {
// your code
resValue "string", "<key>", "<value>"
buildConfigField "string", "<key>", "<value>"
// ...
}
debug {
// your code
resValue "string", "<key>", "<value>"
buildConfigField "string", "<key>", "<value>"
// ...
}
}
Hope this help!
Upvotes: 7
Reputation: 16976
How can I make it work to be able to access variables from gradle in my Java code?
The issue in your codes seems to be using in release
buildType which might cause the issue (not resolving it.
But, this is how it should be :
In gradle.properties
:
ExpDate="DEC 31 23:59:59 EDT 2018"
In app/Build.gradle
(Note that it should be in android block code):
def APP_EXP_DATE = '"' + ExpDate + '"' ?: '"Define Expire Date"'
android.buildTypes.each { type ->
type.buildConfigField 'String', 'APP_EXP_DATE', ExpDate
}
Usage:
BuildConfig.APP_EXP_DATE
As a Toast
:
Toast.makeText(activity, BuildConfig.APP_EXP_DATE, Toast.LENGTH_LONG).show()
Upvotes: 5