Search code examples
androidandroid-9.0-pie

What is versionCodeMajor? And what is the difference with versionCode?


I just found that PackageInfo.versionCode is deprecated in Android Pie. They point you to use PackageInfo.getLongVersionCode() instead. The JavaDoc of this new method is:

Return versionCode and versionCodeMajor combined together as a single long value. The versionCodeMajor is placed in the upper 32 bits.

But what is versionCodeMajor? How must I use it? What's the difference between versionCodeMajor and the old versionCode?

The documentation of it say nearlly nothing:

Internal major version code. This is essentially additional high bits for the base version code; it has no other meaning than that higher numbers are more recent. This is not a version number generally shown to the user, that is usually supplied with R.attr.versionName.


Solution

  • Up to now, the only way I've found to set versionCodeMajor with Android Studio 3.2.1 is through AndroidManifest.xml and disabling 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" [...]
    

    'cause setting it in build.gradle file you get

    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'
            }
        }
    }
    

    Could not find method versionCodeMajor() for arguments

    So, having set your chosen major version code number in AndroidManifest.xml and having disable InstantRun then you could get it in this way:

    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;
        }
    

    or you could use PackageInfoCompat like that:

    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;
        }
    

    And this should answer how to use it or a way to.

    For when and why using it... I guess it's up to you... as, as you pointed out, the documentation tells you nothing on why you should use it. What doc says it’s you should present to your users only the well known versionNumber.

    I guess they add some more bits to versionCode ‘cause they needed a greater number for their versioning ^^’

    That said, if you dind’t set the versionCodeMajor either in AndroidManifest.xml or in the build.gradle file (when it will handle it) or you set it to 0 then this value is identical to the old versionNumber deprecated field.