2

According to my CI requirements I'm trying to achieve two things:

  • set apk file name in format: .apk
  • set folder for generated .apk file: app/build/outputs/apk/myBuild.apk

I used android gradle plugin 2.3 and got following script to manage task above (it worked correctly for gradle plugin 2.3):

applicationVariants.all {
        variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(
                        output.outputFile.parent,
                        variant.productFlavors.get(0).name + variant.buildType.name + ".apk")
            }

Now I got the following code (according to latest updates in android gradle plugin 3.0):

applicationVariants.all { variant ->
        variant.outputs.all { output ->
            logger.debug("fileNameParent", output.outputFile.parent)
            outputFileName = new File(
                    "apk/",
                    outputFileName.replace(".apk", "MyBuild.apk"))
        }

And got following structure (see picture):

enter image description here

P.S. I read Android Gradle Plugin migration guide, but it doesn't help me to achieve my goal :(

Taxist Samael
  • 1,157
  • 1
  • 9
  • 23

2 Answers2

3

The first part of the question: how to generate filename.

applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File("${variant.productFlavors.get(0).name}${variant.buildType.name}.apk")
        }

The second part of the question: how to set path.

According to documentation it's impossible:

Using the Variant API to manipulate variant outputs is broken with the new plugin. It still works for simple tasks, such as changing the APK name during build time... However, more complicated tasks that involve accessing outputFile objects no longer work. That's because variant-specific tasks are no longer created during the configuration stage.

Taxist Samael
  • 1,157
  • 1
  • 9
  • 23
-1

this is the correct code:

applicationVariants.all { variant ->
    variant.outputs.all{
        outputFileName = outputFileName.replace("app-${variant.buildType.name}.apk", ${variant.productFlavors.get(0).name} + "-" + ${variant.buildType.name} + ".apk")
    }
}

please keep in mind that you have to put this code in build.gradle app file on android{ } section, and not on android{ buildTypes{ debug/release{ }}} sections

This will return for:

debug build:

app-debug.apk >> YOUR_PRODUCT_NAME-debug.apk

release build:

app-release.apk >> YOUR_PRODUCT_NAME-release.apk

don't use outputFileName = new File() or you have the problem of variant API manipulation

  • Why should I replace file Instead of creating file? I've tested code above and it's working as expected. – Taxist Samael Nov 16 '17 at 12:41
  • I've tested code above and it's working as expected. Please explain which manipulation do you mean. – Taxist Samael Nov 16 '17 at 12:47
  • Hi @SlavaSotone I mean variant API manipulations (of gradle plugin v3.0.0) that you linked in your post: [link](https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html) – Luca Bognolo Nov 24 '17 at 18:24