get Apk Version Code - Android App

Android examples for App:APK Information

Description

get Apk Version Code

Demo Code


//package com.java2s;

import android.content.Context;

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

public class Main {
    public static int getApkVersionCode(Context context, String apkPath) {

        if (context == null || apkPath == null) {
            return -1;
        }//  w  w  w . jav  a  2s.  com

        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath,
                PackageManager.GET_ACTIVITIES);
        if (info != null) {
            ApplicationInfo appInfo = info.applicationInfo;

            return info.versionCode;
        }

        return -1;
    }
}

Related Tutorials