Search code examples
androidgradlegradle-pluginandroid-studio-3.3

Apk rename with the Gradle plugin v4.10.1


I´ve updated my Android Studio today to the 3.3 version which came with Gradle plugin version 4.10.1.

Previously, my build.gradle was renaming my apk´s with this code to the following structure:

app-{buildType[release|debug]}-{flavor[prod|stage]}-{versionName[1.2.4]-{versionCode[43]}.apk

app-release-prod-1.1.4-45.apk.

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = output.outputFile.name.replace(".apk", "-${variant.versionName}-${variant.versionCode}.apk").replace("-unsigned", "")
    }
}

But I got this error after updating.

WARNING: API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variantOutput.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: app

The problem is at output.outputFile.name since you can't access output data on this plugin version.

So far I´ve tried this approach without success.

applicationVariants.all { variant ->
    variant.flavors*.name.all { flavor ->
        outputFileName = "${flavor}-${variant.buildType.name}-${variant.versionName}-${variant.versionCode}.apk".replace("-unsigned", "")
    }
}

Any idea?

=======================================================

UPDATE

I took a retake on this matter, I´ve tried the following snippet, but I'm having issues retrieving the flavor of that variant.

android.applicationVariants.all { variant ->
    def flavor = variant.flavorName
    variant.outputs.all { output ->
        def builtType = variant.buildType.name
        def versionName = variant.versionName
        def versionCode = variant.versionCode
        outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
    }
}

outputs: app--release-1.0.4-88.apk

Thanks


Solution

  • Try this:

    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def builtType = variant.buildType.name
            def versionName = variant.versionName
            def versionCode = variant.versionCode
            def flavor = variant.flavorName
            outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
        }
    }
    

    This outputs the following apk name : app-release-myFlavor-0.0.1-1.apk.