什么是 versionCodeMajor?和 versionCode 有什么区别?
What is versionCodeMajor? And what is the difference with versionCode?
我刚发现 PackageInfo.versionCode
is deprecated in Android Pie. They point you to use PackageInfo.getLongVersionCode()
。这个新方法的 JavaDoc 是:
Return versionCode
and versionCodeMajor
combined together as a single long value. The versionCodeMajor
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;
}
这应该回答如何使用它或一种方法。
何时以及为何使用它...我想这取决于您...正如您指出的那样,文档没有告诉您为什么应该使用它。文档说你应该只向你的用户展示众所周知的版本号。
我猜他们在 versionCode 中添加了更多位,因为他们需要更多的数字来进行版本控制 ^^'
就是说,如果您没有在 AndroidManifest.xml
或 build.gradle
文件中设置 versionCodeMajor
(当它处理它时),或者您将其设置为 0
则此值与旧的 versionNumber
弃用字段相同。
另一种获取版本名称和版本代码的方法是通过 BuildConfig
BuildConfig.VERSION_CODE
BuildConfig.VERSION_NAME
我刚发现 PackageInfo.versionCode
is deprecated in Android Pie. They point you to use PackageInfo.getLongVersionCode()
。这个新方法的 JavaDoc 是:
Return
versionCode
andversionCodeMajor
combined together as a single long value. TheversionCodeMajor
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;
}
这应该回答如何使用它或一种方法。
何时以及为何使用它...我想这取决于您...正如您指出的那样,文档没有告诉您为什么应该使用它。文档说你应该只向你的用户展示众所周知的版本号。
我猜他们在 versionCode 中添加了更多位,因为他们需要更多的数字来进行版本控制 ^^'
就是说,如果您没有在 AndroidManifest.xml
或 build.gradle
文件中设置 versionCodeMajor
(当它处理它时),或者您将其设置为 0
则此值与旧的 versionNumber
弃用字段相同。
另一种获取版本名称和版本代码的方法是通过 BuildConfig
BuildConfig.VERSION_CODE
BuildConfig.VERSION_NAME