美文网首页
Android Studio Gradle依赖冲突解决思路

Android Studio Gradle依赖冲突解决思路

作者: 岁月静好浅笑安然 | 来源:发表于2019-08-22 15:10 被阅读0次

1.Terminal 运行命令gradlew -q :app:dependencies查看依赖。

输出示例

+--- com.android.support:appcompat-v7:25.2.0 -> 27.1.1
|    +--- com.android.support:support-annotations:27.1.1
|    +--- com.android.support:support-core-utils:27.1.1
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    \--- com.android.support:support-compat:27.1.1
|    |         +--- com.android.support:support-annotations:27.1.1
|    |         \--- android.arch.lifecycle:runtime:1.1.0
|    |              +--- android.arch.lifecycle:common:1.1.0
|    |              \--- android.arch.core:common:1.1.0
|    +--- com.android.support:support-fragment:27.1.1
|    |    +--- com.android.support:support-compat:27.1.1 (*)
|    |    +--- com.android.support:support-core-ui:27.1.1
|    |    |    +--- com.android.support:support-annotations:27.1.1
|    |    |    +--- com.android.support:support-compat:27.1.1 (*)
|    |    |    \--- com.android.support:support-core-utils:27.1.1 (*)
|    |    +--- com.android.support:support-core-utils:27.1.1 (*)
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    +--- android.arch.lifecycle:livedata-core:1.1.0
|    |    |    +--- android.arch.lifecycle:common:1.1.0
|    |    |    +--- android.arch.core:common:1.1.0
|    |    |    \--- android.arch.core:runtime:1.1.0
|    |    |         \--- android.arch.core:common:1.1.0
|    |    \--- android.arch.lifecycle:viewmodel:1.1.0
|    +--- com.android.support:support-vector-drawable:27.1.1
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    \--- com.android.support:support-compat:27.1.1 (*)
|    \--- com.android.support:animated-vector-drawable:27.1.1
|         +--- com.android.support:support-vector-drawable:27.1.1 (*)
|         \--- com.android.support:support-core-ui:27.1.1 (*)
+--- com.android.support:support-v4:27.1.1
|    +--- com.android.support:support-compat:27.1.1 (*)
|    +--- com.android.support:support-media-compat:27.1.1
|    |    +--- com.android.support:support-annotations:27.1.1
|    |    \--- com.android.support:support-compat:27.1.1 (*)
|    +--- com.android.support:support-core-utils:27.1.1 (*)
|    +--- com.android.support:support-core-ui:27.1.1 (*)
|    \--- com.android.support:support-fragment:27.1.1 (*)
+--- com.android.support:design:27.1.1

分析技巧

  • 1直接将输出复制出来到notepad++,使用ctrl+f查找->,因为有不同版本,就会使用 ->符号列出区别。

冲突解决

  • 1.逐个排查(此方法运行报错,还不知道原因)
implementation('junit:junit:4.12'){
  exclude group : 'org.hamcrest',module:'hamcrest-core'
}
//最终,如果我们向包含1.3版本到构建中,我们可以从“mockito"中排除他
androidTestImplementatione('org.mockito:mockito-core:1.10.19'){
  exclude group : 'org.hamcrest',module:'hamcrest-core'
}
  • 3.强制指定
//在app.gradle 中的Android闭包中使用
 configurations.all{
       resolutionStrategy{
        force 'com.android.support:support-v4:27.1.1'
        force 'com.android.support:appcompat-v7:27.1.1'
      
       }
    }
//在build.gradle 中设置工程全局强制依赖

allprojects{
      configurations.all {
           resolutionStrategy.force 'com.android.support:appcompat-v7:28.0.0'
           resolutionStrategy.force 'com.android.support:support-v4:28.0.0'
           resolutionStrategy.force 'com.android.support:animated-vector-drawable:28.0.0'
           resolutionStrategy.force 'com.android.support:support-media-compat:28.0.0'
      }
}

也有人这样写build.gradle

   configurations.all{
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == 'com.android.support') {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion '27.1.1'
                }
            }
        }
         }

相关文章

网友评论

      本文标题:Android Studio Gradle依赖冲突解决思路

      本文链接:https://www.haomeiwen.com/subject/ddpnsctx.html