Android BuildConfig.VERSION_NAME 返回空值?

标签 android

我只想在我的登录屏幕上显示我的版本名称,但是,它总是返回 null。我在我的应用程序的 gradle 构建中定义了我的版本名称,如下所示:

    defaultConfig {
    applicationId "com.maplesyrupindustries.j.airportmeet"
    minSdkVersion 19
    targetSdkVersion 24
    versionCode 7
    versionName "1.0.6"
    multiDexEnabled true
}

我在登录的 onCreate 中调用它:

    String build = BuildConfig.VERSION_NAME;
    Log.e(TAG, BuildConfig.VERSION_NAME);
    tvVersion.setText("Alpha " + build);

然而,构建字符串始终为空。给了什么?

最佳答案

请试试这个

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;//Version Name
int verCode = pInfo.versionCode;//Version Code

关于Android BuildConfig.VERSION_NAME 返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38872907/

相关文章:

java - 加密应用程序数据的正确方法

Android seekbar如何在短时间内阻止拖动/禁用它

android - 无法将自定义拇指定位在搜索栏的中心

Android直播视频流延迟

android - android中的contentprovider和contentResolver有什么区别

android - 按下后退并重新打开时, fragment 不会在 viewpager 中恢复其状态

java - 当 minifyEnabled 为 true 时 Android 应用程序崩溃

java - 找不到类 'dalvik.system....'

android - EAS 构建失败,错误代码为 1,但 Expo Build 成功,我需要 EAS 构建来删除未使用的包,如 com.segment.analytics.android :analytics

android - 错误 : Unable to resolve dependency for ':react-native-maps@debug/compileClasspath' : Could not resolve androidx. appcompat :appcompat:1. 0.0