Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change apk output folder in Gradle 4.1

I would like to change the APK output folder and this is what I used to do:

applicationVariants.all { variant ->
    variant.outputs.all {
        def filePath = "${rootProject.rootDir.absolutePath}/apks/${variant.name}"
        println("My Path: " + filePath)
        outputFileName = filePath
    }
}

However, it didn't work in Gradle 4.1 (Android studio 3.0 preview). Instead of generating the folder as the path above, it generated the above path inside old debug folder like image below:

enter image description here

Does anyone have a solution for this? Thanks.

like image 896
Kingfisher Phuoc Avatar asked Oct 28 '17 01:10

Kingfisher Phuoc


People also ask

Where does Gradle build APK?

By default, the Gradle build creates two . apk files in the build/outputs/apk folder.

Can I rename APK file?

You can just rename it just like any file in your computer even Michael. apk . And still all android phones will ignore the file names and go get the label at android:label="@string/app_name" so just copy the file and change it to anything you want and it works.

Which folder contains APK in Android Studio?

So the apk in Android studio is generated inside build folder of app module.


3 Answers

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

like image 161
Harry Han Avatar answered Nov 13 '22 02:11

Harry Han


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.

like image 22
azizbekian Avatar answered Nov 13 '22 03:11

azizbekian


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.

like image 22
Marta Rodriguez Avatar answered Nov 13 '22 04:11

Marta Rodriguez