reixa
reixa

Reputation: 7011

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

Upvotes: 12

Views: 6717

Answers (7)

GensaGames
GensaGames

Reputation: 5788

As mentioned in the comments, the right way was to use ${variant.getFlavorName()}.apk or variant.baseName.

Upvotes: 1

Sergio
Sergio

Reputation: 461

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.

Upvotes: 19

subrahmanyam boyapati
subrahmanyam boyapati

Reputation: 2888

applicationVariants.all { variant ->
        variant.outputs.all {
            def appName = "AppName"
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def newName = "${appName}${defaultConfig.versionName}_${buildType}.apk"
            outputFileName = newName
        }
    }

Below code will generate apk file name as

AppName1.2.0_buildType.apk

Upvotes: 0

Arul
Arul

Reputation: 1209

I hope this could help. I can't say this is the best way but it works.

productFlavour{
    uat {
        versionName "2.8.74"
        buildConfigField("String", "ENVIRONMENT", '"uat"')
        setProperty("archivesBaseName", "iotg-uat-v" + versionName)
    }

    staging {
        versionName "2.9.4"
        buildConfigField("String", "ENVIRONMENT", '"staging"')
        setProperty("archivesBaseName", "iotg-staging-v" + versionName)
    }
  }

Upvotes: 0

Kaushal Panchal
Kaushal Panchal

Reputation: 1825

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' ))

    }
}

Upvotes: 4

user10917361
user10917361

Reputation:

You can do it like this:

defaultConfig {
    ...

    project.ext.set("archivesBaseName", applicationId + "_V" + versionName + "("+versionCode+")_" + new Date().format('dd-MM mm'));

}

Upvotes: 2

ma34s
ma34s

Reputation: 11

Can you try below.

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

Upvotes: 0

Related Questions