Change apk output folder in Gradle 4.1

梦想的初衷 提交于 2019-12-02 20:35:06

This is a workaround to keep the output path same after upgrade to gradle 4.x.

applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "../" + outputFileName
    }
}

now apk is generated at platforms/android/build/outputs/apk/android-release.apk

From migration guide:

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, as shown below:


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

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.

I had a similar issue, because I needed the output apk in a known folder and not in a folder depending on the computer user name. So I have fixed like this:

applicationVariants.all { variant ->
    variant.outputs.all {
        def apk = output.outputFile;
        def newName = apk.name.replace(".apk", "-v" + variant.versionName + "-RELEASE.apk");
        newName = newName.replace("-" + variant.buildType.name, "");

        outputFileName = "./" + newName
    }
}

With this I get the apk in: ".../outputs/apk/flavorName/buildTypeName/xxx.apk"

Hope it helps you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!