The new version of gradle removes the issue of versionCode and versionName

From the com.android.tools.build:gradle:4.1.0beginning, the build.gradledocument officially removed versionNameand versionCode, refer to the link .

If you still need BuildConfig.VERSION_NAMEit, 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.xmlbring the version number, resulting in no version number in the last package. So you also need to AndroidManifest.xmladd 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_NAMEthe 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()

        }

Guess you like

Origin blog.csdn.net/Ser_Bad/article/details/109995482