56

Can anyone tell me how to get the application version in Android?

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
nileshbirhade
  • 817
  • 2
  • 7
  • 14
  • 8
    possible duplicate of [How to get the build/version number of your Android application?](http://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application) – Joshua Pinter Feb 02 '15 at 21:54

13 Answers13

148

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.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • i m confuse about how to give update version to user ? Android market does it or we have to give user info about it please give me link documents about it – nileshbirhade Jun 03 '11 at 06:45
  • 1
    @nileshbirhade: Ask a separate stack overflow question about it. Or try googling first. That's how I got this answer – Merlyn Morgan-Graham Jun 03 '11 at 20:24
  • Someone attempted to edit the question to add exception handling, but simply swallowed the exception. It isn't clear to me in which cases the call to `getPackageInfo` will throw (somehow your application doesn't have a version specified?), and what is useful behavior in such a case. The edit didn't make that clear either, so I rejected it. – Merlyn Morgan-Graham Oct 24 '16 at 07:54
  • @MerlynMorgan-Graham actually, you HAVE to use a try-catch or it won't let you compile, it gives you an "unhandled exception: NameNotFoundException" at compile-time – Ali Bdeir Nov 04 '16 at 16:46
  • @AbAppletic ah, forgot about checked exceptions (fortunately for me). Feel free to edit it then, as long as a useful fallback behavior is added, and probably the appropriate logging instead of silent swallowing of the exception (like the blog post I linked does). – Merlyn Morgan-Graham Nov 04 '16 at 17:03
  • can we use this BuildConfig.VERSION_NAME ?? @MerlynMorgan-Graham – Poyyamozhi Ramakrishnan Dec 15 '21 at 16:41
  • @PoyyamozhiRamakrishnan I googled to find my answer, cause I was into trying to snatch up points w/ answers back in 2011. I think you can find your answer if you google. Also, you can just try it and see what happens. – Merlyn Morgan-Graham Jan 13 '22 at 23:14
74

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.

Jimmy Ilenloa
  • 1,989
  • 21
  • 21
14
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

Amir
  • 1,066
  • 1
  • 13
  • 26
Mitul Nakum
  • 5,514
  • 5
  • 35
  • 41
  • GET_META_DATA seems to only apply for ComponentInfo, not PackageInfo: http://developer.android.com/reference/android/content/pm/PackageManager.html#GET_META_DATA – Matthew Read Jul 20 '15 at 23:14
12

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
   }
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
10

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;
Pang
  • 9,564
  • 146
  • 81
  • 122
Meisam
  • 358
  • 5
  • 14
4

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

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
umi
  • 157
  • 1
  • 6
2

In your (Module: app) gradle define the version name and version code

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

versionCode

The versionCode is an 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;
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63
1

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}'
pkuszewski
  • 262
  • 1
  • 12
  • This is almost exactly what I need, except that you have to install the app to get this to work. I need something like this but without having to build the app at all. This is because I need the version number before my build happens (think to increment it) – kdbdallas Feb 08 '17 at 18:08
1

int versionCode = BuildConfig.VERSION_CODE;

String versionName = BuildConfig.VERSION_NAME;

KAMAL VERMA
  • 675
  • 7
  • 10
1

Java

String versionCode = String.valueOf(BuildConfig.VERSION_CODE);
String versionName = String.valueOf(BuildConfig.VERSION_NAME);

Kotlin

val versionCode = BuildConfig.VERSION_CODE
val versionName = BuildConfig.VERSION_NAME
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
1

If you are using the latest build gradle 8.0.0 or above in build.gradle (project level) like below

classpath 'com.android.tools.build:gradle:8.0.0'

First, add below code in build.gradle(app module) within android block, and rebuild Project

android {
    buildFeatures {
       buildConfig = true
    }
}

then you can get version code and version name programmatically from your activity or fragment as follows:

int versionCode = BuildConfig.VERSION_CODE;
String versionName = BuildConfig.VERSION_NAME;
Yosidroid
  • 2,053
  • 16
  • 15
0

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;
    }
}
Anderson Fortaleza
  • 2,449
  • 20
  • 22
0

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
    }
Jenis Kasundra
  • 583
  • 9
  • 19