Skullper
Skullper

Reputation: 775

Variant Api android in Gradle 4.1

In previous version of gradle I have such code as below for generating outputFileName for my release apk.

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(outputPathName)
        }
    }

But after last gradle update this code is not working. I have read on official migrating docs that I should change my code to:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

And this is not working. This code just creates path in /home/pugman/AndroidStudioProjects/clickerapp/app/build/outputs/apk/release directory. Also I have read this text:

"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. This results in the plugin not knowing all of its outputs up front, but it also means faster configuration times."

Does this means that method above will not work?

Upvotes: 0

Views: 1126

Answers (2)

SanC
SanC

Reputation: 1

in your module build.gradle.kts

androidComponents.onVariants { variant ->
    variant.outputs.forEach { output ->
        if (output is com.android.build.api.variant.impl.VariantOutputImpl) {
            output.outputFileName.set("your file name")
        }
    }
}

Upvotes: 0

Nimrod Dayan
Nimrod Dayan

Reputation: 3100

As you said, the official migration docs states that there have been breaking changes introduced in 3.0.0. With that said, it also says clearly:

It still works for simple tasks, such as changing the APK name during build time

And indeed it does :) But I suspect that you're probably doing something wrong in your build.gradle. Anyways, notice that they prefix applicationVariants with android in the example:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

But, the prefix is not needed unless you're outside the android closure, e.g.:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    // More stuff here...
}

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

If this doesn't solve the issue then most likely you issue is somewhere else in your build.gradle.

Upvotes: 2

Related Questions