How to define apk output directory when using gradle?

后端 未结 4 767
孤城傲影
孤城傲影 2020-12-08 08:07

How to define apk output directory when using gradle?

I would like to have possibility to upload apk to shared folder after each build.

4条回答
  •  忘掉有多难
    2020-12-08 08:33

    I found the solution that works with the latest Gradle plugin:

    def archiveBuildTypes = ["release", "debug"];
    
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (variant.buildType.name in archiveBuildTypes) {
                // Update output filename
                if (variant.versionName != null) {
                    String name = "MY_APP-${variant.versionName}-${output.baseName}.apk"
                    output.outputFile = new File(output.outputFile.parent, name)
                }
                // Move output into DIST_DIRECTORY
                def taskSuffix = variant.name.capitalize()
                def assembleTaskName = "assemble${taskSuffix}"
                if (tasks.findByName(assembleTaskName)) {
                    def copyAPKTask = tasks.create(name: "archive${taskSuffix}", type: org.gradle.api.tasks.Copy) {
                        description "Archive/copy APK and mappings.txt to a versioned folder."
                        print "Copying APK&mappings.txt from: ${buildDir}\n"
                        from("${buildDir}") {
                            include "**/mapping/${variant.buildType.name}/mapping.txt"
                            include "**/apk/${output.outputFile.name}"
                        }
                        into DIST_DIRECTORY
                        eachFile { file ->
                            file.path = file.name // so we have a "flat" copy
                        }
                        includeEmptyDirs = false
                    }
                    tasks[assembleTaskName].finalizedBy = [copyAPKTask]
                }
            }
        }
    }
    

提交回复
热议问题