Android gradle apk splits, getting versionCode & versionName in code

Viewed 567

We start splitting APKs to reduce APK size. And we cannot figure out why the versionCode we get in code (Java) is always the default one.

Here is the relevant gradle:

defaultConfig {
    applicationId applicationIdPrefix
    versionCode 1000
    versionName "3.3.0"

// This is the configuration we want to publish
defaultPublishConfig "productionRelease"

applicationVariants.all { variant ->
  def apkName = "$projectName-${variant.name}-v${versionString}.${baseVersionCode}.apk"

  variant.outputs.each { output ->
    // Redefine the versionCode and the apkName to include ABI when available
    def abiName = output.getFilter(OutputFile.ABI)
    def baseAbiVersionCode = project.ext.abiCodes.get(abiName)

    if (baseAbiVersionCode != null) {
      output.versionCodeOverride = variant.versionCode + baseAbiVersionCode
      output.versionNameOverride = "$versionString (${output.versionCodeOverride})"

And this is how to retrieve the versionName and versionCode:

BuildConfig.VERSION_NAME
BuildConfig.VERSION_CODE

They are always 3.3.0 and 1000. Why? We want to get these values because we want to display them under settings.

1 Answers
Related