Keitaro Urashima
Keitaro Urashima

Reputation: 1001

Get version_name from the version_code of an android package?

Is there any way to get the version_name based on the version_code of an android package?

For example: 'com.nianticlabs.pokemongo' version_code: 2017121800 => version_name: 0.87.5

all I want is something like:

function getVersionName(version_code) {
   // do smt with version_code
   return version_name;
}

Upvotes: 2

Views: 1039

Answers (3)

Nick Fortescue
Nick Fortescue

Reputation: 13836

No this is not possible in general. Every app is completely free to choose it's own version name (user readable string) and version code scheme. Many apps will have two APKs which have different version codes with exactly the same version name. See the docs.

Upvotes: 0

AdricoM
AdricoM

Reputation: 579

But I don't think you can get one depending on the other, those are two separate things: only a string and an int

In native java you have:

public static int getVersionCode(Context context) {
    try {
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return pInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        return -1;
    }
}

public static String getVersionName(Context context) {
    try {
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return pInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        return "";
    }
}

you could look for the equivalent in your javascript google API

Upvotes: 2

Ege Kuzubasioglu
Ege Kuzubasioglu

Reputation: 6282

Easiest way to get version name:

 private String getVersionName() {

            versionName = BuildConfig.VERSION_NAME;
            return versionName;
        }

Upvotes: 1

Related Questions