Fixing the “Build Type contains custom BuildConfig fields, but the feature is disabled” error w/ buildConfigField

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)