Gradle插件相关的问题记录一下

笪建章
2023-12-01

Gradle插件遇到的问题

切换到As工程后或多或少的遇到了一些问题,以前的时候总是解决后不记录,写这个是为了做个备份,以方便以后开发使用,如有错误的请留言指正,感谢!!!。

一、gradle插件版本和gradle版本对应关系

参见链接: https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle

Plugin versionRequired Gradle version
1.0.0 - 1.1.32.2.1 - 2.3
1.2.0 - 1.3.12.2.1 - 2.9
1.5.02.2.1 - 2.13
2.0.0 - 2.1.22.10 - 2.13
2.1.3 - 2.2.32.14.1+
2.3.0+3.3+
3.0.0+4.1+
3.1.0+4.4+
3.2.0 - 3.2.14.6+
3.3.0 - 3.3.34.10.1+
3.4.0 - 3.4.35.1.1+
3.5.0 - 3.5.45.4.1+
3.6.0 - 3.6.45.6.4+
4.0.0+6.1.1+
4.1.0+6.5+

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

3、代理问题:

执行gradlew build。第一次的时候会下载gradle相关内容。这个时候报错了代理访问报错。

报错被截断。错误信息就是加载3.5.3加载不出来,一直报错访问时代理失败
Could not resolve all artifacts for configuration ':classpath'.    > Could not resolve com.android.tools.build:gradle:3.5.3.

于是我修检查了下我的代理配置。
(1)File->Settings -> HTTP Proxy -> No Proxy
(2)检查 gradle.properties位置在:C:\Users.gradle\ 查看里面是否包含了代码信息。删除掉。

二、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 r u n c l o s u r e 1 _run_closure1 runclosure1_closure6 c l o s u r e 13 ) v a l u e s : [ b u i l d 6 i 4 c 6 i 01 x 2 c 4 b m y r w v j r s m e f 3 _closure13) values:[build_6i4c6i01x2c4bmyrwvjrsmef3 closure13)values:[build6i4c6i01x2c4bmyrwvjrsmef3_run_closure1 c l o s u r e 6 _closure6 closure6_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)
            }
        }
    }

6、Manifest merger failed with multiple errors, see logs

查看详细日志:

gradlew processDebugManifest --stacktrace

然后根据日志去处理对应的问题(在mac下执行的命令是:./gradlew processDebugManifest --stacktrace(即在命令前面加上./))

7、The targetSdk version should not be declared in the android manifest file

修正:targetsdk设置应放到build.gradle文件中

INFO: The targetSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.

8、com.android.ide.common.signing.KeytoolException:Failed to read key meihuo from store

解决方法:检查alias是否存在,检查keystore密码和密码是否匹配正确

查看密钥相关内容:

keytool -list -v -keystore "keystore文件"

从RSA文件中查看密钥信息

keytool -printcert -file xxx.RSA

9、More than one file was found with OS independent path ‘qihoohttp/okhttp3/internal/publicsuffix/publicsuffixes.gz’

解决方法:

android{
	packagingOptions {
        exclude 'publicsuffixes.gz'
    }
}

类似于这种问题,哪里报错,处理哪里

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/ASL2.0'
} 

关于packagingOptions的属性相关,可查看:
链接: 参考文档.

10、AndroidStudio修改包名报错:No matching client found for package name

正常情况下我们在androidStudio中修改包名直接修改applictaionId即可。但是这次我修改了我的工程后发现并不能修改成功,原因就是我集成了google服务,需要把google-service.json中的包名也改一下。

11、Could not find method viewBinding() for arguments

解决问题:
升级AndroidStudio版本至少到3.6版本,因为viewBinding功能是在3.5.6才变为AS的一部分。升级版本后注意相应的gradle插件的版本也要注意提升

后续继续更新

有问题的可以下方留言,有好的解决方法我这里也会记录下来

暂时更新这么多,之后有问题会随时往下更新

 类似资料: