[英]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 我正在使用下面的代码在MyApplication类中以编程方式获取版本名称和版本代码,并将它们声明为静态成员
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: 我的应用程序gradle文件是:
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. 它在调试模式下显示正确的版本名称(0.95-DEBUG)和版本代码(15),但在发行模式下显示旧版本名称(0.85)和版本代码(11)。
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. 当您要获取信息时,其中YOUR_MODULE_PACKAGE_NAME是模块的名称。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.