简体   繁体   中英

Getting wrong version code and version name in android app release version

I am using the code below to get version name and version code programmatically in MyApplication class and I declared them as static members

public class MyApplication extends Application {
   Public static String MY_VERSION_NAME;
   Public static int MY_VERSION_NAME;  
    @Override
    public void onCreate() {
       getPackageInfo();
    }  
    private void getPackageInfo() {
        Context context = getApplicationContext();
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            MY_VERSION_NAME = info.versionName;
            MY_VERSION_CODE = (int) PackageInfoCompat.getLongVersionCode(info);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            myversionName = "Unknown-01";
        }
   } 
}

and my app gradle file is:

android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
    applicationId "com.example"
    minSdkVersion 19 
    targetSdkVersion 28
    versionCode 15
    versionName "0.95"
    vectorDrawables.useSupportLibrary = true
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
    debug {
        applicationIdSuffix '.debug'
        versionNameSuffix '-DEBUG'
    }
}
lintOptions {
    disable 'MissingTranslation'
}


compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}}

It shows correct version name (0.95-DEBUG) and version code (15) in debug mode but It shows old version name (0.85) and version code (11) in release mode.

how can I solve this issue? I have tried it on many devices and the result is the same.

EDIT: the problem exists in the setting>app>app info too.

Instead of use

PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            MY_VERSION_NAME = info.versionName;
            MY_VERSION_CODE = (int) PackageInfoCompat.getLongVersionCode(info);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            myversionName = "Unknown-01";
        }

Use

BuildConfig.VERSION_NAME
BuildConfig.VERSION_CODE

*Edit

Where YOUR_MODULE_PACKAGE_NAME is the name of module when you want get info.

Remember if you pass the Context of your Application you will get info on that module

PackageInfo packageInfo = getPackageManager().getPackageInfo(YOUR_MODULE_PACKAGE_NAME, 0);
packageInfo.versionName;
packageInfo.versionCode;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM