BuildConfig.VERSION_CODE is missing after update to 'com.android.tools.build:gradle:4.1.0'

I have a multi modular app and for some reason VERSION_CODE generated field has disappeared from all modules except the root one

That appears to be related to this reported issue. In that case, it was VERSION_NAME that was missing from library modules. See this specific comment for their rationale. Apparently, they fixed VERSION_NAME but did not fix VERSION_CODE. When I test this, I do not get either VERSION_NAME or VERSION_CODE, so I cannot explain how your module gets VERSION_NAME.

You should be able to switch to using buildConfigField to supply your version code:

 buildConfigField "long", "VERSION_CODE", global["versionString"]

add buildConfigField in buildTypes in module build.gradle file

buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")        
buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")

example,

buildTypes {
    debug{
        buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
        buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")

        //..
    }
    release {
        buildConfigField("long", "VERSION_CODE", "${defaultConfig.versionCode}")
        buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")

        //...
    }
}

the defaultConfig.versionCode and defaultConfig.versionName value is

defaultConfig {
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    //..
}

in your module build.gradle

you can create field in BuildConfig use buildConfigField method in module build.gradle. The method first parameter is create field type, second is field name, third is value.

Android Studio 4.1


We also have a multi-module setup and we were able to workaround with minimal changes by adding the following lines to every module's build.gradle

buildConfigField 'int', 'VERSION_CODE', "${rootProject.appVersionCode}"
buildConfigField 'String', 'VERSION_NAME', "\"${rootProject.appVersionName}\""