简体   繁体   中英

Get Android library module version number from inside demo app

I can programmatically get my app's version name like this :

String versionName = BuildConfig.VERSION_NAME; // 1.0

However, I have a demo app for a library I am making. In my project (since the library and the demo app are in the same project) I use

dependencies {
    implementation project(":mongol-library")
}

In other apps people would import it with

dependencies {
    implementation 'net.studymongolian:mongol-library:0.9.16'
}

In the demo app I would like to include the library's version name rather than the demo app version name. I could hard code the string, of course and update it every time that I updated the library, but that would be error prone. Is there a way to programmatically access a library module's version name?

Related questions

Use
String libVersionName = your.lib.package.name.BuildConfig.VERSION_NAME;

or in your library, just use BuildConfig.VERSION_NAME to get it.


NB:
BuildConfig.VERSION_NAME in app module is your.app.package.name.BuildConfig.VERSION_NAME

Somehow the other options with BuildConfig didn't work out for me. I ended up with this code:

context.getPackageManager().getPackageInfo("com.package.name", 0).versionName

For my library "com.package.name" was only the first part of the dependency from build.gradle:

implementation 'com.package.name:library:1.0.0'

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