android – 什么是versionCodeMajor?和versionCode有什么区别?
作者:互联网
我刚刚发现在Android Pie中不推荐使用PackageInfo.versionCode
.他们指出你使用PackageInfo.getLongVersionCode()
.这种新方法的JavaDoc是:
Return 07002 and 07003 combined together as a single long value. The 07003 is placed in the upper 32 bits.
但是什么是versionCodeMajor?我该如何使用它? versionCodeMajor和旧的versionCode有什么区别?
它的文档几乎没有说:
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.
<?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'
}
}
}
Could not find method versionCodeMajor() for arguments
因此,在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;
}
这应该回答如何使用它或一种方式.
关于何时以及为何使用它……我想这取决于你…正如你所指出的那样,文档会告诉你为什么要使用它.什么文档说你应该向你的用户呈现众所周知的versionNumber.
我猜他们会向versionCode添加更多位,因为他们需要更多的版本来进行版本化^^’
也就是说,如果你没有在AndroidManifest.xml或build.gradle文件中设置versionCodeMajor(当它处理它时),或者你将它设置为0,那么这个值与旧的versionNumber弃用字段相同.
标签:android-9-0-pie,android 来源: https://codeday.me/bug/20190823/1697213.html