简体   繁体   中英

How to get Version from AndroidManifest.xml in a Library Project?

How to get Version from AndroidManifest.xml in a Library Project? Because there, I then don't have any activity in it.

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();
}

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.

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