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でした。
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以上である必要があるので更新します。
※詳しくはこちらを参照。
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以上である必要があるので更新します。
※詳しくはこちらを参照。
android {
compileSdkVersion = 17
- buildToolsVersion = '25.0.0'
+ buildToolsVersion = '26.0.2'
sourceSets {
main {
compileの廃止
dependencies内で利用するライブラリを記述するときに使うcompileがimplementationに変わりました。
単純に一括置換すると余計なものも置き換えられるので注意。
※他にあるので詳しくはこちらを参照。
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を指定しても動作するので必須の対応ではありません。
repositories {
jcenter()
mavenCentral()
- maven { url 'https://maven.google.com/' }
+ google()
flatDir{
dirs 'libs'
}
flavorDimensionsの追加
flavorDimensionsの指定が必須になりました。
flavorDimensionsを定義してproductFlavors内で指定します。
※詳しくはこちらを参照。
+ flavorDimensions "default"
productFlavors {
develop {
+ dimension "default"
}
}
matchingFallbacksの指定
依存するライブラリモジュール側にアプリと同じbuildTypesまたはproductFlavorsが存在しない場合はエラーとなるようになりました。
その場合はmatchingFallbacksを指定して、存在しない場合に利用するデフォルトを指定します。
※詳しくはこちらを参照。
buildTypes {
debug {
...
}
review {
// 依存ライブラリ側ではreviewがないのでその場合はdebug(debugもなければrelease)を利用する。
+ matchingFallbacks = ['debug', 'release']
}
release {
...
}
APKファイル名変更
今まで出力するAPKのファイル名を変更していた場合にエラーになるので修正が必要です。
※詳しくはこちらを参照。
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
}
}