android 包冲突解决 以及 解决包冲突后出现新的问题 的解决

2,909 阅读1分钟

1.包冲突

程序使用了CameraX,后来导入了selenium,就出现问题了,错误如下:

Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules guava-25.0-jre.jar (com.google.guava:guava:25.0-jre) and listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)

Go to the documentation to learn how to Fix dependency resolution errors.

com.google.guavaguavalistenablefuture俩模块都引用了ListenableFuture,属于依赖包冲突。经过一番折腾,找到了解决方案。 在冲突的module内,找到build.gradle,在android{}内添加configurations,如下:

android {
    ......
    configurations {
        all*.exclude group: 'com.google.guava', module: 'listenablefuture'
        all*.exclude group: 'com.google.guava', module: 'guava'
    }
    ......
}

2.新的问题

解决包冲突后,出现了新的BUG,如下:

More than one file was found with OS independent path 'META-INF/versions/9'

这是依赖包冲突遗留问题,工程多个jar包引用同一个文件,生成多个META-INF/versions/9文件,故提示出错。 解决方法:在依赖包冲突的module或者引用了该modulemodulebuild.gradle文件的android{}内添加packagingOptions,如下:

android{
    ......
    packagingOptions {
        exclude 'META-INF/*'
        exclude 'META-INF/versions/9'
    }
    ......
}

3.顺便记录查看依赖树的方法

Terminal内

gradlew :xxxx:dependencies --configuration compile/implementation
//xxxx代表module名称,不要忽略module名前面的“:”号
//导包若用compile则configuration参数后面用compile,若是implementation则用implementation
//可以查看简单的依赖包

gradlew :xxxx:dependencies
//同上
//可以查看详细的依赖树