What Is Versioncodemajor? And What Is The Difference With Versioncode?
Solution 1:
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"?><manifestxmlns: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:
staticlonggetAppVersionCode(Context context)throws PackageManager.NameNotFoundException {
PackageInfopinfo= context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
longreturnValue= 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:
staticlonggetAppVersionCode(Context context)throws PackageManager.NameNotFoundException {
PackageInfopinfo= context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
longreturnValue= Long.MAX_VALUE;
returnValue = PackageInfoCompat.getLongVersionCode(pinfo);
if (BuildConfig.DEBUG) {
intmajorCode= (int)(returnValue >> 32);
intversionCode= (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.
Post a Comment for "What Is Versioncodemajor? And What Is The Difference With Versioncode?"