「Android」AS升级到3.1及Gradle4.4的填坑

前言

早上看到说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导致的,这个后续验证后同步结果。

    原文作者:刀码旦
    原文地址: https://www.jianshu.com/p/3115326ff0e9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞