How to get Version from AndroidManifest.xml in a Library Project? Because there, I then don't have any activity in it.
Asked
Active
Viewed 3,510 times
3
-
I've linked two duplicates, depending on whether you mean the _app's_ version number or the _library's_ version number. – Ryan M Feb 25 '23 at 03:56
2 Answers
1
PackageManager m = getPackageManager();
String app_ver = "";
try {
app_ver = m.getPackageInfo(this.getPackageName(), 0).versionName;
}
catch (NameNotFoundException e) {
// Exception won't be thrown as the current package name is
// safe to exist on the system.
throw new AssertionError();
}

Briareos386
- 1,927
- 18
- 34

Sachin Gurnani
- 2,444
- 7
- 36
- 45
-
which class "this" should extends? this is a simple manager which is supposed to return the version of the app. – dragda May 22 '12 at 13:59
-
So, my class now extends ContextWrapper in order to be able to get "getPackagemanager()". thx for the tip. – dragda May 29 '12 at 15:22
-
3How does this work for a standalone library, which doesn't have any context of its own, but has its own version code in its Android Manifest. – bianca Feb 25 '13 at 23:14
1
If you want the specific version of the library (and not the app using that library), use:
BuildConfig.VERSION_NAME
BuildConfig.VERSION_CODE
Just make sure that you import the BuildConfig for your library (as literally every android library/app has it's own BuildConfig on it's package path). The Android Studio's Gradle Build system indicates that this is always generated as of version 0.7.0 (see changelog). This persists as a generated class into the hosting app/library.

JCricket
- 1,318
- 2
- 17
- 34