什么是versionCodeMajor?和versionCode有什么区别? [英] What is versionCodeMajor? And what is the difference with versionCode?

查看:110
本文介绍了什么是versionCodeMajor?和versionCode有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

返回 versionCode versionCodeMajor 组合在一起作为单个long值. versionCodeMajor 放在上方位.

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

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

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:

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

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.

推荐答案

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

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" [...]

'将其设置在您获得的 build.gradle 文件中

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

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

Could not find method versionCodeMajor() for arguments

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

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

,或者您可以这样使用 PackageInfoCompat :

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.

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

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.

我想他们会在versionCode中增加一些位,因为他们需要更多的版本来进行^^

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

也就是说,如果您没有在 AndroidManifest.xml build.gradle 文件中设置 versionCodeMajor 将对其进行处理),或者将其设置为 0 ,则此值与旧的 versionNumber 弃用字段相同.

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.

这篇关于什么是versionCodeMajor?和versionCode有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆