How to get the application version

can anyone tell me how to get applcation version in android?

+47
android
Jun 03 2018-11-11T00:
source share
10 answers

This page has tips on how to do this from java:

PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo( context.getPackageName(), 0); String version = info.versionName; 

In addition, this link contains official information on how to properly configure the version of your application .

+130
Jun 03 2018-11-11T00:
source share

I am using Android Studio, I realized that I can use one line code to get this.

 /*version name*/ BuildConfig.VERSION_NAME /*version code*/ BuildConfig.VERSION_CODE 

Edited by:

If you use other Android libraries, make sure you import BuildConfig from your application package. This is similar to the automatically generated class R for identifying resources.

+66
Jul 06 '15 at 11:14
source share

To get information about the application:

 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 } 
+12
Jun 03 2018-11-11T00:
source share
 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; } } } 

Additional information on this

+12
Jun 13 2018-11-11T00:
source share

In you (module: application) gradle specify the version name and version code

 defaultConfig { applicationId "com.sing.smart" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.1.0" } 

Versioncode

VersionCode is an integer value used to easily distinguish between application versions.

Application developers should increase this value when releasing updates to their applications on the Android Market, so it can determine if users are using the old version of the application and offer them to update it.

versionName

VersionName is a string containing the usual "release version", as shown in other desktop applications, such as "1.4.5" or "3.7".

VersionName is simply a "human readable" version code.

 PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0); int versionNumber = pinfo.versionCode; String versionName = pinfo.versionName; 
+6
Jun 28 '16 at 5:20
source share

The simplest and best answer I've found is to simply import BuildConfig

 import your.package.BuildConfig; 

then just

 String verName = BuildConfig.VERSION_NAME; int verCode = BuildConfig.VERSION_CODE; 

cheers :)

+5
May 04 '17 at 5:36
source share

If you need it for scripts (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}' 
+1
Dec 04 '15 at 12:23
source share

int versionCode = BuildConfig.VERSION_CODE;

String versionName = BuildConfig.VERSION_NAME;

+1
Aug 17 '16 at 7:18
source share

If you are using eclipse try this:

  PackageInfo packageInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0); String version = packageInfo.versionName; 

In Android Studio:

 int versionCode = BuildConfig.VERSION_CODE; 

make sure you have the version code of the mansion in the build.gradle file at the module level.

+1
Sep 05 '17 at 10:41
source share

In Nativescript (using Typescript) you can do 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; } } 
0
Apr 13 '16 at 20:42 on
source share



All Articles