Gradle插件遇到的问题
切换到As工程后或多或少的遇到了一些问题,以前的时候总是解决后不记录,写这个是为了做个备份,以方便以后开发使用,如有错误的请留言指正,感谢!!!。
一、gradle插件版本和gradle版本对应关系
参见链接: https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle
Plugin version | Required Gradle version |
---|---|
1.0.0 - 1.1.3 | 2.2.1 - 2.3 |
1.2.0 - 1.3.1 | 2.2.1 - 2.9 |
1.5.0 | 2.2.1 - 2.13 |
2.0.0 - 2.1.2 | 2.10 - 2.13 |
2.1.3 - 2.2.3 | 2.14.1+ |
2.3.0+ | 3.3+ |
3.0.0+ | 4.1+ |
3.1.0+ | 4.4+ |
3.2.0 - 3.2.1 | 4.6+ |
3.3.0 - 3.3.2 | 4.10.1+ |
3.4.0+ | 5.1.1+ |
3.5.0+ | 5.4.1-5.6.4 |
1、Plugin version
gradle插件版本就是我们通常在classpath 'com.android.tools.build:gradle:3.0.0’中的版本号
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
2、Required Gradle version
gradle版本就是我们通常在gradle-wrapper.properties文件中的
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
二、Gradle相关的报错
1、ERROR: Could not find method implementation() for arguments [directory ‘libs’] on object of type org
解决方法:参照上面的gradle版本对应列表修改自己的gradle插件版本,implementation()应该是在gradle插件3.0.0+支持的,所以使用3.0.0+然后将gradle修改为自己的对应的版本就可以了
2、Gradle DSL method not found: ‘google()’
解决方法:一般出现这个是因为使用的gradle版本是4.0以下比如是3.3时导致的,这个时候要用
将
maven {
url 'https://maven.google.com'
}
替换
google()
3、No signature of method: java.util.ArrayList.all() is applicable for argument types: (build_6i4c6i01x2c4bmyrwvjrsmef3 _closure6 _run_closure1 _closure13@218b464f]Possible solutions: any(), tail(), tail(), last(), last(), add(java.lang.Object) 或者Cannot set the value of read-only property ‘outputFile’ for …
报错原因:因为使用gradle脚本打包apk重命名的时候会使用下面代码:
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "小工具.apk"
}
}
当前报错是因为我使用的是AndroidStudio3.4.1,但是环境是在gradle 3.3环境下使用gradle插件为2.3.0导致的,当前的gradle插件不支持这么用。
上面是我的报错原因,但追根究底,所有人报这个错是因为每个gradle版本以及对应的gradle插件在以下方法的使用都是有区别的因为gradle插件版本不同,因此给出以下解决方法和示例
解决方法:
(1)使用的gradle版本和gradle插件要对应
(2)根据版本选择下面示例中对应的方法修改
区别是:
(1)variant.outputs.all修改为variant.outputs.each
(2) output.outputFile和outputFileName的使用
示例:
//gradle 4.1 plugin 3.0.0
android.applicationVariants.all { variant ->
variant.outputs.all {
//这里修改apk文件名
def fileName = "${defaultConfig.applicationId}.apk"
outputFileName = fileName
}
}
//gradle 3.3 plugin 2.3.0
android.applicationVariants.all { variant ->
variant.outputs.each {output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
File outputDirectory = new File(outputFile.parent)
//这里修改apk文件名
def fileName = "${defaultConfig.applicationId}.apk"
output.outputFile = new File(outputDirectory,fileName)
}
}
}
3-2:(同上面3的解决方法一样)Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[], versionCode=1, versionName=1.0-debug}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
4、#ICZ4V 编译报错:Error:Unable to load class ‘org.gradle.logging.StyledTextOutput$Style’.
2种解决方式
方式1 ----修改gradle 版本为 2.14.1 修改plugin Version版本为2.2.3
方式2 -----升级到 classpath ‘com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.4’,Gradle用3.3的就能解决这个问题
5、gradle aar重命名
android.libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith("release.aar")) {
def fileName = "XXX.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
后续继续更新
有问题的可以下方留言,有好的解决方法我这里也会记录下来
暂时更新这么多,之后有问题会随时往下更新