更新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即将落日後的旧专案调整做解说~


<<:  Day 14. slate × Interfaces × Ref

>>:  半透明的文字看板

【D12】制作图表:加权指数和交易金额的图表

前言 进行观察还是需要用图表表示,这时候叠图就很好用。之前都一张一张画,现在呈现叠图的方式。 本日程...

Day28 简易小键盘小实作3

接着则是加法运算,按下加数後,label变空字串,运算.add,是否值行运算(是),先前的数字为萤幕...

Day 26 密码规则定义规划实作

根据GDPR第5条和CCPA§§1798.83(d)(E)(iii) 和 §§1798.91.04(...

[Angular] Day28. Control Value Accessor (CVA)

前几篇介绍了 Form 的基本操作与概念,也介绍了如何在表单中加入验证,最後要来介绍 Angular...

{DAY 15} Pandas 学习笔记 part.1

前言 Pandas 是强大的资料科学分析工具,结合前几天所学的NumPy特性 提供方便读取及使用的资...