Android 解决第三方库版本冲突

前言

在开发游戏SDK时会使用一些第三方库,例如上文提到的 OkHttp ,或者集成某些第三方,而这些第三方使用的系统库(例如 supportv7)和项目组使用的版本不一致,在编译时就会出现版本冲突问题。解决办法有两个:

  1. 尽量不使用第三库,例如针对 OkHttp ,如果没有特殊的需求(比如需要中断某个请求),可以不使用 OkHttp ,自行开发一个基于系统 API 的网络框架。
  2. 解决版本冲突。以下提供一些解决版本冲突的方法。

版本冲突可能出现的报错提示及对应解决方案

  1. 加入AndroidX之后 targetVersion= 26,报错:Android resource linked error

报错原因:按字面意思就是找不到对应的资源。

解决思路:

  1. 检查找不到的资源相关库是否已添加;
  2. 如已添加,查看是否出现版本冲突,可能是使用了AndroidX 的库,但是项目里有support库。
    1. 可以检查项目是否已配置:android.useAndroidX=true (表示“Android插件会启用对应的AndroidX库,而非Support库”;)
    2. compileSdkVersion 版本提相对高一点,比如 29,不必一定和targetVersion 保持一致。
  3. 清除缓存(项目里的缓存)、或者电脑里的缓存,用户/.gradle目录。
  4. 用以下介绍的一半解决版本冲突方法尝试解决。

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”

  1. 这里是示例,按报错提示是 support.v4 这个系统库版本出现版本冲突。

解决冲突一般思路

  1. 使用gradle 命令分析 dependencies,查看哪里出现冲突,在对应的第三方文件中去除对应的冲突文件。 查看依赖树的方法有三种:

    1. 在终端使用命令:./gradlew app:dependencies ,app 代表需要分析的module
    2. ./gradlew app:dependencies --configuration releaseRuntimeClasspathreleaseRuntimeClasspath 指具体的variants 类型。./gradlew :app:dependencies --configuration implementation 只查看 implementation 的依赖树。
    3. 点击右侧 gradle-app-tasks-help-dependencies 运行。
    4. 查看某个依赖库中的依赖:./gradlew :app:dependencyInsight --dependency <依赖名> --configuration compile
  2. 分析依赖树

    +--- 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 (*)
    
    1. * 的表示这个依赖被忽略,因为其他顶级依赖中也依赖了这个传递的依赖, Gradle 会自动分析下载最适合的依赖。
    2. 1.0.0 -> 1.1.0 的表示会先使用高版本,如果需要使用低版本的 API ,需要排除掉高版本。
  3. 排除版本

    1. 排除掉某个 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'
      }
      
    2. 统一指定一个版本

      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'
          }
      }
      

    4. OkHttp 4.x 版本和 3.x 版本的冲突。

    报错信息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)

    解决办法:

    1. 参考 OkHttp 官方文档:https://square.github.io/okhttp/#releases

    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 包包名后反射调用的方法就会出错。

修改第三方 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 <rulesFile> <inJar> <outJar>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值