3

I am creating an AAR library, In the arr manifest we have version name, and version code. How can I get this version. I don't want the main app version. I just want the library version.

Is there a way to get this version?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shrikanth Kalluraya
  • 1,099
  • 1
  • 16
  • 34

2 Answers2

5

Step #1: Define those values as resources.

Step #2: Adjust your manifest to reference those resources.

Step #3: Refer to those values from the rest of your app via those resources.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the help.. but now version is set from build.gradle file.. how will i reference strings value from build.gradle – Shrikanth Kalluraya May 19 '16 at 20:31
  • @ShrikanthKalluraya: Define a constant in the `build.gradle` file (or in `gradle.properties`). Use that both for your version information and for `resConfig` statements that defines the resources. Or, use the constant for `buildConfigField` statements that add fields to `BuildConfig`, if you only need to refer to the values in Java code. – CommonsWare May 19 '16 at 20:39
  • Can I use BuildConfig to define app version in build.gradle? Sorry I have never done this. I want same value to be used in java and build.gradle. Can you please guide me? – Shrikanth Kalluraya May 19 '16 at 23:53
  • No, but you can use the same value in build.gradle (or gradle.properties) to fill in both buildConfigField and versionCode, for example. – CommonsWare May 20 '16 at 00:54
  • @CommonsWare Is there perhaps some gradle trick to get the dependency version, so that you could get it at runtime? Something that could even work when you write it in the Android-library itself? I asked about this here: https://stackoverflow.com/q/70460845/878126 – android developer Dec 23 '21 at 12:53
0

I just tested this answer on our AAR libraries and it is working fine to get the versionName and versionCode defined in the build.gradle of your AAR lib. It appears to be the easiest solution. Basically:

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

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

For versionCode, use: int libVersionCode = your.lib.package.name.BuildConfig.VERSION_CODE;

wilos
  • 21
  • 3