什么是versionCodeMajor?和versionCode有什么区别?

时间:2018-10-03 15:10:37

标签: android android-9.0-pie

我刚刚发现PackageInfo.versionCode在Android Pie中已弃用。他们指出您改为使用PackageInfo.getLongVersionCode()。此新方法的JavaDoc是:

  

versionCodeversionCodeMajor返回在一起,作为一个单一的long值。 versionCodeMajor放在高32位。

但是versionCodeMajor是什么?我该如何使用? versionCodeMajor和旧的versionCode有什么区别?

它的文档几乎什么也没说:

  

内部主要版本代码。对于基本版本代码,这实际上是额外的高位;它没有其他含义,而是更高的数字是最新的。这不是通常向用户显示的版本号,通常随R.attr.versionName一起提供。

1 个答案:

答案 0 :(得分:6)

到目前为止,我发现在Android Studio 3.2.1中设置versionCodeMajor的唯一方法是通过AndroidManifest.xml并禁用 InstantRun

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:versionCodeMajor="8" [...]

'将其设置在build.gradle文件中,

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.xxx.myapplicationp"
        minSdkVersion 'P'
        targetSdkVersion 'P'
        versionCode 127
        //versionCodeMajor 8
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
  

找不到用于参数的versionCodeMajor()方法

因此,在AndroidManifest.xml中设置了所选的主要版本代码并禁用了 InstantRun ,那么您可以通过以下方式获取它:

static long getAppVersionCode(Context context) throws PackageManager.NameNotFoundException {
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        long returnValue = Long.MAX_VALUE;
        //if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P
                && !"P".equals(Build.VERSION.CODENAME) /* This 'cause the dev preview */) {
            returnValue = pinfo.versionCode;
        } else {
            returnValue = pinfo.getLongVersionCode();
            Log.d("AAA", "Full long value: " + returnValue);
            Log.d("AAA", "Major Version Code (your chosen one) " + (returnValue >> 32)); // 8 in this scenario
            Log.d("AAA", "Version Code (your chosen one) " + (int)(returnValue & 0x00000000ffffffff)); // 127 in this scenario
        }
        return returnValue;
    }

或者您可以像这样使用PackageInfoCompat

static long getAppVersionCode(Context context) throws PackageManager.NameNotFoundException {
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        long returnValue = Long.MAX_VALUE;
        returnValue = PackageInfoCompat.getLongVersionCode(pinfo);
        if (BuildConfig.DEBUG) {
            int majorCode = (int)(returnValue >> 32);
            int versionCode = (int)(returnValue & 0x00000000ffffffff); // or simply returnValue

            Log.d("AAA", "Full long value: " + returnValue);
            Log.d("AAA", "Major Version Code (your chosen one) " + majorCode); // 8 in this scenario
            Log.d("AAA", "Version Code (your chosen one) " + versionCode); // 127 in this scenario
        }
        return returnValue;
    }

这应该回答如何使用它或一种方法。

何时以及为什么使用它...我想这取决于您...正如您所指出的,文档告诉您注意为什么应该使用它。 doc的意思是,您应该只向用户显示众所周知的versionNumber。

我想他们会在versionCode中增加一些位,因为他们需要更大的版本号^^'

也就是说,如果您没有在versionCodeMajorAndroidManifest.xml文件中设置build.gradle(何时处理),还是将其设置为{{1} },则该值与旧的0弃用的字段相同。