在开发游戏SDK时会使用一些第三方库,例如上文提到的 OkHttp ,或者集成某些第三方,而这些第三方使用的系统库(例如 supportv7)和项目组使用的版本不一致,在编译时就会出现版本冲突问题。解决办法有两个:
报错原因:按字面意思就是找不到对应的资源。
解决思路:
android.useAndroidX=true
(表示“Android插件会启用对应的AndroidX库,而非Support库”;)compileSdkVersion
版本提相对高一点,比如 29,不必一定和targetVersion
保持一致。2.A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
3. java.lang.ClassNotFoundException: Didn’t find class "android.support.v4.content.ContextCompat”
解决冲突一般思路
使用gradle
命令分析 dependencies
,查看哪里出现冲突,在对应的第三方文件中去除对应的冲突文件。 查看依赖树的方法有三种:
./gradlew app:dependencies
,app
代表需要分析的module
。./gradlew app:dependencies --configuration releaseRuntimeClasspath
,releaseRuntimeClasspath
指具体的variants
类型。./gradlew :app:dependencies --configuration implementation
只查看 implementation
的依赖树。gradle
-app
-tasks
-help
-dependencies
运行。./gradlew :app:dependencyInsight --dependency <依赖名> --configuration compile
分析依赖树
+--- androidx.appcompat:appcompat:1.2.0
| | | +--- androidx.annotation:annotation:1.1.0
| | | +--- androidx.core:core:1.3.0 -> 1.3.1
| | | | +--- androidx.annotation:annotation:1.1.0
| | | | +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.1.0
| | | | | +--- androidx.lifecycle:lifecycle-common:2.1.0
| | | | | | \--- androidx.annotation:annotation:1.1.0
| | | | | +--- androidx.arch.core:core-common:2.1.0
| | | | | | \--- androidx.annotation:annotation:1.1.0
| | | | | \--- androidx.annotation:annotation:1.1.0
| | | | +--- androidx.versionedparcelable:versionedparcelable:1.1.0
| | | | | +--- androidx.annotation:annotation:1.1.0
| | | | | \--- androidx.collection:collection:1.0.0 -> 1.1.0
| | | | | \--- androidx.annotation:annotation:1.1.0
| | | | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)
| | | +--- androidx.cursoradapter:cursoradapter:1.0.0
| | | | \--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | +--- androidx.fragment:fragment:1.1.0
| | | | +--- androidx.annotation:annotation:1.1.0
| | | | +--- androidx.core:core:1.1.0 -> 1.3.1 (*)
| | | | +--- androidx.collection:collection:1.1.0 (*)
| | | | +--- androidx.viewpager:viewpager:1.0.0
| | | | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | | | +--- androidx.core:core:1.0.0 -> 1.3.1 (*)
| | | | | \--- androidx.customview:customview:1.0.0
| | | | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0
| | | | | \--- androidx.core:core:1.0.0 -> 1.3.1 (*)
*
的表示这个依赖被忽略,因为其他顶级依赖中也依赖了这个传递的依赖, Gradle 会自动分析下载最适合的依赖。排除版本
排除掉某个 module
// 从 com.google.android.gms:play-services-base:15.0.1 这个依赖库中排除掉 com.android.support 组,包括 supportv7、supportv4
api("com.google.android.gms:play-services-base:15.0.1") {exclude group: 'com.android.support'
}
// 其他示例
implementation("com.github.bumptech.glide:glide:4.13.0"){exclude group:"androidx.vectordrawable"exclude group:"androidx.viewpager"exclude group:"androidx.loader"}// 从 com.google.android.gms:play-services-base:15.0.1 这个依赖库中排除掉 supportV4
api("com.google.android.gms:play-services-base:15.0.1") {exclude module: 'support-v4'
}
统一指定一个版本
configurations.all {
// 遍历所有依赖库resolutionStrategy.eachDependency { DependencyResolveDetails details ->def requested = details.requested
// 查到所有 support 库if (requested.group == 'com.android.support') {
// multidex 使用 1.0.3 ,其他使用 27.0.2 版本if (!requested.name.startsWith("multidex")) {details.useVersion '27.0.2'}else {details.useVersion '1.0.3'}}}}
或者使用以下方法指定统一版本
configurations.all {resolutionStrategy {force 'com.android.support:support-v4:28.0.0'}
}
报错信息1:java.lang.NoSuchFieldError: No field Companion of type Lokhttp3/MediaType$Companion; in class Lokhttp3/MediaType; or its superclasses (declaration of ‘okhttp3.MediaType’ appears in /data/app/XXX.bilibili-l1q_xuHU_zW9STBi7KFdug==/base.apk)
解决办法:
https://square.github.io/okhttp/changelogs/upgrading_to_okhttp_4/ 查看更新,4.x 可以兼容 3.x。 版本号不变,使用老版本的方法:
//使用 MediaType.parse ,不使用 MediaType.Comparion 如果是 3.9.0 以下也不能使用 MediaType.get
// RequestBody.create 新版本是参数在前类型在后
// 3.X 版本是 类型在前RequestBody.create(JSON, JsonUtils.map2jsonObject(params.getParams()).toString());
报错信息2:Failed resolution of: Lokhttp3/internal/Version;
解决办法:在项目里增加 一个和 okhttp 库的同样缺少的包名和类。
解决版本冲突还有一种方法,就是批量修改了第三方的包名,使用的时候使用的修改后的 的 jar 包,这样能保证和其他人使用的第三方库不冲突。但是这个方法有个很大的弊端,不建议使用。 弊端是,这种修改的方式是无法修改 反射调用的类名包名的,这样修改 jar 包包名后反射调用的方法就会出错。
修改第三方 jar 包包名方法:
1、下载一个工具 jarjar
,官网下载地址:code.google.com/p/jarjar/
2、新建一个文件夹,将jarjar
和需求修改包名的 jar 放到这个文件夹下,使用 txt 编写一个修改规则。
3、txt 中输入内容:
rule pattern result
# org.apache.commons 表示我们需要修改的 jar 包的包名,
# org.apache.commons.android.@1 表示我们需要修改的包名
rule org.apache.commons.** org.apache.commons.android.@1
4、通过 cmd 执行命令行修改
java -jar jarjar.jar process