Fixing the "Build Type contains custom BuildConfig fields, but the feature is disabled" error w/ buildConfigField
Asked Answered
M

6

117

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?

Myiasis answered 30/11, 2022 at 21:23 Comment(0)
M
151

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.

Myiasis answered 30/11, 2022 at 21:23 Comment(5)
I found a similar problem for an older v of AS here issuetracker.google.com/issues/161727311, but that one introduces another way if setting those values through gradle. Haven't testet them yet.Linkboy
Confirming that setting android.defaults.buildfeatures.buildconfig=true doesn't produce error when building project, but hasn't check other influences yet.Linkboy
buildConfig = true fixed my isssueCilla
I want to add it to buildTypes instead of onVariants and I can still use it: buildTypes { getByName("debug") { buildConfigField(....`)....Overtrade
Android studio it self suggests to set android.buildFeatures.buildConfig true in app build.gradle, so I added android { buildFeatures { buildConfig = true } } and it worked for me too.Riff
S
104

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
    }
}
Shupe answered 5/12, 2022 at 17:55 Comment(2)
for each module? What is this 1995?Basement
@Basement "Always has been" memeCollincolline
T
9

add these lines to your gradle.properties:

android.buildConfig=true
API_KEY=0000

and these to build.gradle(module):

buildFeatures {
        buildConfig = true
    }
Terhune answered 19/7, 2023 at 7:32 Comment(2)
I didn't have to add first part in gradle.properties. And I added buildFeatures{ buildConfig = true } in build.gradle(app) file only and it worked.Halliburton
Maybe something has changed in the new versions of gradle. I needed two lines at that time.Terhune
N
4

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.

Nit answered 21/11, 2023 at 9:51 Comment(0)
J
3

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

Jackfish answered 4/12, 2022 at 6:45 Comment(1)
Humbly downvoted this, since solution is deprecated.Colbycolbye
J
0

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
                    }
                }
            }
        }
    }
}
Jab answered 27/2 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.