一. 最近將AndroidStudio2.x升級到了Android Studio3.x遇到了一系列的問題.
as 3.0開始支持lambda表達式了, 因此為了優(yōu)化項目刪除額外的retrolambda庫, 需要從as2.x升級到as3.x
二. 先說下升級的步驟, 然后在說遇到的問題.
升級步驟:
- 更新gradle插件
-
在
rootProject
目錄下的build.gradle
文件中將gradle插件升級為3.x
buildscript { dependencies { //升級gradle插件: 2.x => 3.x classpath 'com.android.tools.build:gradle:3.x' ... } ... }
-
更新gradle分發(fā)包:
將<rootProject>/gradle/wrapper/gradle-wrapper.properties
文件中的gradle版本升級為4.1
, 如下:
9BFC9E33-D3E4-4179-A86D-02D1F83D9E32.png
-
升級輸出文件(*.apk)名稱相關(guān)配置
AS2.x
時配置如下://設(shè)置apk文件的名稱 (as2.x) applicationVariants.all { variant -> variant.outputs.each { output -> def apk = output.outputFile def newName = "VRVideo_" + variant.buildType.name + "_" + defaultConfig.versionName + "_" + defaultConfig.versionCode + ".apk"; output.outputFile = new File(apk.parentFile, newName) } }
AS3.x
時配置如下://設(shè)置apk文件的名稱 (as3.x) applicationVariants.all { variant -> variant.outputs.all { outputFileName = "VRVideo_" + variant.buildType.name + "_" + defaultConfig.versionName + "_" + defaultConfig.versionCode + ".apk" } }
將build.gradle文件中的
compile
更新為api / implementation
(下面會講compile/api/implementation的區(qū)別)更新
android-apt
插件
-
刪除android-apt相關(guān)配置
//root project中的build.gradle相關(guān)配置 buildscript { dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } ... } //library-module 或 main-module的build.gradle中的配置 apply plugin: 'com.neenbedankt.android-apt' dependencies { apt 'org.greenrobot:eventbus-annotation-processor:3.0.1' ... } apt { arguments { eventBusIndex "xxx.xxx.XxxIndex" } }
- EventBus配置升級
使用gradle插件內(nèi)置的annotationProcessor替代android-apt的apt配置, 如下:
注解解析器配置:
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
換成
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
-
EventBus的特殊配置:
apt { arguments { eventBusIndex "xxx.xxx.XxxIndex" } }
換成:
android { ... javaCompileOptions { annotationProcessorOptions { arguments = [eventBusIndex: 'xxx.xxx.XxxIndex'] } } }
- ButterKnife配置升級 (7.0.1 => 8.8.1)
- 配置文件更改, 參考=>ButterKnife
- 更新API調(diào)用:
@Bind => @BindView
;
@Bind({}} => @BindViews({})
;
ButterKnife.unbind() => Unbinder.unbind() --- ButterKnife.bind()會返回Unbinder對象
- 更新proguard文件 (其他配置一樣, 更新下面這句)
#-keep class **$$ViewInjector { *; } #butterknife6.x-生成的類
#-keep class **$$ViewBinder { *; } #butterknife7.x生成的類
-keep class **_ViewBinding { *; } #butterknife8.x生成的類
三. 升級過程中遇到的問題
- apk文件名稱相關(guān)配置升級 (如二所示)
- 注解解析器升級, 即將android-apt插件換成gradle插件中的
annotationProccessor
(gradle插件自2.2+開始支持內(nèi)置的annotationProcessor配置) - EventBus配置升級 (如二所示)
- ButterKnife配置升級 (如二所示)
-
安卓構(gòu)建工具升級
BC1D97FE-B8E5-4827-B7A7-4427273462F4.png
從上圖可獲得兩條信息:
-
Android Gradle Plugin 3.0.1
支持的最低Android SDK Build Tools
的版本為26.0.2
- 在
as3.x
上buildToolsVersion 'x.x.x'
這個配置可以省略掉, 因為每個版本的Android Gradle Plugin
都有一個默認版本的Build Tools
因此, 你可以刪掉buildToolsVersion 'x.x.x'
這句配置 或 Build Tools
的版本升級到26.0.2
或更高版本
-
compile => api, implementation
, api和implementation的區(qū)別就如其詞義所示:api
即用于對外(當前module之外)提供接口,implementation
就是用來實現(xiàn)當前module(定義的api)的. 這樣解釋有點抽象, 可能你并沒聽懂. 舉個例子:
你在你的Library Module
中引用了一個第三方庫(如fastjson), 你想其他人使用你的這個Library Module
的時候也能直接使用這個第三方庫, 這時你應(yīng)該用api 'com.xxx.xxx'
(即將關(guān)鍵詞api之后引用的library的api對外公開, 不僅可以在當前module中使用, 其他引用當前module的調(diào)用者也可以使用第三方庫); 否則, 則用implementation 'com.xxx.xxx'
總結(jié)一下:
之前版本的compile和現(xiàn)今的api等價, 即之前用compile的地方都可以換成api
compile和api都是引用傳遞的, 即: 如果B引用了C, A引用了B; 那么A也引用了C
implementation不是引用傳遞的, 即: 如果B引用了C, A引用了B; 那么A并沒有引用到C (如果A要引用C, 你必須在A的build.gradle中添加引用配置)
一般原則:
Library Module
中用api
, 而Main Module
中用implementation
(當然, 如果Library Module
中引用的模塊只在當前Library Module
中使用, 那就應(yīng)該用implementation
)-
將
debugCompile project(path: ':xxx', configuration: 'debug') releaseCompile project(path: ':xxx', configuration: 'release)
改成
debugImplementation project(':xxx') releaseImplementation project(':xxx')
前面那個寫法主要是為了在library module中正確的使用BuildConfig.DEBUG的值 (參考: http://www.lxweimin.com/p/1907bffef0a3).
在as3.x中, 已經(jīng)BuildConfig.DEBUG值錯誤問題已經(jīng)修復(fù)了所以直接用debugImplementation或releaseImplementation就可以了, 不再需要指定configuration.
debugImplementation project(path: 'xxx', configuration: 'debug')
這樣配置是不能正常工作的, 會導致下面錯誤:
Error:Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :library.
-
Library Module
中不能使用shrinker配置, 錯誤如下:
EDAF6B81-4060-4532-B548-FB96869D89D0.png
根據(jù)錯誤信息, 直接把Library Module
中的build.gradle配置中的shrinkResources true
刪掉即可恢復(fù)正常 -
../res/values/styles_dialog.xml:5:5-15:13: AAPT: error: style attribute '@android:attr/windowFrame' not found.
as3.x 使用aapt2來處理資源文件, 編譯時會拋出上面錯誤. 暫時先禁用aapt2, 即在rootProject/gradle.properties
文件中添加下列語句:
android.enableAapt2=false
如果要用aapt2呢?? ......
References:
http://blog.csdn.net/xx326664162/article/details/68490059
https://developer.android.google.cn/studio/build/gradle-plugin-3-0-0-migration.html