Gradle解决依赖冲突

Posted by CoXier on February 27, 2018

在项目中遇到了一个 Bug :

No resource found that matches the given name ‘android:TextAppearance.Material.Widget.Button.Borderless.Colored’.

经过请教,发现问题来自 compileSdkVersion 和 依赖的第三方库的 support 版本不同,compileSdkVersion 版本低于 support 版本,在编译的时,就会找不到一些 support 需要的资源。解决的方法:

 compile(xxx) {
     exclude module: 'support-v4'
     exclude module: 'appcompat-v7'
 }

关键字 exclude 。参考 https://medium.com/mindorks/avoiding-conflicts-in-android-gradle-dependencies-28e4200ca235

如果你需要展示依赖关系的话,碰巧在解决上面的过程中我发现了一个命令,还比较有用。

 ./gradlew -q app:dependencies --configuration debugCompileClasspath   

依赖树:

 ------------------------------------------------------------
Project :app
------------------------------------------------------------

debugCompileClasspath - Resolved configuration for compilation for variant: debug
\--- 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 (*)

(*) - dependencies omitted (listed previously)