Android Studio 3.0前后版本的差异
1. Gradle版本不匹配
修改项目下 gradle/wrapper/gradle-wrapper.propertie 文件中的distributionUrl
AS 3.0 ~ 3.0.1改为:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
AS 3.1.1 ~3.1.3 改为:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
2. Gradle插件不匹配
1)项目根目录下的 build.gradle文件中两个repositories节点都添加google()
- E.G
- buildscript{
- repositories {
- google() //新增
- }
- dependencies {
- classpath'com.android.tools.build:gradle:3.0'//与AS版本一致
- //classpath'com.android.tools.build:gradle:3.1.1'
- }
- }
- allprojects {
- repositories{
- google() //新增
- }
- }
2)项目app下 build.gradle文件中,修改相关支持库版本
AS 3.0 ~ 3.0.1:
- android {
- compileSdkVersion 26
- buildToolsVersion "26.0.2"
- ...
- }
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:26.1.0'
- implementation 'com.android.support:design:26.1.0'
- testImplementation 'junit:junit:4.12'
- ...
- }
AS 3.1.1 ~ 3.1.3:
- android {
- compileSdkVersion 27
- buildToolsVersion "27.0.3"
- ...
- }
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'com.android.support:appcompat-v7:27.1.1'
- implementation 'com.android.support:design:27.1.1'
- testImplementation 'junit:junit:4.12'
- ...
- }
3. Gradle编译报flaovr配置错误
- Error
Error:A problem occurred configuring project ':app'.> All flavors must now belong to a named flavor dimension.
-
Solution
AS 3.0后Gradle添加了flavorDimensions属性,用来控制多个版本的代码和资源,缺失就会报错。在项目app下build.gradle文件中,添加flavorDimensions:
- android {
- ...
- flavorDimensions "tier","minApi"
- productFlavors{
- fees{
- dimension"tier"
- ...
- }
- minApi23{
- dimension"minApi"
- ...
- }
- }
- }
如果不需要多版本控制只需添加:flavorDimensions "code"(随意定义)
- android {
- ...
- defaultConfig {
- ...
- flavorDimensions "code"
- }
- ...
- }
4. Gradle自定义apk名称报错(Cannot set the value of read-only property 'outputFile' )
-
E.G
AS 3.0之前自定义apk名称:
- applicationVariants.all { variant ->
- variant.outputs.each { output ->
- def fileName = "${variant.versionName}_release.apk"
- def outFile = output.outputFile
- if (outFile != null && outFile.name.endsWith('.apk')) {
- output.outputFile =newFile(outFile.parent, fileName)
- }
- }
AS 3.0之后,同样代码自定义apk名称却会报错:
- Error
Error:(56, 0) Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
-
Solution
outputFile变为只读,不能修改输出的名称所以报错。修改为:
- applicationVariants.all { variant ->
- variant.outputs.all { output -> // each 改为 all
- def fileName = "${variant.versionName}_release.apk"
- def outFile = output.outputFile
- if (outFile != null && outFile.name.endsWith('.apk')) {
- outputFileName = fileName // output.outputFile 改为 outputFileName
- }
- }
把each修改为all,然后通过outputFileName修改生成apk的名称。此外,AS 3.0后打包完,除了apk包文件,还会多一个 output.json 参数文件。
5. AS 3.0后关键字依赖变化
-
E.G
AS 3.0之前依赖关键字:compile
- dependencies {
- compile fileTree(include: ['*.jar'], dir: 'libs')
- compile 'com.android.support:appcompat-v7:26.1.0'
- compile files('libs/gson-2.3.1.jar')
- compile project(':mylibrary')
- ...
- }
AS 3.0之后依赖关键字:implementation
- dependencies {
- implementation fileTree(include: ['*.jar'], dir: 'libs')
- implementation 'com.android.support:appcompat-v7:26.1.0'
- implementation files('libs/gson-2.3.1.jar')
- implementation project(':mylibrary')
- ...
- }
AS 3.0后Gradle关键字依赖发生变化:
compile(implementation/api),provided(compileOnly),apk(runtimeOnly)
AS 3.0后,在使用新依赖配置项时,引用本地库使用implementation指令时,若出现找不到导包或资源问题报错,可以更换依赖指令为api重新编译。关于implementation和api的区别:
implement
概念 : 将该依赖隐藏在内部,而不对外部公开。
理解 : 在 app mudule 中使用 implement 依赖的第三方库, 在其他 mudule 是无法调用的.
举例 : 此时项目中有一个 mudule 是 ImageLoader ,其内部用 implement 指令依赖了 glide 这个库, 那么此时我们在 app mudule 中无法调用 glide 库中的方法.
compile
概念: android studio 3.0 版本后废弃该指令 改用 api 代替, api 完全等同于之前的 compile 指令, 也就是普通的依赖, 第三方库在 mudule 中依赖后其他 mudule 都可以使用该库.
6. AAPT2编译报错
- Error
Error: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
- Solution
在项目根目录下gradle.properties文件中关闭APPT2编译:
- ...
- android.enableAapt2 = false
7. PNG 图片错误,AAPT err(Facade for):Unable to open PNG file
- Error
AAPT err(Facade for):……Unable to open PNG file
-
Solution
项目app下build.gradle文件中添加下面属性:
- android{
- ...
- aaptOptions{
- cruncherEnabled = false
- useNewCruncher = false
- }
- ...
- }
用来关闭AS图片PNG合法性检查的,直接不让它检查。
-
Note
如果还是有错误,请检查:
1 ) .9.png图片放在res/drawable文件夹下
2 ) .9.png图片四边都要有黑线,确保图片是标准的.9.png图片
8. 输入法中文状态下无法选词
-
Question
AS 3.0后在输入中文时候会出现键盘不显示,无法筛选词输入中文 -
Solution
首先要说不是你的输入法问题,而是AS 3.0后的一个BUG,下面提供几种解决方案:
1 ) 如果你还在2.3.x的环境下开发,为了避免输入法问题,建议你暂时不要升级到3.x;
2 ) 如果你想2.3.x升级使用3.x,那么不建议你使用2.x的jre替换3.x的jre方式去处理输入法问题,虽然暂时可以解决输入问题,但是后面升级的时候你还得把2.x 的jre换回3.x的jre,否则升级后将无法正常使用AS;
3 ) 最简单最有效的解决办法就是在使用 AS的时候,切换到 windows自带的中文输入法就可以正常输入中文筛选词语了,相对而言,这样方便很多。虽然没有第三方输入法用起来那么顺手,但是可以有效解决输入法问题和避免以后升级的问题;
4 ) 终极方案:升级到 AS 3.1.1即可解决,AS 3.1.1已经修复了输入法中文状态下无法选词的BUG。
9. 移除无用资源问题
- Error
Error: Removing unused resources requires unused code shrinking to be turned on.
- Solution
- android {
- ...
- buildTypes {
- debug {
- signingConfig signingConfigs.release
- debuggable true
- zipAlignEnabled true
- minifyEnabled true //是否混淆
- shrinkResources true //是否去除无效的资源文件
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- release {
- signingConfig signingConfigs.release
- zipAlignEnabled true
- debuggable true
- minifyEnabled true //是否混淆
- shrinkResources true //是否去除无效的资源文件
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- ...
- }
AS 3.0.1后,如果使用shrinkResources来移除未引用资源,必须要先开启混淆minifyEnabled,才能通过资源压缩器将它们移除,否则编译会报错。
10. 软件升级安装冲突
-
Error
AS在线升级后,安装重启软件时会出现部分文件安装冲突。如下图所示:
更新问题图
Solution
方法一:
1)找到 AS 安装目录下的 uninstall.exe 卸载文件,运行卸载当前旧版本AS(卸载前记得备份代码和配置文件);
2)下载最新版本的AS安装包,运行安装并导入配置文件和项目。
方法二:
1)点击 Cancel 取消安装并关闭AS;
2)找到 AS 安装目录下的 studio64.exe 启动文件,右键 —— 以管理员身份运行 打开AS(提高AS的权限);
3)点击 Help —— Checkout for Updates —— Update and Restart 重新更新下载安装。