error: cannot find symbol variable BuildConfig.VERSION_CODE in android studio
symbol: variable VERSION_CODE
location: class BuildConfig
error occured when i updated my androidstudio to latest version with gradle build version to distributionUrl=https://services.gradle.org/distributions/gradle-7.4-all.zip this in gradle-wrapper.properties, and tried all the possibilities of downgrading gradle build tool version,
my code is
String versionCode = String.valueOf(BuildConfig.VERSION_CODE);
may be this is deprecated or removed in this, i don't know exactly alternative for this.
Add the versionCode and versionName field in build.gradle as below:
//
Android Studio 4.1, Library Modules, and VERSION_CODE Android Studio 4.1 — or, more accurately, version 4.1.0 of the Android Gradle Plugin — has a breaking change: it no longer adds VERSION_CODE (and, sometimes, VERSION_NAME), to BuildConfig.
This was originally reported back in June, with Canary 4, but despite being a P1 ticket, it did not get resolved before AS 4.1 shipped. I found out about it from Stack Overflow. And, since it was mentioned in the release notes, this might not change.
If you create a scrap AS 4.1 project, your app module will have typical versionCode and versionName properties:
That module uses the com.android.application plugin, and it generates a BuildConfig that contains VERSION_CODE and VERSION_NAME:
If you then add a library module to the same project, it too will come with the same versionCode and versionName lines… but they do not get reflected in the module’s BuildConfig:
I am somewhat surprised that the ticket was not closed as “working as intended”, as this change was intentional
this was done on purpose as having a version for libraries (present in the manifest file or in code) does not make sense for libraries in Android. Only applications have an android version. UPDATE: A few hours after I posted this, it was indeed closed as “intended behavior”. Somebody who I suspect is Xavier Ducrohet posted an extended explanation of how all this came about.
And the release notes suggest that versionCode and versionName will be removed from the Gradle DSL in the future.
What worked for me was to declare them manually, based on this issue comment:
In my case, I just hard-coded the values, but you could arrange to pull them from some common location.
Note that there may be varying behavior here. In the Stack Overflow question, SO user Void reported two differences from what I am seeing:
VERSION_NAME was getting generated, but not VERSION_CODE
Using buildConfigField for VERSION_CODE would not work, but using a unique name would
In light testing, I could not reproduce those findings, but be prepared for some variation in symptoms.
The safest thing to do, if you elect to go the buildConfigField route, is to not use VERSION_CODE and VERSION_NAME, but instead use your own unique names, as Void did. Basically, consider VERSION_CODE and VERSION_NAME to be part of a Google-managed namespace, and assume that Google might mess with them. If you use your own names, with luck, they will be untouched by future changes to the Android Gradle Plugin.
UPDATE: In the aforementioned extended explanation, Google echoed this recommendation: use your own custom fields.
***if you want use this in your library you may have chances to conflict for older and newer version of build tools for that you have to add check or make unique name to get generate in BuildConfig.java file like below:
and instead old use new BuildConfig.VERSION_CODE_1, name , it avoid the multiple generating name and code and avoids conflicts***