Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 348
axierjhtjz Avatar asked Jan 15 '19 10:01

axierjhtjz


2 Answers

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.

like image 168
Sergio Avatar answered Sep 20 '22 06:09

Sergio


Using setProperty method you can rename your .apk name.

You can do this.

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.expertBrains.abc"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 75
        versionName "2.6.15"
        multiDexEnabled true
        setProperty("archivesBaseName", "(" + versionName + ") "+ new Date().format( 'yyyy-MM-dd HH:mm' ))

    }
}
like image 39
Kaushal Panchal Avatar answered Sep 20 '22 06:09

Kaushal Panchal