Android BuildConfig.VERSION_NAME返回null?

Spe*_*ake 6 android

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

    defaultConfig {
    applicationId "com.maplesyrupindustries.j.airportmeet"
    minSdkVersion 19
    targetSdkVersion 24
    versionCode 7
    versionName "1.0.6"
    multiDexEnabled true
}
Run Code Online (Sandbox Code Playgroud)

我在登录名的onCreate中调用它:

    String build = BuildConfig.VERSION_NAME;
    Log.e(TAG, BuildConfig.VERSION_NAME);
    tvVersion.setText("Alpha " + build);
Run Code Online (Sandbox Code Playgroud)

但是,构建字符串始终为空。是什么赋予了?

Abh*_*tel 10

请试试这个

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

  • 我知道这是另一个解决方案,但我很好奇为什么 BuildConfig.VERSION_NAME 为空? (3认同)

Kes*_*era 5

BuildConfig.VERSION_NAME is not working 
1). In Case when you import Wrong Package Please check your import Package 
2). Please import your application package & solve this issue permanently


  PackageInfo pInfo = null;
  try {
       pInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }   

 String versionName = pInfo.versionName;//Version Name
 int versionCode = pInfo.versionCode;//Version Code

 Log.e("keshav"," VERSION_NAME -->" + versionName +"versionCode -> "+versionCode);
Run Code Online (Sandbox Code Playgroud)

输出

E/keshav:  VERSION_NAME -->2.0.4  versionCode -> 26
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述