使用 Guava 编译 google 云服务客户端库

compiling google cloud service client library with guava

我正在使用 android 工作室。我正在开发一个带有 google 云应用引擎(端点)和 google 云存储的应用程序。当我编写 gradle 依赖项时,如下所示:

dependencies {
    compile 'com.google.apis:google-api-services-storage:v1beta2-rev77-1.20.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:multidex:1.0.1'
    compile project(path: ':backend', configuration: 'android-endpoints')


    compile files('libs/joda-time-2.8.2.jar')
    compile ('com.google.appengine.tools:appengine-gcs-client:0.4.4')
    compile files('libs/guava-18.0.jar')
}

我在编译时检索到一个错误:

Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
 java.util.zip.ZipException: duplicate entry: com/google/common/reflect/TypeToken$TypeCollector$ForwardingTypeCollector.class

我试过这样排除

compile ('com.google.appengine.tools:appengine-gcs-client:0.4.4'){
    exclude group: 'com.google.guava'
}

但没有任何效果。有人可以帮助我吗?

1) 首先,让我们从您的依赖项中删除这个丑陋的罐子。我是关于 joda-time 和 guava

compile 'joda-time:joda-time:2.8.2'  
compile 'com.google.guava:guava:18.0'     

2) 将 exclude group: 'com.google.guava'appengine 移动到 apis

compile ('com.google.apis:google-api-services-storage:v1beta2-rev77-1.20.0') {
        exclude group: 'com.google.guava'
}

并将其添加到您的 :backend

compile (project(path: ':backend', configuration: 'android-endpoints')) {
    exclude group: 'com.google.guava'
}

3) 现在你可以得到这个错误 com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '...java'' finished with non-zero exit value 1
如果是,我们应该将排除添加到 appengine

compile('com.google.appengine.tools:appengine-gcs-client:0.4.4') {
    exclude group: 'javax.transaction'
}

4) 现在你得到 Duplicate files copied in APK META-INF/NOTICE.txt
我们用 android {} 部分

中的 packagingOptions 来解决这个问题
packagingOptions {
        exclude 'META-INF/NOTICE.txt'
}

所以你的最终结果

android {
    ...
    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
    }
}
dependencies {
    compile (project(path: ':backend', configuration: 'android-endpoints')) {
        exclude group: 'com.google.guava'
    }

    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:multidex:1.0.1'

    compile 'joda-time:joda-time:2.8.2'
    compile 'com.google.guava:guava:18.0'

    compile ('com.google.apis:google-api-services-storage:v1beta2-rev77-1.20.0') {
        exclude group: 'com.google.guava'
    }
    compile('com.google.appengine.tools:appengine-gcs-client:0.4.4') {
        exclude group: 'javax.transaction'
    }
}