Kotlin、Java、multidex、Dagger 2、Butterknife 和 Realm:transformClassesWithJarMergingForDebug:重复条目:org/je

Posted

技术标签:

【中文标题】Kotlin、Java、multidex、Dagger 2、Butterknife 和 Realm:transformClassesWithJarMergingForDebug:重复条目:org/jetbrains/annotations/NotNull.class【英文标题】:Kotlin,Java,multidex,Dagger 2,Butterknife and Realm: transformClassesWithJarMergingForDebug: duplicate entry: org/jetbrains/annotations/NotNull.class 【发布时间】:2017-02-01 07:02:30 【问题描述】:

我们有现有的 Java android 代码。我们想轻松地慢慢开始迁移到 Kotlin。我们使用 Dagger 2、Butterknife 和 Realm。我们使用 Java 8 编译器(但我们的 targetCompatibilitysourceCompatibility 是 1.7)。

我已经在网上漫游了几个小时,看到了 SO、GitHub 对话等,并设法解决了除一个以外的所有问题:

构建成功,但是在尝试调试时,我们得到:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/jetbrains/annotations/NotNull.class

有没有办法解决 Gradle 中的重复条目(可能使用 packagingOptionsresolutionStrategy?因为它是来自插件的重复项...?)?

再次 - build project 没有任何错误,只有 transformClassesWithJarMergingForDebug 失败!

这是应用程序的 gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
//apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'

kapt  generateStubs = true 

android 
    signingConfigs 
        config 
            keyAlias 'MyAndroidKey'
            keyPassword 'password'
            storeFile file('android.jks')
            storePassword 'password'
        
    
    compileSdkVersion 23
    buildToolsVersion '23.0.3'
    defaultConfig 
        vectorDrawables.useSupportLibrary = true
        applicationId "com.acme.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        //testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        testInstrumentationRunner "com.acme.app.utils.CustomAndroidJUnitRunner"

        // http://***.com/a/26515860/587467:
        multiDexEnabled true
    
    //dexOptions  incremental false 
    buildTypes 
        release 
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        
    
    packagingOptions 
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    

    configurations.all 
        resolutionStrategy.force 'com.android.support:support-annotations:23.3.0', 'junit:junit:4.12'
    

    targetCompatibility = 1.7
    sourceCompatibility = 1.7
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    
    sourceSets  main.java.srcDirs += 'src/main/kotlin' 


dependencies 
    // http://***.com/a/26515860/587467:
    compile 'com.android.support:multidex:1.0.1'

    compile fileTree(include: ['*.jar'], dir: 'libs')

    /// <Firebase> Cloud Messaging (FCM) - the Google Cloud Messaging (GCM) new version:
    compile 'com.google.firebase:firebase-messaging:9.0.2'
    /// </Firebase>

    /// <Dagger>
    //provided 'javax.annotation:jsr250-api:1.0'
    //apt 'com.google.dagger:dagger-compiler:2.5'
    //compile 'com.google.dagger:dagger:2.5'
    //compile 'com.google.dagger:dagger-compiler:2.4'
    /// </Dagger>

    /// <KotlinDagger>
    compile 'com.google.dagger:dagger:2.5'
    kapt 'com.google.dagger:dagger-compiler:2.5'
    kaptTest 'com.google.dagger:dagger-compiler:2.5'
    kaptAndroidTest 'com.google.dagger:dagger-compiler:2.5'
    //provided 'org.glassfish:javax.annotation:10.0-b28'
    /// </KotlinDagger>

    /// <JSON>
    //testCompile 'javax.json:javax.json-api:1.0'
    testCompile 'org.glassfish:javax.json:1.0.4'
    // https://github.com/FasterXML/jackson-docs/wiki/Using-Jackson2-with-Maven
    // http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/
    //testCompile 'com.fasterxml.jackson.core:jackson-core:2.6.3'
    testCompile 'com.fasterxml.jackson.core:jackson-databind:2.7.4'
    /// </JSON>

    //apt 'com.jakewharton:butterknife-compiler:8.0.1'
    kapt 'com.jakewharton:butterknife-compiler:8.0.1'

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'net.java.dev.jna:jna-platform:4.2.2'

    // <InstrumentedTests>
    // <MutualExclusive>
    // <Or>
          //androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    // </Or>
    // <Or>
    //    androidTestCompile 'com.android.support:support-annotations:23.0.0'
        androidTestCompile 'com.android.support.test:runner:0.4'
        //androidTestCompile 'com.android.support.test:rules:0.4'
        // <Espresso>
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
        // </Espresso>
        //androidTestCompile "org.robolectric:robolectric:3.0"
    // </Or>
    // </MutualExclusive>
    // </InstrumentedTests>

    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:support-v13:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.jakewharton:butterknife:8.0.1'
    compile 'com.google.code.ksoap2-android:ksoap2-android:3.6.1'
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'

    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:recyclerview-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'joda-time:joda-time:2.9.4'
    //compile 'io.reactivex:rxjava:1.1.6'
    compile 'io.reactivex:rxjava-async-util:0.21.0'
    //<Floating Buttons>
    compile 'com.github.clans:fab:1.6.4'
    //</Floating Buttons>

    compile 'com.facebook.fresco:fresco:0.11.0'
    compile 'org.jetbrains:annotations-java5:15.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.7.4'
    compile 'com.google.code.gson:gson:2.4'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.3"


// https://firebase.google.com/docs/android/setup#add_the_sdk
// Firebase Cloud Messaging (FCM) - the Google Cloud Messaging (GCM) new version:
apply plugin: 'com.google.gms.google-services'
repositories 
    mavenCentral()

【问题讨论】:

【参考方案1】:

尝试删除:compile 'org.jetbrains:annotations-java5:15.0'。如果它不起作用,请按照以下步骤操作:

转到您的 app/build.gradle 文件并添加:

configurations 
    cleanedAnnotations

    compile.exclude group: 'org.jetbrains' , module:'annotations'

改变

compile 'org.jetbrains:annotations-java5:15.0'

 compile files("$buildDir/libs/annotations-cleaned.jar")  builtBy 'cleanAnnotationsJar' 
    cleanedAnnotations 'org.jetbrains:nnotations-java5:15.0'

最后补充:

task cleanAnnotationsJar(type:Jar) 
    configurations.cleanedAnnotations.each  f ->
        from zipTree(f)
    
    archiveName = "annotations-cleaned.jar"
    exclude 'org/jetbrains/annotations/NotNull.class'
    exclude 'org/jetbrains/annotations/Nullable.class'

检查:https://discuss.kotlinlang.org/t/duplicate-classes-in-kotlin-runtime-and-com-intellij-annotations/154/3

【讨论】:

谢谢,成功了!但我似乎很难理解 Gradle 调整:如果你能用简单的语言解释第二个解决方案的作用,我们将不胜感激! 第二种解决方案取自下面的链接。它执行Gradle 任务以查看Nullable.classNotNull.class 或是否加倍。如果是,则仅从 cleanAnnotations 依赖项中获取。 如果有效,请将其标记为其他有类似问题的答案。很高兴我能帮上忙【参考方案2】:

我将compile ("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version") 更改为compile ("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version") exclude group: 'org.jetbrains', module: 'annotations' ,有效。试试看!

【讨论】:

以上是关于Kotlin、Java、multidex、Dagger 2、Butterknife 和 Realm:transformClassesWithJarMergingForDebug:重复条目:org/je的主要内容,如果未能解决你的问题,请参考以下文章

As 设置Multidex出现java.lang.NoClassDefFoundError

Multidex、Proguard 和 Firebase

Proguard 可以保存到 multidex 应用程序吗?

Android应用使用Multidex突破64K方法数限制

Android应用使用Multidex突破64K方法数限制

Android 使用android-support-multidex解决Dex超出方法数的限制问题