22
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidStudioでAPKを作ったあとに特定のディレクトリにAPKをコピーする

Last updated at Posted at 2014-07-16

リリース用のAPKを必ず同じ場所に配置したい場合があると思いますが、AndroidStudio/gradleなら専用タスクを作ることで簡単に実現できます。

参考: http://stackoverflow.com/questions/21434554/copying-apk-file-in-android-gradle-project/21533902#21533902

build.gradle
apply plugin: 'com.android.application'

android {
...
    def publish = project.tasks.create("publishAll")
    android.applicationVariants.all { variant ->
        if (variant.buildType.name.equals("release")) {
            def task = project.tasks.create("publish${variant.name.capitalize()}Apk", Copy)
            task.from(variant.outputFile)
            task.into(deployTo)

            task.dependsOn variant.assemble
            publish.dependsOn task

            // copyXxxMappingTxt
            if (variant.buildType.runProguard) {
                def copyTask = project.tasks.create("copy${variant.name.capitalize()}MappingTxt", Copy)

                // variant.outputFile = "build/outputs/apk/xxx.apk"
                // from: "build/outputs/proguard/free/release/mapping.txt"
                // into: "proguard-outputs/free-release-mapping.txt"

                def flavorName = variant.productFlavors[0].name
                def buildTypeName = variant.buildType.name

                copyTask.from(variant.outputFile.parent + "/../proguard/${flavorName}/${buildTypeName}/mapping.txt")
                copyTask.rename('(.*)', "${flavorName}-proguard-\$1")
                copyTask.into(variant.outputFile.parent + "/../../../proguard-outputs")

                System.println("* copy mapping txt : [${flavorName}][${buildTypeName}]")

                copyTask.dependsOn variant.assemble
                task.dependsOn copyTask
            }
        }
    }
...
}
...
gradle.properties
deployTo=D:\\hoge\\fuga

上記のように書いておくと、"publishAll" という task を実行するだけで release の assemble が実行されたあとに "D:\hoge\fuga" にAPKが配置されます。

複数の ProductFlavor がある場合も release 版だけが全部実行されるので便利です。

ついでに copyXxxMappingTxt のようなタスクで mapping.txt を特定のディレクトリにコピーするようにしてあります。
これも "publishAll" で実行されます。

22
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?