Kapil Rajput
Kapil Rajput

Reputation: 11545

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

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.

Upvotes: 2

Views: 2885

Answers (2)

Kapil Rajput
Kapil Rajput

Reputation: 11545

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

Upvotes: 0

Zhefei
Zhefei

Reputation: 31

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

Upvotes: 3

Related Questions