1
1

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.

Gradleで任意のファイル名で.apkを無警告で書き出す (2019/11版)

Last updated at Posted at 2019-11-15

TL;DR

AndroidのGradle Buildで出力される.apkのファイル名を好きに決めたかったので,何年か前にいろいろググってbuild.gradleを設定してあったのですが,最近のGradleのVersion UpでWarningが出るようになってしまいました...

なので,最新Verでも無警告で気持ちよく任意名の.apkを出力できるようにします.

環境

Android Studio : 3.5.2
Android Gradle Plugin Version : 3.5.2
Gradle Version : 5.4.1

何が問題だったか

もともとのbuild.gradleでは ↓ のような処理で.apkの名前を差し替えていましたが,
output.outputFile.nameの処理が内部でDeprecatedなAPIを使っているらしく,
API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'.
という警告が出るようになっていました.
(replaceしろと言われましても,そもそもそんなAPI使っとりませんがな...)

app/build.gradle
applicationVariants.all { variant ->
    variant.outputs.all { output ->
        if (variant.name == 'release') {
            if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
                outputFileName = "hoge-fuga-piyo.apk"
            }
        }
    }
}

しばらく放置してましたが,直すことにします.

できあがったもの

app/build.gradle
applicationVariants.all { variant ->
    variant.outputs.each { output ->
        if (output.outputFileName.endsWith(".apk")) {
            switch (variant.name) {
                case "debug":
                    output.outputFileName = "DEBUG.apk"
                    break
                case "release":
                    output.outputFileName = "RELEASE.apk"
                    break
            }
        }
    }
}

これでBuild APKでもGenerage Signed APKでも好きな名前で.apkを書き出すことができるようになります.

さいごに

AndroidがGradle Buildに対応したころ,任意APK名とかそのうち公式で対応するでしょ,と思ってたんですが,いまだに無いのは解せんですよ.

TODO:

.apk名にVersion Nameを入れたいような気もするので,そのうち追加します.
(これに関してはぐぐるといっぱいでてきますが)

---///

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?