简体   繁体   中英

Retrieve the app package and version in Android

I'm implementing an error report system in my app,

how can I retrieve programmatically the package of my app and its version?

Thanks

The PackageInfo class has a versionCode field that will give you the version, the versionName field will give you the version name, and packageName that will give you the package name. Context will give you a PackageManger instance that will give you a PackageInfo instance.

String packageName = "exampleApp";
try {
    PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
    int versionNumber = pi.versionCode;
    String versionName = pi.versionName;
    String packageName = pi.packageName;
} catch (NameNotFoundException e) {

    e.printStackTrace();
}

Use the following:

Context.getApplicationContext.getApplicationInfo().packageName;

This returns a string.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM