gradle依赖版本冲突

时间:2016-04-04 11:55:49

标签: android android-studio gradle

我有以下依赖脚本:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.asdf.asdf"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildTypes.each {
        it.buildConfigField 'String', 'YOUTUBE_API_KEY', YoutubeApiKey
    }
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0-beta-2'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:0.13.2'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                javanano {
                    option 'ignore_services=true'
                }
            }

            task.plugins {
                grpc {
                    option 'nano=true'
                }
            }
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
    compile 'com.android.support:cardview-v7:23.2.1'
    compile 'com.android.support:support-annotations:23.2.1'
    compile 'com.android.support:customtabs:23.2.1'
    compile 'com.google.apis:google-api-services-youtube:v3-rev164-1.21.0'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'io.grpc:grpc-protobuf-nano:0.13.2'
    compile 'io.grpc:grpc-okhttp:0.13.2'
    compile 'io.grpc:grpc-stub:0.13.2'
    compile 'com.google.guava:guava:18.0'
}

虽然youtube库和grpc库都依赖于google guava库,但它们依赖于导致冲突的不同版本。 Youtube依赖于com.google.guava:guava-jdk5:17.0com.google.guava:guava:18.0上的grpc。(注意工件差异,如果可能相关)问题是grpc最终试图找到youtube版本的guava中定义的方法存在于其自己的依赖版本中。我该如何解决这个问题?

错误消息

FATAL EXCEPTION: SyncAdapterThread-1
    Process: com.asdf.asdf, PID: 4025
             java.lang.NoSuchMethodError: No static method directExecutor()Ljava/util/concurrent/Executor; in class Lcom/google/common/util/concurrent/MoreExecutors; or its super classes (declaration of 'com.google.common.util.concurrent.MoreExecutors' appears in /data/data/com.fentale.dalol/files/instant-run/dex/slice-guava-jdk5-17.0_a8ada10dcaf113cb6e3b4d3e5b46975833f8ae8f-classes.dex)
                at io.grpc.internal.ClientCallImpl.<init>(ClientCallImpl.java:100)
                at io.grpc.internal.ManagedChannelImpl$RealChannel.newCall(ManagedChannelImpl.java:320)
                at io.grpc.internal.ManagedChannelImpl.newCall(ManagedChannelImpl.java:299)
                at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:130)
                at com.fentale.dalol.nano.DalolGrpc$DalolBlockingStub.topPosts(DalolGrpc.java:365)

方法“directExecutor”在guava-v18中定义,但grpc尝试从guava-jdk5访问它。

3 个答案:

答案 0 :(得分:2)

我会尝试使用

排除guava-jdk5
exclude module: 'guava-jdk5'

在您的依赖项中。

问题是如果工件具有不同的名称(例如,guava和guava-jdk5),则无法检测到版本冲突。然后可能会发生加载错误的类,因为两个jar都被包含在内。

答案 1 :(得分:1)

手动指定番石榴版本

compile 'com.google.guava:guava:18.0.0' 

所以你的依赖关系将是以下

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.google.apis:google-api-services-youtube:v3-rev164-1.21.0'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'io.grpc:grpc-protobuf-nano:0.13.2'
    compile 'com.google.guava:guava:18.0.0' 
}

这样你就会强制使用番石榴v18。

或者在顶级gradle中,您可以使用

configurations.all {
    resolutionStrategy.force 'com.google.guava:guava:18.0.0'
}

答案 2 :(得分:1)

您必须编辑 build.gradle 项目级别

exclude module: 'guava-jdk5'

内部依赖项。请查看随附的屏幕截图,了解更多详情enter image description here 如果您使用的是firebase插件,请添加

classpath ('com.google.firebase:firebase-plugins:1.1.0') {
        exclude group: 'com.google.guava', module: 'guava-jdk5'
    }