1

Prior to Android plugin version 3.0.0-alpha4, I have been using the following for publishing different variants of my APKs to a specific file path:

def publish = project.tasks.create("publishAll")
android.applicationVariants.all { variant ->
  def task = project.tasks.create("publish${variant.name}Apk", Copy)
  task.from(variant.outputs[0].outputFile)
  task.into(buildDir)

  task.dependsOn variant.assemble
  publish.dependsOn task
}

I originally got it from this answer from Xavier Ducrohet: Copying APK file in Android Gradle project

As of the new updates to Android Studio Preview which uses version 3.0.0-alpha4, variant.outputFile is deprecated. What is the new suggested way to achieve something like this?

EDIT: Looks like there is no way to currently access the variant output file as pointed out here: https://developer.android.com/studio/preview/features/new-android-plugin-migration.html#variant_api

Looks like we'll have to wait until they introduce those apis

ElectronAnt
  • 2,115
  • 7
  • 22
  • 39
  • Seems it's not related to the new version. Have you checked this https://stackoverflow.com/questions/25997866/gradle-warning-variant-getoutputfile-and-variant-setoutputfile-are-deprecat ? – romtsn Jun 19 '17 at 07:17
  • Why do you think that `variant.outputFile` is deprecated? I'm using the same setup and I see no issues. – azizbekian Jun 19 '17 at 07:31

1 Answers1

7

If you don't use abi splits next snippet works

project.afterEvaluate {
    android.applicationVariants.all { variant ->
        // create file where to copy 
        def backupFolder = rootProject.file("backup")
        def backupFile = new File(backupFolder, String.format("%s_v%s.%d.apk", variant.flavorName, variant.versionName, variant.versionCode))

        variant.outputs.all { output ->
            Task copyAndRenameAPKTask = project.task("copyAndRename${variant.name.capitalize()}APK", type: Copy) {
                from output.outputFile.getParent()
                into backupFolder
                include output.outputFileName
                rename(output.outputFileName, backupFile.getName())
            }

            // if copyAndRenameAPKTask needs to automatically execute assemble before
            copyAndRenameAPKTask.dependsOn(variant.assemble)
            copyAndRenameAPKTask.mustRunAfter(variant.assemble)

            // if assemble needs to automatically execute copyAndRenameAPKTask after
            variant.assemble.finalizedBy(copyAndRenameAPKTask)
        }
    }
}
Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71
  • Just tried it, works for me, thanks! I guess the main takeaway is to put all this logic in the project.afterEvaluate block as you have done so that the output file is available – ElectronAnt Jun 19 '17 at 20:03
  • I am on AS 2.3.3, but I get an error using this code: "Error:(51, 0) No signature of method: java.util.ArrayList.all() is applicable for argument types: (build_e5fu7jxf39bbzgzwdximl8s6$_run_closure3$_closure8$_closure9) values: [build_e5fu7jxf39bbzgzwdximl8s6$_run_closure3$_closure8$_closure9@4cce4d64] Possible solutions: any(), tail(), tail(), last(), last(), add(java.lang.Object)" – CptanPanic Jul 19 '17 at 22:39