From the com.android.tools.build:gradle:4.1.0
beginning, the build.gradle
document officially removed versionName
and versionCode
, refer to the link .
If you still need BuildConfig.VERSION_NAME
it, you can use the following method
buildConfigField "int", 'VERSION_CODE', String.valueOf(1)
buildConfigField 'String', 'VERSION_NAME', "\"" + "1.0.0" + "\""
But just doing this is not enough, because after this you can't AndroidManifest.xml
bring the version number, resulting in no version number in the last package. So you also need to AndroidManifest.xml
add the version number.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="1"
android:versionName="1.0.0">
</manifest>
Of course, you can also discard BuildConfig.VERSION_NAME
the version and use the following code to read the version number.
try {
val manager = this.packageManager
val info = manager.getPackageInfo(this.packageName, 0)
info.versionName
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
L.i("info.versionName ${
info.versionName} ${
info.longVersionCode} ")
}else{
L.i("info.versionName ${
info.versionName} ${
info.versionCode} ")
}
} catch (e: Exception) {
e.printStackTrace()
}