如何获取Gradle任务的文件输出

时间:2019-07-02 19:37:20

标签: android-studio gradle android-gradle

我想在Android Studio中创建gradle任务,该任务将对另一个项目的build任务的输出进行某些操作

task foo {
   dependsOn ':someApp:build'
   ...
   copy results of :someApp:build to another location
   ...
}

我不能仅仅对路径进行硬编码,因为我不知道someApp是作为debug还是release构建的,并且输出路径会根据构建的类型而变化。

2 个答案:

答案 0 :(得分:0)

不使用硬编码路径的动态复制任务。

applicationVariants.all {
        variant->
            variant.outputs.each { output ->
                println("..a")
                task "copy${variant.buildType.name}"(type:Copy){
                    println("${variant.buildType.name}")
                    dependsOn app:assembleDebug

                    copy {
                        from "$buildDir\\outputs\\apk\\${variant.buildType.name}\\app-${variant.buildType.name}.apk"
                        into "C:\\Users\\rkumar8\\AndroidStudioProjects\\MyApplication\\app\\build\\outputs\\apk\\"
                    }
// you can add multiple copy if required, i am doing it here release build was named app-release-unsigned.apk
                    copy {
                        from "$buildDir\\outputs\\apk\\${variant.buildType.name}\\app-${variant.buildType.name}-unsigned.apk"
                        into "C:\\Users\\rkumar8\\AndroidStudioProjects\\MyApplication\\app\\build\\outputs\\apk\\"
                    }


                }

            }
    }

答案 1 :(得分:-1)

我在应用模块的build.gradle的android块中添加了一个任务。

applicationVariants.all {
        variant->
            variant.outputs.each { output ->
                println("..a")
                task "copy${variant.buildType.name}"(type:Copy){
                    println("${variant.buildType.name}")
                    dependsOn app:assembleDebug

                    copy {
                        from "$buildDir\\outputs\\apk\\${variant.buildType.name}\\app-${variant.buildType.name}.apk"
                        into "C:\\Users\\rkumar8\\AndroidStudioProjects\\MyApplication\\app\\build\\outputs\\apk\\"
                    }
// you can add multiple copy if required, i am doing it here release build was named app-release-unsigned.apk
                    copy {
                        from "$buildDir\\outputs\\apk\\${variant.buildType.name}\\app-${variant.buildType.name}-unsigned.apk"
                        into "C:\\Users\\rkumar8\\AndroidStudioProjects\\MyApplication\\app\\build\\outputs\\apk\\"
                    }


                }

            }
    }
  1. 还有另一篇文章,将我们的任务添加到android构建路径中。 https://discuss.gradle.org/t/how-to-insert-my-task-into-a-pre-defined-build-android-build/29685/3