Reputation: 995
I'd like to simply display my version name on my login screen, however, it's always returning null. I've defined my versionName in my app's gradle build as follows:
defaultConfig {
applicationId "com.maplesyrupindustries.j.airportmeet"
minSdkVersion 19
targetSdkVersion 24
versionCode 7
versionName "1.0.6"
multiDexEnabled true
}
And I am calling it in my login's onCreate:
String build = BuildConfig.VERSION_NAME;
Log.e(TAG, BuildConfig.VERSION_NAME);
tvVersion.setText("Alpha " + build);
Yet, the build string is always empty. What gives?
Upvotes: 15
Views: 19529
Reputation: 11264
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);
Output
E/keshav: VERSION_NAME -->2.0.4 versionCode -> 26
Upvotes: 5
Reputation: 126563
Using BuildConfig.VERSION_NAME
will return null like other values like:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.mypackage.myapp";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
}
The reason, because the file that contains this values BuildConfig.java
, doesn´t exist until you compile the proyect, in fact the file BuildConfig.java
is created inside folder \build
, specifically \build\generated\source\buildConfig\debug\....
Like a better option to get versionName
use this method:
public String getVersionName(Context ctx){
try {
return ctx.getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
Upvotes: 1
Reputation: 4328
Please try this
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String version = pInfo.versionName;//Version Name
int verCode = pInfo.versionCode;//Version Code
Upvotes: 14