Android Studio Gradle依赖冲突解决思路

2019-08-22  本文已影响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

分析技巧

冲突解决

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'
}
//在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'
                }
            }
        }
         }
上一篇下一篇

猜你喜欢

热点阅读