7

Is there any way to know dynamically and programmatically the versionCode of an Android application??

I don´t know... maybe something like

 getApplicationContext.getVersionCode();

Thank you very much.

1

4 Answers 4

29

If you're using gradle (default in AndroidStudio) you can:

BuildConfig.VERSION_CODE
Sign up to request clarification or add additional context in comments.

Comments

3

You find the following from using this code

 public int getVersionCode() {
    int v = 0;
    try {
        v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {

    }
    return v;
}

Class Required

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

1 Comment

This is more dynamic, this should be the accepted answer.
1

This should be surrounded by a try...catch.

PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
version = pInfo.versionName;
versionCode = pInfo.versionCode;

Comments

1

try this

BuildConfig.VERSION_CODE

and import

import com.<your_pakcage_name>.BuildConfig;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.