12

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

reixa
  • 6,903
  • 6
  • 49
  • 68
  • possibly related: https://stackoverflow.com/questions/54206898/variantoutput-getpackageapplication-is-obsolete – Martin Zeitler Jan 16 '19 at 00:33
  • 1
    Have you tried `${variant.getFlavorName()}.apk` or `variant.baseName`? – GensaGames Oct 09 '19 at 19:36
  • variant.baseName it´s the same as variant.buildType.name, it prints "release". But variant.getFlavorName() did the job. Also, variant.flavorName works too but had a little problem with the other part of the gradle file and didn't work, now it does. Thanks. – reixa Oct 10 '19 at 06:50
  • @GensaGames if you post the answer I will gladly give you the bounty – reixa Oct 10 '19 at 06:51
  • Hi @axierjhtjz see this comment https://stackoverflow.com/a/58373100/2448013 – subrahmanyam boyapati Oct 14 '19 at 08:52
  • @GensaGames could you please post your comment as an answer? It´s the first correct response to the question I made. – reixa Oct 15 '19 at 09:12
  • 1
    @axierjhtjz Sorry for busy with workplace change) glad you have resolved your issue) – GensaGames Oct 16 '19 at 16:12

7 Answers7

18

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.

Sergio
  • 451
  • 3
  • 11
4

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

    }
}
Kaushal Panchal
  • 1,785
  • 1
  • 11
  • 27
2

You can do it like this:

defaultConfig {
    ...

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

}
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • Thanks, could be an option but I also like to have the flavor on my apk name. Would like to maintain the previous apk name structure. – reixa Jan 15 '19 at 15:20
1

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

GensaGames
  • 5,538
  • 4
  • 24
  • 53
0

Can you try below.

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

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)
    }
  }
Arul
  • 1,031
  • 13
  • 23
0
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

subrahmanyam boyapati
  • 2,836
  • 1
  • 18
  • 28