Copy APK file from build location to some other location in gradle 4.4

﹥>﹥吖頭↗ 提交于 2020-01-03 06:32:13

问题


As in gradle 4.4 it is not possible to change path of APK output file and we can't use absolute path for the apk's output now from the docs - Modifying variant outputs at build time may not work so I searched on SO and found a solution that we can copy apk to our desired location after it gets build but I don't have much idea on gradle scripting and i am not able to call copy task. Can anyone help me.

code from my gradle :

    android {
    ................
    android.applicationVariants.all { variant ->
    variant.outputs.all {
        if (variant.name.contains("Release")) {
             outputFileName = "${variant.name}-${variant.versionName}.apk"
        }
     }
     assembleRelease {
        dependsOn copyDocs
    }
    }// end of android brace

    task copyApk(type: Copy) {
    from outputFileName
    into file("${project.buildDir}/outputs/apk")
   }
  }

From this way i am getting error :

Could not get unknown property 'outputFileName' for task

Any idea how to copy apk file to another path? Thanks.


回答1:


android.applicationVariants.all { variant ->
    variant.outputs.all {
        println variant.name
        if (variant.name.contains("release")) {
            outputFileName = new File("../", "${variant.name}-${variant.versionName}.apk")
            println outputFileName
        }
    }
}

and remove

task copyApk



回答2:


I am able to do it by running copy script in gradle file like this :

android.applicationVariants.all { variant ->
variant.outputs.all {

copy {
      from file("${project.buildDir}/outputs/apk/" + variant.name + "/release/${outputFileName}")
      into file("${project.buildDir}/outputs/apk/")
     }
    delete file("${project.buildDir}/outputs/apk/" + variant.name) // i don't want apk on this location so after successful copy i am deleting it
  }
 }


来源:https://stackoverflow.com/questions/50169963/copy-apk-file-from-build-location-to-some-other-location-in-gradle-4-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!