Bra*_*bin 4 android android-9.0-pie
我刚刚发现PackageInfo.versionCode在Android Pie中已弃用.他们指向你使用PackageInfo.getLongVersionCode().这种新方法的JavaDoc是:
返回
versionCode并versionCodeMajor组合在一起作为单个long值.在versionCodeMajor被放置在高32位.
但是什么versionCodeMajor呢?我该如何使用它?versionCodeMajor和旧有versionCode什么区别?
它的文档几乎没有说:
内部主要版本代码.这基本上是基本版本代码的额外高位; 没有其他意义,因为更高的数字是最近的.这不是通常向用户显示的版本号,通常由R.attr.versionName提供.
到目前为止,我发现versionCodeMajor使用Android Studio 3.2.1 设置的唯一方法是通过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" [...]
Run Code Online (Sandbox Code Playgroud)
'因为你在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'
}
}
}
Run Code Online (Sandbox Code Playgroud)
无法为参数找到方法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;
}
Run Code Online (Sandbox Code Playgroud)
或者您可以像这样使用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;
}
Run Code Online (Sandbox Code Playgroud)
这应该回答如何使用它或一种方式.
关于何时以及为何使用它......我想这取决于你...正如你所指出的那样,文档会告诉你为什么要使用它.什么文档说你应该向你的用户呈现众所周知的versionNumber.
我猜他们会向versionCode添加更多位,因为他们需要更多的版本来进行版本化^^'
也就是说,如果您没有在文件versionCodeMajor中AndroidManifest.xml或build.gradle文件中设置它(当它将处理它时)或者您将其设置为,0那么此值与旧的versionNumber弃用字段相同.
| 归档时间: |
|
| 查看次数: |
1429 次 |
| 最近记录: |