the+android+gradle,Android 史上最全gradle升级3.0+问题汇总(一)

Android Gradle3.0是一个大版本的升级,至于升级的好处,这里就不多说了。直接开干。

升级步骤

目前最新的gradle版本为3.2.0,对应gradle-4.6-all,并且build版本号要求28.0.2以上。但是28.0.2兼容上还是有挺多问题的,这里就不升级到最新的了。

这里升级为3.1.4,gradle4.4,buildToolsVersion为27.0.3,targetSdkVersion用27,compileSdkVersion用27。

修改版本号

build.gradle文件

dependencies {

classpath 'com.android.tools.build:gradle:3.1.4'

....

}

allprojects {

repositories {

jcenter()

google()//添加这句话

}

}

gradle/wrapper/gradle-wrapper.properties文件

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

app/build.gradle文件

android {

compileSdkVersion 27

buildToolsVersion 27.0.3

....

defaultConfig {

targetSdkVersion 27

....

}

}

//v4,v7包统一强制用27.1.0

dependencies {

implementation("com.android.support:support-v4:27.1.0") {

exclude group:'com.android.support', module:'support-v4'

}

implementation("com.android.support:appcompat-v7:27.1.0") {

exclude group:'com.android.support', module:'appcompat-v7'

}

}

然后运行编译,你会发现各种报错,下面我们慢慢来处理。

问题汇总

1、outputFile

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.

这是因为output.outputFile变成了只读属性,不能再往里面写东西了。所以这里需要修改更改打包名字的方式。以下是我这边的修改

原代码:

applicationVariants.all { variant ->

variant.outputs.each { output ->

def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith('debug.apk')) {

def fileName = "SFA_测试版.apk"

output.outputFile = new File(outputFile.parent, fileName)

}

}

}

修改成:

applicationVariants.all { variant ->

variant.outputs.all {

if (outputFile != null && outputFile.name.endsWith('debug.apk')) {

outputFileName = "SFA_测试版.apk"

}

}

}

2、incremental属性弃用,需要删掉设置

DSL element 'DexOptions.incremental' is obsolete and will be removed at the end of 2018.

incremental属性弃用,需要删掉设置

dexOptions {

//incremental true //直接删除这个配置就可以了

...

}

3、compile属性名字更改

Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.

dependencies {

compile fileTree(include: ['*.jar'], dir: 'libs')

testCompile 'junit:junit:4.12'

compile 'com.umeng.analytics:analytics:6.1.2'

....

}

修改如下:

dependencies {

implementation fileTree(include: ['*.jar'], dir: 'libs')

testImplementation 'junit:junit:4.12'

implementation 'com.umeng.analytics:analytics:6.1.2'

....

}

compile —> implementation或api    如果是依赖module的话,需要改成api

testCompile —> testImplementation或testApi

4、butterknife兼容

Annotation processors must be explicitly declared now.  The following dependencies on the compile classpath are found to contain annotation processor.  Please add them to the annotationProcessor configuration.

- butterknife-7.0.1.jar (butterknife-7.0.1.jar)

针对这个问题,有一个快速解决办法,但不推荐使用

android {

defaultConfig {

javaCompileOptions {

annotationProcessorOptions {

includeCompileClasspath true

}

}

}

}

因为在gradle 3.0以后不推荐使用这个插件,最好是更换最新的使用方法,如下:

在根目录的build.gradle文件加上这句:

dependencies {

classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'

}

在app/build.gradle中加上:

dependencies {

implementation 'com.jakewharton:butterknife:8.8.1'

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

}

但是这样改之后,bind的使用方法跟之前的就不一样了,代码中肯定会报错,如:

错误: 找不到符号

符号:   类 Bind

位置: 程序包 butterknife

@Bind(R.id.et_employee_id)  需要改成  @BindView(R.id.et_employee_id)

如果代码涉及改动太多地方,可以使用第一种方式。

5、找不到依赖module引入的包

Compilation failed; see the compiler error output for details.

错误: 程序包com.loopj.android.http不存在

这是因为依赖的module里面用implementation的话会出现主module找不到该包,使用api才可以对外开放,如:

dependencies {

implementation fileTree(dir: 'libs', include: '*.jar')

implementation 'com.loopj.android:android-async-http:1.4.9'

}

需要改成:

dependencies {

api fileTree(dir: 'libs', include: '*.jar')

api 'com.loopj.android:android-async-http:1.4.9'

}

6、AAPT2错误

AAPT2 error: check logs for details

Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details

报这个错误的原因有很多,归根到底还是代码本身的错误。

参考了很多资料,其中一种方法就是在gradle.properties添加android.enableAapt2=false,然后添加了,有些项目是可以解决了,但是部分项目还是会出现另外一个错

The option 'android.enableAapt2' isdeprecated and should not be used anymore.

Use 'android.enableAapt2=true' to removethis warning.

It will be removed at the end of 2018..

这是因为选项“android.enableAapt2”已被弃用,不应再使用。

使用“android.enableAapt2=true”来删除这个警告。

它将在2018年底被移除。

而且这个设置也是治标不治本,还是要去找出代码中的错误。

那究竟怎么解决的,我发现我的项目是因为升级到28.0.2以上才会出现这个问题,那就尝试把buildToolsVersion版本号降低会27.0.3,重新编译。然后完美通过了。所以目前还是用27.0.3的版本就好。最新的AS对应的gradle是3.2.0,但是要求buildToolsVersion版本号是28.0.2以上,所以,我还是选了使用3.1.4的版本,对应4.4。

7、support包的类找不到问题

错误: 找不到符号

符号:   类 KeyEventCompat

位置: 程序包 android.support.v4.view

因为v4包用27.1.0的有可能部分类找不到,可以降为26.1.0,或者找替代类。

最后贴上我的配置:

ext {

minSdkVersion = 15

targetSdkVersion = 27

compileSdkVersion = 27

buildToolsVersion = '27.0.3'

supportVersion = '27.1.0'//

}

8、调试错误Instant-Run

Installation failed with message Failed to establish session

总结以下两种解决办法:

1.如果是小米系统的手机,关掉小米手机开发者选项中的“miui优化”选项

2.关闭2.3的Instant-Run也解决了这个问题。File---Setting---Build---Instant-Run---Enable Instant Run to hot swap code (勾勾去掉)

均可有效解决上述问题。

目前遇到的问题就这么多,后面待续....

有问题欢迎交流....

关于功能业务方面的,请继续看Android 史上最全gradle升级3.0+问题汇总(二)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值