AndroidStudio升級到3.1及Gradle4.4的填坑

weixin_34357887發表於2018-04-02

前言

早上看到說AndroidStudio for mac osx已出v3.1穩定版,下午也收到了更新提示,順手一點就開啟了更新。更新版本肯定會遇到坑的,這我有心理準備,有坑就填是我們的必備技能。坑好歹填完了,但是慢慢發現我已身處雷區...經過一番排雷以及問踩過雷的前輩們,我最後帥氣的一轉身,回退了版本---這雷區就是更新後卡到爆炸,輸入內容的時候會卡,大約平均5秒,尤其是編輯build.gradle的時候,簡直無法忍受。查了下,先烈們給出幾個解決方案,都作用甚微,後又在技術群裡問了其他人,好多也沒碰到這雷區,一切順暢無阻。看來,膽小如我之輩只能等先烈們把雷區踩平,再追隨了~~~這裡我也將更新時踩的坑,給先驅們送塊磚。

更新後遇到的警告和報錯如下:

issue1.

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=commonDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

原來的配置:

android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def file = output.outputFile
            if (file != null && file.name.endsWith('.apk') && file.name.indexOf('debug') == -1) {
                output.outputFile = new File(file.parent, file.name.replace(".apk", "-"
                        + defaultConfig.versionName + "-" + getCurrentTime() + ".apk"))
            }
        }
    }

解決辦法:將each修改為all,output.outputFile修改為outputFileName,具體配置如下:

android.applicationVariants.all { variant ->
        variant.outputs.all{ output ->
            def file = output.outputFile
            if (file != null && file.name.endsWith('.apk') && file.name.indexOf('debug') == -1) {
                outputFileName = "elecontrol_${defaultConfig.versionName}_${getCurrentTime()}"
            }

        }
    }

issue2.

Configuration 'compile' is obsolete and has been replaced with 'implementation'.
It will be removed at the end of 2018

Configuration 'androidTestApi' is obsolete and has been replaced with 'androidTestImplementation'.
It will be removed at the end of 2018

Configuration 'testCompile' is obsolete and has been replaced with 'testImplementation'.
It will be removed at the end of 2018

Configuration 'testApi' is obsolete and has been replaced with 'testImplementation'.
It will be removed at the end of 2018

Configuration 'androidTestCompile' is obsolete and has been replaced with 'androidTestImplementation'.
It will be removed at the end of 2018

釋義-第一個警告:compile 會在2018年底取消,被implementation替代,解決警告的方法就是compile換成implementation就好。
釋疑-compile與implementation的區別:
compile: 可以傳遞依賴引用,比如,B依賴A,C再依賴B,C也能依賴A的引用或者依賴。
implementation:不可傳遞依賴引用,也就是上例中,C不能依賴A的引用或者依賴。
因此,implementation的編譯時間會短一些。
解決辦法:將build.gradle裡的compile替換成implementation即可。
對於下面幾個警告一樣的意思,api也會被替代,直接替換即可。
issue3

The specified Android SDK Build Tools version (25.0.1) is ignored, as it is below the minimum supported version (27.0.3) for Android Gradle Plugin 3.1.0.
Android SDK Build Tools 27.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '25.0.1'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
Update Build Tools version and sync project

釋義-Gradle Plugin3.1.0對應匹配的BuildToolsVersion最小支援版本27.0.3,因此要將專案中buildToolsVersion修改為27.0.3,版本匹配就可以了。
解決方法:
1.手動修改build.gradle裡的配置項。
2.或者專案右鍵 open module settings --> buildToolsVersion修改為27.0.3
Tips:另外android.support依賴庫的版本也應該改為對應版本,我統一改為27.0.3但由於我本地沒有此版本,,更新又更新不成功,本地有一個27.0.1,所以改為了27.0.1,build成功。所以這裡的版本匹配應該是匹配的大版本,也就是27。

後記1

神奇的事:重啟了電腦後,瞬間感覺流暢了~~~~看來的確是我的問題,喜歡嚐鮮的,應該可以嘗試!

後記2

重啟電腦這神操作,總感覺不是正道,並不是真正的解決辦法。後來升級到Gradle4.4之後,個人專案你可以隨便改,但是公司專案就不能那麼隨意了,畢竟不能保證專案組的開發人員都升級到了4.4,導致各種莫名其妙的問題出現,後回退到3.0.1,問題依舊。索性徹底刪除AndroidStudio 重灌了3.1,卡頓以及其他問題統統消失了~
附上徹底刪除AS的方法:

rm -Rf /Applications/Android\ Studio.app
rm -Rf ~/Library/Preferences/AndroidStudio*
rm ~/Library/Preferences/com.google.android.studio.plist
rm -Rf ~/Library/Application\ Support/AndroidStudio*
rm -Rf ~/Library/Logs/AndroidStudio*
rm -Rf ~/Library/Caches/AndroidStudio*

重灌,重新配置。另外暫未更新Gradle4.4,AndroidStudio3.1流暢起來~
Tips:暫未更新Gradle4.4,目前不確定之前的卡頓是否是因為升級Gradle導致的,這個後續驗證後同步結果。

相關文章