简体   繁体   中英

Gradle Copy APK file using publish task in Android Studio 3.0

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

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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM