In the new version of Android Studio (Flamingo | 2022.2.1 Canary 9) with the org.jetbrains.kotlin (1.8.0-Beta) plugin and 8.0.0-alpha09 gradle plugin, a new build suddenly gets this error:
Build Type 'release' contains custom BuildConfig fields, but the feature is disabled.
Is there a way to make this go away?
CodePudding user response:
Answering my own question -- there is a quick solution. Try adding the following line to gradle.properties
, and the problem should hopefully stop bothering you (for now):
android.defaults.buildfeatures.buildconfig=true
This issue is due to the deprecation of buildConfigField
(from android.packageBuildConfig) as described in this commit.
UPDATE 12/12/22:
Per a note from Roar Grønmo below, there is a newer way to sneak the timestamp into the BuildConfig.java
file than the one I suggested back in 2014.
To use this newer method, first delete any lines in your current build.gradle
(or build.gradle.kts
) file that looks like:
buildConfigField("String", "BUILD_TIME", "\"" System.currentTimeMillis().toString() "\"")
Instead, first add the following to the top of your build.gradle.kts
:
import com.android.build.api.variant.BuildConfigField
and outside of the android { ... }
part of build.config.kts
add this:
androidComponents {
onVariants {
it.buildConfigFields.put(
"BUILD_TIME", BuildConfigField(
"String", "\"" System.currentTimeMillis().toString() "\"", "build timestamp"
)
)
}
}
You shouldn't have to make any new changes to your main codebase-- the timestamp can still be accessed in Kotlin like this:
private val buildDate = Date(BuildConfig.BUILD_TIME.toLong())
Log.i("MyProgram", "This .apk was built on ${buildDate.toString()}");
That's it! Note this still requires the change to gradle.properties
described above or you will see an Accessing value buildConfigFields in variant ____ has no effect as the feature buildConfig is disabled. warning.
There may still be a better way to do this without using BuildConfigField
, but if so, I don't know it. If anyone has a more permanent fix, please let me (us) know.
CodePudding user response:
adding android.defaults.buildfeatures.buildconfig=true
to your gradle.properties
would fix this.