简体   繁体   中英

How to read a library's versionName from build.gradle?

I want to log and report (in errors) what version of my library is being used. How is it possible to programmatically read android.defaultConfig.versionName from build.gradle of my library ?

I have tried:

PackageInfo packageInfo = applicationContext.getPackageManager()
    .getPackageInfo("com.my.library",0);`

Where com.my.library is the package name of my library.

However, this results in a NameNotFoundException .

Any ideas ?

You can access the version name of library projects using the library project's BuildConfig.VERSION_NAME constant.

For example, you can get the version name of the v7 support library via android.support.v7.appcompat.BuildConfig.VERSION_NAME .

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