くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

Gradleでapkファイル名を指定する part2(com.android.tools.build:gradle:1.0.0)

12月9日にAndroidStudioの1.0.0がリリースされ、プレビュー版から安定版に移行しました!!

めでたい!!

が、しかし。安定版になったのはいいのですが、それに伴い、Gradleのcom.android.tools.buildのバージョンが1.0.0を要求されてしまった。。。

このバージョンアップにより、いくつかのAPIが廃止され、いままでやってたのがうまく動かなくなってしまった。。。

詳しいことはこちらを参考にしてもらうとして。

前のエントリーで紹介したapkファイル名を指定するやり方も影響を受けているようで、修正が必要になった。その際の備忘録。

バージョンは、0.14.2から1.0.0へのアップデート。

classpath 'com.android.tools.build:gradle:0.14.2'

↓ 

classpath 'com.android.tools.build:gradle:1.0.0'

該当の箇所はこんな感じに変更するとうまくいった!!

まずは、前回(ver0.14.2)の

apply plugin: 'com.android.application'

android {
    defaultConfig {
        ・・・
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 3
        versionName "1.1"

        ・・・・
    }
・・・
    applicationVariants.all { variant ->
        if (variant.buildType.name.equals("release")) {
            def file = variant.outputFile
            def newName = "<filename>_ver${defaultConfig.versionName}.apk"
            variant.outputFile = new File(file.parent, newName)
        }
    }
}

そして今回(1.0.0)の

apply plugin: 'com.android.application'

android {
    defaultConfig {
        ・・・
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 3
        versionName "1.1"

        ・・・・
    }
・・・
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (variant.buildType.name.equals("release")) {
                def file = output.outputFile
                def newName = "<filename>_ver${defaultConfig.versionName}.apk"
                output.outputFile = new File(file.parent, newName)
            }
        }
    }
}

変更点としては、下記の2点です。

  1. android.applicationVariants.allandroid.を付けないとapplicationVariantsにアクセスできなくなった。
  2. variantから直接outputFileを参照することができず、variant.outputsoutputが必要になった。

より詳細は、下記のUser Guideをみるのがいいと思います。

[PR] めもらばではこんなあぷりを作っています!