How do I get my application Version
AndroidAndroid Problem Overview
Can anyone tell me how to get applcation version in Android?
Android Solutions
Solution 1 - Android
This page has a tips on how to do it from java:
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(
context.getPackageName(), 0);
String version = info.versionName;
Also, this link has official information on how to properly set up your application versioning.
Solution 2 - Android
I am using Android Studio, I realized I could use one line code to get this.
/*version name*/
BuildConfig.VERSION_NAME
/*version code*/
BuildConfig.VERSION_CODE
Edited:
If you are using other android libraries, make sure you import BuildConfig from your application package. This is similar to the automatically generated R class for identifying resources.
Solution 3 - Android
public int getVersion(Context context) {
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return pInfo.versionCode;
} catch (NameNotFoundException e) {
return 0;
}
}
}
More info on this link
Solution 4 - Android
To get application information:
PackageManager manager = this.getPackageManager();
try {
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
String packageName = info.packageName;
int versionCode = info.versionCode;
String versionName = info.versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
}
Solution 5 - Android
The easiest and best answer I found is to just import your BuildConfig
import your.package.BuildConfig;
then just
String verName = BuildConfig.VERSION_NAME;
int verCode = BuildConfig.VERSION_CODE;
Solution 6 - Android
If you are using eclipse try this:
PackageInfo packageInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
String version = packageInfo.versionName;
In Android Studio :
int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;
make sure you have mansion version code in your module-level build.gradle file
Solution 7 - Android
In you (Module : app) gradle define version name and version code
defaultConfig {
applicationId "com.sing.smart"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.1.0"
}
versionCode
The versionCode is integer value used to easily differentiate between app versions.
App developers must increment this value when they release updates to their apps in Android Market, so it can determine if users are using an old version of the app, and offer them to update it.
versionName
The versionName is a string containing a regular “release version” as seen in other desktop applications, such as “1.4.5” or “3.7”.
The versionName is just a “human readable” version code.
PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;
String versionName = pinfo.versionName;
Solution 8 - Android
If you need it for scripting purposes (not for programming) you can use this command:
adb shell "pm dump com.example.your.package.name | grep \"versionCode\"" | awk '{print $1}' | awk -F '=' '{print $2}'
Solution 9 - Android
int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;
Solution 10 - Android
On Nativescript (with Typescript) one can do like this:
import {Observable} from 'data/observable';
import * as applicationModule from 'application'
export class AboutViewModel extends Observable {
public version: string;
constructor() {
super();
let manager = applicationModule.android.context.getPackageManager();
let info = manager.getPackageInfo(applicationModule.android.context.getPackageName(), 0);
this.version = info.versionName;
}
}
Solution 11 - Android
In kotlin :
fun getAppVersionName(context: Context): String {
var appVersionName = ""
try {
appVersionName =
context.packageManager.getPackageInfo(context.packageName, 0).versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return appVersionName
}
fun getAppVersionCode(context: Context): Long {
var appVersionCode = 0L
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
appVersionCode =
context.packageManager.getPackageInfo(context.packageName, 0).longVersionCode
}
else{
appVersionCode =
context.packageManager.getPackageInfo(context.packageName, 0).versionCode.toLong()
}
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
return appVersionCode
}