gradle依赖冲突的解决方式以及开发中遇到的相关问题

查找整个项目依赖关系树

要想查看整个项目的依赖传递关系,使用命令:

gradlew :app:dependencies --configuration releaseRuntimeClasspath

其中app是项目的模块名称

运行以后查找结果中的含义如下

  • x.x.x (*) 该依赖已经有了,将不再重复依赖
  • x.x.x -> x.x.x 该依赖的版本被箭头所指的版本代替
  • x.x.x -> x.x.x(*) 该依赖的版本被箭头所指的版本代替,并且该依赖已经有了,不再重复依赖

gradle依赖冲突的解决方式

  • exclude关键字
implementation('androidx.constraintlayout:constraintlayout:1.1.3') {
    //解决冲突第一种排除方式
    exclude group: 'androidx.constraintlayout', module: 'constraintlayout-solver'
}
  • configuration配置
//定义配置名称
configurations {
    //自定义配置名称
    abc {
        println 'abc'
    }
    //第二种方式解决冲突
    configuration {
        all*.exclude module: 'annotation'
    }
    //第三种方式解决冲突,强制指定
    all {
        resolutionStrategy {
            force 'androidx.fragment:fragment:1.0.0'
        }
    }
}
  • 强制指定
//第三种方式解决冲突,强制指定
configurations.all {
    resolutionStrategy {
        force 'androidx.fragment:fragment:1.0.0'
    }
}
  • transitive属性
implementation('com.google.android.material:material:1.1.0') {
    //解决冲突第四种,false排除传递依赖,true:不排除传递依赖
    transitive false
}

java.lang.NoSuchMethodError

在当前项目开发过程中引入了了腾讯云对象存储COS的库文件之后,

implementation 'com.tencent.qcloud:cosxml:5.5.5'

项目死活都跑不起来;报了以下的错误:

完整的错误日志如下:

 java.lang.NoSuchMethodError: No virtual method getHttpUrlChecked(Ljava/lang/String;)Lokhttp3/HttpUrl; in class Lokhttp3/internal/Internal; or its super classes (declaration of 'okhttp3.internal.Internal' appears in /data/app/com.hlm.wohe-XpG2pwvZh31wjFos-V_Pcw==/base.apk!classes4.dex)
        at okhttp3.internal.huc.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:358)
        at okhttp3.internal.huc.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:343)
        at okhttp3.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:412)
        at okhttp3.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:551)
        at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

这里报的是java.lang.NoSuchMethodError异常,,okhttp3库文件方法找不到,可能的原因就是引用了最新的库文件,找不到这个方法,所以要去除重复的库。
在这里插入图片描述
这个时候就变成了解决okhttp3库文件重复引用的问题;操作如下
运用exclude group 关键字就可以

    implementation ('com.tencent.qcloud:cosxml:5.5.5'){	//所加的第三方框架
        exclude group:'com.squareup.okhttp3', module: 'okhttp' // 加载时排除框架中的okhttp包
    }

这样不用框架中的okhttp包,而用自己项目中的okhttp包

另附:

More than one file was found with OS independent path ‘META-INF/DEPENDENCIES’

在这里插入图片描述
大概意思就是工程生成了不止一个META-INF/DEPENDENCIES文件,看起来是因为多个 jar 包里包含了同样的文件(DEPENDENCIES.txt),导致打包时因为担心相互覆盖问题而提示出错,在module的build.gradle中加入如下配置项:

		 packagingOptions {
		        exclude 'proguard-project.txt'
		        exclude 'project.properties'
		        exclude 'META-INF/LICENSE.txt'
		        exclude 'META-INF/LICENSE'
		        exclude 'META-INF/NOTICE.txt'
		        exclude 'META-INF/NOTICE'
		        exclude 'META-INF/DEPENDENCIES.txt'
		        exclude 'META-INF/DEPENDENCIES'
		    }

Duplicate class

Duplicate class org.apache.commons.logging.Log found in modules jetified-commons-logging-1.1 (commons-logging:commons-logging:1.1.1) and jetified-commons-logging-api-1.1 (commons-logging:commons-logging-api:1.1)
在这里插入图片描述
解决方法:
第一步 :

在Terminal运行 gradlew app:dependencies 命令

第二步 :

(ctrl+F)或者右键Find 去搜索commons-logging:commons-logging或者commons-logging:commons-logging-api
在这里插入图片描述
添加如下操作

    implementation ('com.github.junrar:junrar:0.7'){
        exclude group:'commons-logging',module:'commons-logging'
    }

版权声明:本文为eyishion原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。