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

3

42

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?

Maharashtra answered 6/10, 2010 at 16:29 Comment(0)
P
93

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;
}
Pattiepattin answered 6/10, 2010 at 16:35 Comment(0)
B
1

Yes, you can access this information via PackageManager.getPackageInfo

Also, see details about versioning in the documentation

Ballplayer answered 6/10, 2010 at 16:33 Comment(0)
A
1

You can get version code simply from <package_name>.BuildConfig#VERSION_CODE reference. It is generated by ADT plugin/Intellij.

Abernathy answered 28/3, 2016 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.