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?

6

There are 6 best solutions below

0
On

I'll just add this alternative "solution" here... For whomever it might help ?

From developer.android.com documentation :

You can override the default for this for all projects in your build by adding the line android.defaults.buildfeatures.buildconfig=true in the gradle.properties file at the root project of your build.

So one might just want to explicitly declare that in his project level gradle.properties.

5
On

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

Or, per @Scott_AGP's answer, it may be better to add this to build.gradle instead of changing gradle.properties:

android {
    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.

1
On

adding android.defaults.buildfeatures.buildconfig=true to your gradle.properties would fix this.

2
On

Avoid adding android.defaults.buildfeatures.buildconfig=true to your gradle.properties file because that property is deprecated in AGP 8.0 and is scheduled to be removed in AGP 9.0.

Instead, add the following to the per-module build.gradle file for each module using BuildConfig:

android {
    buildFeatures {
        buildConfig = true
    }
}
2
On

add these lines to your gradle.properties:

android.buildConfig=true
API_KEY=0000

and these to build.gradle(module):

buildFeatures {
        buildConfig = true
    }
0
On

Or apply it via android/build.gradle

allprojects {
repositories {
    google()
    mavenCentral()
}
//ERROR Caused by: org.gradle.api.reflect.ObjectInstantiationException:
// Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            project.android {
                if (namespace == null) {
                    namespace project.group
                }
                // Avoid adding android.defaults.buildfeatures.buildconfig=true
                // to your gradle.properties file because that property is deprecated in AGP 8.0 and is scheduled to be removed in AGP 9.0.
                buildFeatures {
                    if (buildConfig == null) {
                        buildConfig true
                    }
                }
            }
        }
    }
}