android-libraryandroid-package-managersversion-numbering

How to get the library module's version from inside the module's code at runtime


Having a Android library module, its version is defined in the build.gradle

group = 'com.sample.data'
version = 1.0.1
project.ext.artifactId = 'data_module'

apply plugin: 'com.android.library'

Inside its code when do log or reporting the crash it needs to include itself's version (the 1.0.1).

using

val pInfo = context.getPackageManager()
                  .getPackageInfo(dataConfiguration.context.getPackageName(), 0)
val version = pInfo.versionName

only gets the hosting app's package info, not the library module's

How to get the library module's version from inside the module's code at runtime?

some post suggest to use the BuildConfig.VERSION_NAME, but in the library module's code it cannt not be resolved (the list does not have the library itself)

enter image description here


Solution

  • After adding same extra stuff in build.gradle's

    defaultConfig {
    
    }
    

    in the code the BuildConfig.VERSION_NAME is resolved.

    It can get the version from BuildConfig.VERSION_NAME.