简体   繁体   English

从演示应用程序内部获取 Android 库模块版本号

[英]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.或者在您的库中,只需使用BuildConfig.VERSION_NAME即可获取它。


NB:注意:
BuildConfig.VERSION_NAME in app module is your.app.package.name.BuildConfig.VERSION_NAME应用模块中的BuildConfig.VERSION_NAMEyour.app.package.name.BuildConfig.VERSION_NAME

Somehow the other options with BuildConfig didn't work out for me.不知何故,BuildConfig 的其他选项对我不起作用。 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:对于我的库“com.package.name”只是 build.gradle 依赖项的第一部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM