iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
Mobile Development

轉職App開發打怪雜記系列 第 29

更新Android Studio Arctic Fox | 2020.3.1與android X 與相關開發環境升級

緣由:

因新版的Android Studio一直彈出提示要更新,Android Gradle Plugin也一直有更新提示,查看了一下新的開發環境好似有了重大的變化,連版本號都看得出來有著大躍進,都要更新了就直上最新的Release版吧~雖然未知的領域很可怕,但該備份的也都備份好了,就來試試看

實作:

將專案的內建函式庫合併成新的androidX,這時候不得不說Android Studio很貼心,有個一鍵轉移成androidX的功能
https://ithelp.ithome.com.tw/upload/images/20210929/20130757qNK0LubXl0.png
點擊後會彈出視窗
https://ithelp.ithome.com.tw/upload/images/20210929/20130757vcVhEeKCSz.png
接著會在下方的Find出現所有會因為此更新影響的檔案,然後點擊Do Refactor
https://ithelp.ithome.com.tw/upload/images/20210929/20130757rDxTV3axKl.png
出現進度條,稍待更新
https://ithelp.ithome.com.tw/upload/images/20210929/20130757JZeTrdcl0d.png
更新完成後會發現原本android.support的函式庫都被換成了androidX為首的函式庫
https://ithelp.ithome.com.tw/upload/images/20210929/20130757P84eKyKJi4.png
然後點擊”File”>”Project Structure…”
https://ithelp.ithome.com.tw/upload/images/20210929/20130757c2hCVmhnYR.png
更新一下java版本(從1.7改成1.8),同步完
https://ithelp.ithome.com.tw/upload/images/20210929/201307575pTfFOPPjX.png
https://ithelp.ithome.com.tw/upload/images/20210929/20130757DtffRE3kdu.png
執行編譯,發現有報錯

  • package android.support.design.R does not exist
    原來還有一些合併成androidX的項目沒有自動轉換
    將android.support.design.R改成com.google.android.material.R即可
    接著再繼續編譯一次,編譯成功,APP運行也正常
    建議更新的部分依專案的需要決定是否需要更新

而Android Gradle Plugin Version和Gradle Version的部分
https://ithelp.ithome.com.tw/upload/images/20210929/20130757SBG24ZE1s4.png
假如要更新到新版,就要升級Android Studio版本
https://ithelp.ithome.com.tw/upload/images/20210929/20130757sh99pUFqZL.png

於是將版本從4.1.2更新成Arctic Fox | 2020.3.1
https://ithelp.ithome.com.tw/upload/images/20210929/201307576o1uxnIHvY.png
將Android Gradle Plugin Version更新為7.0.2
將Gradle Version更新為7.0.2
https://ithelp.ithome.com.tw/upload/images/20210929/20130757RraGMOYIXT.png
然後編譯後出現此錯誤

  • Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8

我將java1.8 改為11後
還是出現

  • Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8
    關閉android Studio再重啟,或同步好幾次都沒有用
    於是我查找了資料原來要到SDK Location中設定
    https://ithelp.ithome.com.tw/upload/images/20210929/201307575y3k0TRqST.png
    https://ithelp.ithome.com.tw/upload/images/20210929/20130757JRbg1pLQXA.png
    這樣才可以成功編譯

然後出現下面的警告與錯誤
警告:

* DSL element 'dexOptions' is obsolete and should be removed. It will be removed in version 8.0 of the Android Gradle plugin. Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically. 
* Using flatDir should be avoided because it doesn't support any meta-data formats. 

錯誤:

* API 'BaseVariant.getApplicationIdTextResource' is obsolete and has been replaced with 'VariantProperties.applicationId'. It will be removed in version 7.0 of the Android Gradle plugin. For more information, see TBD. To determine what is calling BaseVariant.getApplicationIdTextResource, use -Pandroid.debug.obsoleteApi=true on the command line to display more information. 
* Some problems were found with the configuration of task ':app:processCa88asiaDebugGoogleServices' (type 'GoogleServicesTask'). 

先解決錯誤
將專案的依賴項的google 服務更新成最新版

dependencies {
    classpath 'com.android.tools.build:gradle:7.0.2’
    classpath 'com.google.gms:google-services:4.2.0'
}

更新版本為4.3.8

dependencies {
    classpath 'com.android.tools.build:gradle:7.0.2'
    classpath 'com.google.gms:google-services:4.3.8'
}

接著處理警告一
* DSL element 'dexOptions' is obsolete and should be removed. It will be removed in version 8.0 of the Android Gradle plugin. Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically.
找到下列的項目?內容?

dexOptions {
    dexInProcess true
    preDexLibraries true
    javaMaxHeapSize "4g"
}

刪除以上內容即可

繼續處理警告二
* Using flatDir should be avoided because it doesn't support any meta-data formats.
於儲存庫中找到下列指令

flatDir {
    dirs 'libs'
}

刪除以上指令,於”android”項下調整添加下列指令即可

sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

以上更新完成,也清掉警告和錯誤了~下一篇針對jcenter即將落日後的舊專案調整做解說~


上一篇
更新android專案API版本
下一篇
如何把Jcenter或macenCenter等網路開源依賴庫搬遷至本地依賴
系列文
轉職App開發打怪雜記30

尚未有邦友留言

立即登入留言