Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect my app's own android:versionCode at run time

Is there a way for my app to know the android:versionCode from AndroidManifest.xml or do I have to create a separate constant in one of my classes?

like image 433
700 Software Avatar asked Oct 06 '10 16:10

700 Software


People also ask

What is versionCode and versionName in Android?

The version code is an incremental integer value that represents the version of the application code. The version name is a string value that represents the “friendly” version name displayed to the users.

How do you find the version of an app on Android?

Once in Settings, tap Apps and notifications (or your phone manufacturer's name it). When you're in there, tap See all xyz apps (where xyz is the number of apps you have installed). Now scroll down until you find the app you want to find the version for. Then tap it in the list.


2 Answers

I put this in my subclassed android.app.Application but you can use it anywhere you have a context. Just change getPackageManager() to context.getPackageManager().

public int getVersion() {     int v = 0;     try {         v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;     } catch (NameNotFoundException e) {         // Huh? Really?     }     return v; } 
like image 176
Jere.Jones Avatar answered Sep 23 '22 02:09

Jere.Jones


Yes, you can access this information via PackageManager.getPackageInfo

Also, see details about versioning in the documentation

like image 23
uthark Avatar answered Sep 22 '22 02:09

uthark