How do I get file outputs of gradle task

2019-08-27 15:14发布

问题:

I want to create a gradle task in Android Studio that will do something with the output of the build task of another project

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

I can't just hardcode a path as I won't know if someApp was built as debug or release and the output paths will vary based on the type of build.

回答1:

dynamic copy task without using hardcoded path.

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\\"
                    }


                }

            }
    }


回答2:

I added a task in android block of build.gradle of app module.

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. There is another post for adding our task in android build path. https://discuss.gradle.org/t/how-to-insert-my-task-into-a-pre-defined-build-android-build/29685/3