9
7

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.

Android Plugin for Gradle 3.0.x移行でやったことをざっくり

Posted at

Android Plugin for Gradleのバージョンを3にあげると記法が変わっている箇所がいくつかあるので
自分で対応した箇所をざっくりまとめます。

尚、細かい解説は公式にあります。
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html?hl=ja

Gradle pluginのバージョン更新

Gradle pluginのバージョンを更新します。
2018/03/03時点での最新バージョンは3.0.1でした。

build.gradle
 buildscript {
     repositories {
         jcenter()
         maven { url 'https://maven.google.com/' }
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:2.3.1'
+        classpath 'com.android.tools.build:gradle:3.0.1'
     }
 }

Gradleのバージョン更新

Android Plugin for Gradle 3.0を使うにはGradleのバージョンが4.1以上である必要があるので更新します。
※詳しくはこちらを参照。

gradle/wrapper/gradle-wrapper.properties
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

buildToolsVersionの更新

Android Plugin for Gradle 3.0を使うにはAndroid SDK Build Tools のバージョンが26.0.2以上である必要があるので更新します。
※詳しくはこちらを参照。

app/build.gradle
android {
    compileSdkVersion = 17
-   buildToolsVersion = '25.0.0'
+   buildToolsVersion = '26.0.2'

    sourceSets {
        main {

compileの廃止

dependencies内で利用するライブラリを記述するときに使うcompileがimplementationに変わりました。
単純に一括置換すると余計なものも置き換えられるので注意。
※他にあるので詳しくはこちらを参照。

app/build.gradle
dependencies {
-    compile 'com.google.code.gson:gson:2.8.1'
-    testCompile "junit:junit:4.12"
-    androidTestCompile "com.android.support.test:runner:1.0.1"
+    implementation 'com.google.code.gson:gson:2.8.1'
+    testImplementation "junit:junit:4.12"
+    androidTestImplementation "com.android.support.test:runner:1.0.1"
}

googleのmavenリポジトリが追加 [任意]

googleのmevenリポジトリの記述がgoogle()で行えるようになりました。
直接URLを指定しても動作するので必須の対応ではありません。

app/build.gradle
repositories {
    jcenter()
    mavenCentral()
-   maven { url 'https://maven.google.com/' }
+   google()
    flatDir{
        dirs 'libs'
    }

flavorDimensionsの追加

flavorDimensionsの指定が必須になりました。
flavorDimensionsを定義してproductFlavors内で指定します。
※詳しくはこちらを参照。

app/build.gradle
+   flavorDimensions "default"
    productFlavors {
        develop {
+           dimension "default"
        }
    }

matchingFallbacksの指定

依存するライブラリモジュール側にアプリと同じbuildTypesまたはproductFlavorsが存在しない場合はエラーとなるようになりました。
その場合はmatchingFallbacksを指定して、存在しない場合に利用するデフォルトを指定します。
※詳しくはこちらを参照。

app/build.gradle
buildTypes {
      debug {
          ...
      }
      review {
          // 依存ライブラリ側ではreviewがないのでその場合はdebug(debugもなければrelease)を利用する。
+         matchingFallbacks = ['debug', 'release']
      }
      release {
          ...
      }

APKファイル名変更

今まで出力するAPKのファイル名を変更していた場合にエラーになるので修正が必要です。
※詳しくはこちらを参照。

app/build.gradle
     applicationVariants.all { variant ->
-        variant.outputs.each { output ->
+        variant.outputs.all { output ->
             def apkDate = new java.text.SimpleDateFormat("yyMMdd").format(new Date())
             def apkName = "app_${apkDate}_${variant.productFlavors[0].name}.apk"
-            output.outputFile = new File(output.outputFile.parent, apkName)
+            outputFileName = apkName
         }
     }
9
7
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
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?