前言
要遷移 先 看官網(wǎng)遷移策略
https://developer.android.google.cn/studio/build/gradle-plugin-3-0-0-migration.html
以下只是我個人的遷移思路和碰到的一些問題及解決方法,僅供參考。
升級Gradle版本
這個在Android Studio 3.0 打開時會自動提醒下載,點擊下載升級就好,Gradle android tools 也會自動升級到3.0.0,當然還要新增 google 庫。
buildscript {
repositories {
...
// You need to add the following repository to download the
// new plugin.
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
apt、compile更換
所有的 apt 要改寫成 annotationProcessor,例如:
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.4.0’
compile 官方已廢棄,推薦改為 api 及 implementation。這個 implementation 我還未理解。我個人只是將庫的依賴改為 api,例如:
api 'com.facebook.fresco:fresco:1.5.0'
定義 flavor dimensions
這是新增的一個所謂 “維度” 的東西。
當你定義了 productFlavors 時,你必須定義 flavor dimensions。flavorDimensions 填的是若干個 String 型。類似以下:
flavorDimensions "apple", "banana"
productFlavors {
qq {
dimension "apple"
...
}
oppo {
dimension "banana"
...
}
}
“apple” 和 “banana” 就分別是兩個 “維度” 。此時你的gradle命令中會出現(xiàn)assembleQqOppo類似的命令。
感覺不對吧?我要么打qq這個包要么打oppo這個包,怎么會出現(xiàn)QqOppo雜交的包呢?
這就是 “維度” 的特性。就是交叉組合。例如:A B C,會組合成:A、 B、C、 AB、 AC、 BC、 ABC 這些。
這顯然不是我想要的,所以我只定義了一個維度。類似以下:
flavorDimensions "1.0"
productFlavors {
qq {
dimension "1.0"
...
}
oppo {
dimension "1.0"
...
}
...
}
解決一些錯誤
這時候同步gradle,可能會出現(xiàn)類似以下的錯誤:
Error:Unable to resolve dependency for ':app@debug/compileClasspath':
Could not resolve project :library.
Error:Unable to resolve dependency for ':app@release/compileClasspath':
Could not resolve project :library.
這是因為你在 app 主模塊的 gradle 中定義了 buildTypes。類似以下:
buildTypes {
debug {
...
}
alpha {
...
}
release {
...
}
}
這時候你必需保證編譯時所有的子 library 模塊能夠指定 buildType。也就是說如果你要編譯 alpha 版本時,所有的子模塊也必須有 alpha 版本。
這就很尷尬了。我所有的 library 都沒有定義 buildTypes。
我的解決方法很 傻 ~ (如果有更好的解決方法麻煩告知,謝謝~)。在所有的 library 子模塊都加上 buildTypes, 如下:
buildTypes {
debug {}
alpha {}
release {}
}
再次同步gradle。可能出現(xiàn)以下錯誤:
Error:Execution failed for task ':TerminalLibrary:transformResourcesWithMergeJavaResForDebug'.
> More than one file was found with OS independent path 'META-INF/LICENSE'
Information:BUILD FAILED
在 app 主模塊下的 build.gradle 中加入如下配置項,排除掉中間生成的META-INF/xxx文件
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
你可能會出現(xiàn)類似以下的錯誤信息:
Error: style attribute '@android:attr/windowEnterAnimation' not found
解決方法也很簡單,找到定義該style的地方,把 @ 刪掉。
你可能會碰到如下 Warning :
Warning:The android.dexOptions.incremental property is deprecated and it has no effect on the build process.
解決:
dexOptions {
javaMaxHeapSize “4g”
incremental true // 將這句刪除就好
}
我只寫出我碰到的問題及我個人解決方法。更多的問題解決請參考本文頂部的官網(wǎng),還望已官網(wǎng)為準~
Java8 的支持
參考官方
https://developer.android.google.cn/studio/write/java8-support.html
在你的 build.gradle 中配置如下:
android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
如果你是從 Jack 遷移過來,請刪除以下代碼塊:
// Remove this block.
jackOptions {
enabled true
...
}
如果你的使用了 retrolambda 插件。請刪除:
// Remove the following plugin.
apply plugin: 'me.tatarka.retrolambda'
...
// Remove this block after migrating useful configurations.
retrolambda {
...
// If you have arguments for the Java VM you want to keep,
// move them to your project's gradle.properties file.
jvmArgs '-Xmx2048m'
}
如果你不想使用Java8 的特性,可以在 gradle.properties 中將其關閉:
android.enableDesugar=false