解决Gradle依赖关系,不同一个库不同版本的兼容问题

问题分析

系统提示错误

我们在app的build.gradle文件看见一个报错。
原因是我们设置了v7的版本com.android.support:appcompat-v7:26.1.0,但是我们在引用第三方库的时候出现了报错。大致的意思就是我们使用的v7的库和我们的recyclerview的搭配版本不相同,这会使我们的版本不兼容。

分析gradle依赖

为了进一步验证,我们使用了gradle app:dependencies | log.txt 命令,对项目的编译时间和运行时间的依赖库进行查看。

releaseRuntimeClasspath - Resolved configuration for runtime for variant: release
+--- com.android.support:appcompat-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- android.arch.lifecycle:runtime:1.0.0
|    |    |         +--- android.arch.lifecycle:common:1.0.0
|    |    |         \--- android.arch.core:common:1.0.0
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-utils:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-ui:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:support-fragment:26.1.0
|    |         +--- com.android.support:support-compat:26.1.0 (*)
|    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0
|    |    \--- com.android.support:support-compat:26.1.0 (*)
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         +--- com.android.support:support-vector-drawable:26.1.0 (*)
|         \--- com.android.support:support-core-ui:26.1.0 (*)
+--- com.android.support.constraint:constraint-layout:1.1.2
|    \--- com.android.support.constraint:constraint-layout-solver:1.1.2
+--- com.commit451:PhotoView:1.2.4
|    \--- com.android.support:support-v4:23.0.1 -> 26.1.0 (*)
+--- jp.wasabeef:glide-transformations:2.0.1
|    \--- com.github.bumptech.glide:glide:3.7.0
+--- cn.bingoogolapple:bga-refreshlayout:1.1.6
|    +--- com.nineoldandroids:library:2.4.0
|    \--- com.android.support:recyclerview-v7:24.0.0
|         +--- com.android.support:support-annotations:24.0.0 -> 26.1.0
|         \--- com.android.support:support-v4:24.0.0 -> 26.1.0 (*)
+--- com.jakewharton:butterknife:7.0.1
+--- io.reactivex:rxjava:1.2.1 -> 1.3.0
+--- io.reactivex:rxandroid:1.2.1
|    \--- io.reactivex:rxjava:1.1.6 -> 1.3.0
+--- com.squareup.okio:okio:1.13.0
+--- com.squareup.retrofit2:retrofit:2.3.0
|    \--- com.squareup.okhttp3:okhttp:3.8.0 -> 3.9.0
|         \--- com.squareup.okio:okio:1.13.0
+--- com.squareup.retrofit2:converter-scalars:2.3.0
|    \--- com.squareup.retrofit2:retrofit:2.3.0 (*)
+--- com.squareup.retrofit2:converter-gson:2.3.0
|    +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
|    \--- com.google.code.gson:gson:2.7
+--- com.squareup.retrofit2:adapter-rxjava:2.3.0
|    +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
|    \--- io.reactivex:rxjava:1.3.0
+--- com.squareup.okhttp3:okhttp:3.9.0 (*)
+--- com.squareup.okhttp3:logging-interceptor:3.9.0
|    \--- com.squareup.okhttp3:okhttp:3.9.0 (*)
+--- org.greenrobot:greendao:3.2.2
|    \--- org.greenrobot:greendao-api:3.2.2
+--- com.android.support:multidex:1.0.0
+--- com.tbruyelle.rxpermissions:rxpermissions:0.7.1
\--- com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+ -> 5.1.4

上面的标识代表着不同的含义,我来解释下:

  • * 号表示,gradle使用了相同版本或者另一个版本来作为依赖。
  • ->号,表示gradle将使用符号后面的版本作为依赖。

报错原因

  • bga-refreshlayout和v7的比较
+--- com.android.support:appcompat-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- android.arch.lifecycle:runtime:1.0.0
|    |    |         +--- android.arch.lifecycle:common:1.0.0
|    |    |         \--- android.arch.core:common:1.0.0
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-utils:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-ui:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:support-fragment:26.1.0
|    |         +--- com.android.support:support-compat:26.1.0 (*)
|    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0
|    |    \--- com.android.support:support-compat:26.1.0 (*)
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         +--- com.android.support:support-vector-drawable:26.1.0 (*)
|         \--- com.android.support:support-core-ui:26.1.0 (*)

...
+--- cn.bingoogolapple:bga-refreshlayout:1.1.6
|    +--- com.nineoldandroids:library:2.4.0
|    \--- com.android.support:recyclerview-v7:24.0.0
|         +--- com.android.support:support-annotations:24.0.0 -> 26.1.0
|         \--- com.android.support:support-v4:24.0.0 -> 26.1.0 (*)

可以看见,我们的bga-refreshlayout使用的recyclerview还是24.0.0,这就导致了我们的recyclerview的版本和当前的v7版本不兼容。

修复错误

思路

bga-refreshlayout外,其余类库均统一版本库recyclerview的版本库。

  • 统一gradle的recyclerview版本。
    implementation rootProject.ext.dependencies["recyclerview-v7"]

这里加入这段代码,项目中所有的引用库都会统一引用recyclerview

  • 配置bga-refreshlayout使用自己的版本库。
implementation(rootProject.ext.dependencies["refreshlayout"]) {
        exclude group: 'com.android.support'
    }

命令检查

releaseRuntimeClasspath - Resolved configuration for runtime for variant: release
+--- com.android.support:appcompat-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- android.arch.lifecycle:runtime:1.0.0
|    |    |         +--- android.arch.lifecycle:common:1.0.0
|    |    |         \--- android.arch.core:common:1.0.0
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-utils:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    +--- com.android.support:support-core-ui:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0
|    |    |    \--- com.android.support:support-compat:26.1.0 (*)
|    |    \--- com.android.support:support-fragment:26.1.0
|    |         +--- com.android.support:support-compat:26.1.0 (*)
|    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0
|    |    \--- com.android.support:support-compat:26.1.0 (*)
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         +--- com.android.support:support-vector-drawable:26.1.0 (*)
|         \--- com.android.support:support-core-ui:26.1.0 (*)
+--- com.android.support:support-v4:26.1.0 (*)
+--- com.android.support.constraint:constraint-layout:1.1.2
|    \--- com.android.support.constraint:constraint-layout-solver:1.1.2
+--- com.android.support:recyclerview-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0
|    +--- com.android.support:support-compat:26.1.0 (*)
|    \--- com.android.support:support-core-ui:26.1.0 (*)
+--- com.commit451:PhotoView:1.2.4
|    \--- com.android.support:support-v4:23.0.1 -> 26.1.0 (*)
+--- jp.wasabeef:glide-transformations:2.0.1
|    \--- com.github.bumptech.glide:glide:3.7.0
+--- cn.bingoogolapple:bga-refreshlayout:1.1.6
|    \--- com.nineoldandroids:library:2.4.0
+--- com.jakewharton:butterknife:7.0.1
+--- io.reactivex:rxjava:1.2.1 -> 1.3.0
+--- io.reactivex:rxandroid:1.2.1
|    \--- io.reactivex:rxjava:1.1.6 -> 1.3.0
+--- com.squareup.okio:okio:1.13.0
+--- com.squareup.retrofit2:retrofit:2.3.0
|    \--- com.squareup.okhttp3:okhttp:3.8.0 -> 3.9.0
|         \--- com.squareup.okio:okio:1.13.0
+--- com.squareup.retrofit2:converter-scalars:2.3.0
|    \--- com.squareup.retrofit2:retrofit:2.3.0 (*)
+--- com.squareup.retrofit2:converter-gson:2.3.0
|    +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
|    \--- com.google.code.gson:gson:2.7
+--- com.squareup.retrofit2:adapter-rxjava:2.3.0
|    +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
|    \--- io.reactivex:rxjava:1.3.0
+--- com.squareup.okhttp3:okhttp:3.9.0 (*)
+--- com.squareup.okhttp3:logging-interceptor:3.9.0
|    \--- com.squareup.okhttp3:okhttp:3.9.0 (*)
+--- org.greenrobot:greendao:3.2.2
|    \--- org.greenrobot:greendao-api:3.2.2
+--- com.android.support:multidex:1.0.0
+--- com.tbruyelle.rxpermissions:rxpermissions:0.7.1
\--- com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+ -> 5.1.4

看上面的依赖,问题得以解决。

参考文献

  • 【译】Gradle 的依赖关系处理不当,可能导致你编译异常
  • support v4/design/v7依赖的冲突的解决

你可能感兴趣的:(bug)