Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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 ?

like image 650
Someone Somewhere Avatar asked May 05 '15 18:05

Someone Somewhere


1 Answers

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.

like image 56
Bryan Herbst Avatar answered Nov 14 '22 21:11

Bryan Herbst