Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the app package and version in Android

Tags:

java

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

like image 988
Addev Avatar asked Mar 18 '26 21:03

Addev


2 Answers

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();
}
like image 140
Phil Avatar answered Mar 20 '26 11:03

Phil


Use the following:

Context.getApplicationContext.getApplicationInfo().packageName;

This returns a string.

Hope this helps!

like image 33
Codeman Avatar answered Mar 20 '26 11:03

Codeman