本文介绍了Kotlin,Java,multidex,Dagger 2,Butterknife和Realm:transformClassesWithJarMergingForDebug:重复条目:org / jetbrains / annotations / NotNull.class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有现有的Java Android代码。我们想无痛地慢慢开始移动到Kotlin。我们使用Dagger 2,Butterknife和Realm。我们使用Java 8编译器(但是我们的 targetCompatibility sourceCompatibility 是1.7)。

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



构建是成功的,但是当试图进行调试时,我们得到:

 错误:任务':app:transformClassesWithJarMergingForDebug'的执行失败。 
> org / jetbrains / annotations / NotNull.class

有没有办法解决Gradle中的重复条目(也许使用 packagingOptions resolutionStrategy ,因为它是来自插件的重复...?)?

再次 - 生成项目没有任何错误,只有 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'密码'
storeFile文件('android。 jks')
storePassword'password'
}
}
compileSdkVersion 23
buildToolsVersion '23 .0.3'
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationIdcom.acme.app
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName1.0
// testInstrumentationRunnerandroid。
testInstrumentationRunnercom.acme.app.utils.CustomAndroidJUnitRunner

// http://stackoverflow.com/a/26515860/587467:
multiDexEnabled true

// dexOptions {incremental false}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android。 txt'),'proguard-rules.pro'
signingConfig signingConfigs.config
}
}
p ackagingOptions {
排除'META-INF / services / javax.annotation.processing.Processor'
排除'META-INF / DEPENDENCIES.txt'
排除'META-INF / DEPENDENCIES'
排除'META-INF / dependencies.txt'
排除'META-INF / LICENSE.txt'
排除'META-INF / LICENSE'
排除'META-INF / license。 $'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$' 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 {m ain.java.srcDirs + ='src / main / kotlin'}
}

依赖关系{
// http://stackoverflow.com/a/26515860/587467:
编译'com.android.support:multidex:1.0.1'

编译fileTree(包括:['* .jar'],dir:'libs')

///< Firebase>云消息传递(FCM) - Google Cloud Messaging(GCM)新版本:
编译'com.google.firebase:firebase-messaging:9.0.2'
///< / Firebase>

///< Dagger>
//提供'javax.annotation:jsr250-api:1.0'
// apt'com.google.dagger:dagger-compiler:2.5'
// compile'c​​om.google。 dagger:dagger:2.5'
// compile'c​​om.google.dagger:dagger-compiler:2.4'
///< / Dagger>

///< KotlinDagger>
compile'c​​om.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'
//提供'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'c​​om.fasterxml.jackson.core:jackson-core:2.6.3'
testCompile'c​​om.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'c​​om.android.support.test:testing-support-lib:0.1'
//< / Or>
//< Or>
// androidTestCompile'c​​om.android.support:support-annotations:23.0.0'
androidTestCompile'c​​om.android.support.test:runner:0.4'
// androidTestCompile'c​​om。 android.support.test:rules:0.4'
//< Espresso>
androidTestCompile'c​​om.android.support.test.espresso:espresso-core:2.2.2'
//< / Espresso>
// androidTestCompileorg.robolectric:robolectric:3.0
//< / or Or>
//< / MutualExclusive>
//< / InstrumentedTests>

编译'com.android.support:appcompat-v7:23.4.0'
编译'com.android.support:support-v13:23.4.0'
编译' com.google.code.ksoap2-android:ksoap2-android:3.6。com.jakewharton:butterknife:8.0.1' 1'
compile'org.springframework.android:spring-android-rest-template:2.0.0.M3'

compile'c​​om.squareup.retrofit2:retrofit:2.1.0'
compile'c​​om.squareup.retrofit2:converter-gson:2.1.0'
compile'c​​om.squareup.retrofit2:adapter-rxjava:2.1.0'

compile' com.android.support:support-v4:23.3.0'
compile'c​​om.android.support:recyclerview-v7:23.3.0'
compile'c​​om.android.support:cardview-v7: 23.3.0'
编译'io.reactivex:rxandroid:1.2.1'
编译'joda-time:joda-time:2.9.4'
//编译'io.reactivex: rxjava:1.1.6'
编译'io.reactivex:rxjava-asy nc-util:0.21.0'
//<浮动按钮>
编译'com.github.clans:fab:1.6.4'
//< /浮动按钮>

compile'c​​om.facebook.fresco:fresco:0.11.0'
compile'org.jetbrains:annotations-java5:15.0'
compile'c​​om.fasterxml.jackson。 core:jackson-databind:2.7.4'
compile'c​​om.google.code.gson:gson:2.4'
编译'javax.annotation:javax.annotation-api:1.2'
编译org.jetbrains.kotlin:kotlin-stdlib:1.0.3
}

// https://firebase.google.com/docs/android/setup#add_the_sdk
// Firebase Cloud Messaging(FCM) - Google Cloud Messaging(GCM)新版本:
应用插件:'com.google.gms.google-services'
版本库{
mavenCentral ()
}


解决方案

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

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

 配置{
clearedAnnotations

compile.exclude组:'org.jetbrains' ,module:'annotations'
}

更改

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



 编译文件($ {buildDir} /libs/annotations-cleaned.jar){ builtBy'cleanAnnotationsJar'} 
cleanedAnnotations'org.jetbrains:nnotations-java5:15.0'

最后添加:

$ pre $ 任务cleanAnnotationsJar(类型:Jar){
configurations.cleanedAnnotations.each {f - >
from zipTree(f)
}
archiveName =annotations-cleaned.jar
exclude'org / jetbrains / annotations / NotNull.class'
exclude'org /jetbrains/annotations/Nullable.class'
}

检查:


We have existing Java Android code. We want to painlessly slowly start moving to Kotlin. We use Dagger 2, Butterknife and Realm. We use Java 8 compiler (but our targetCompatibility and sourceCompatibility are 1.7).

I've been roaming the web for hours, seen SO, GitHub conversations and so on, and managed to solve all problems except one:

The build is successful, but when trying to debug, we get:

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

Is there a way to resolve duplicate entries in Gradle (maybe using packagingOptions or resolutionStrategy? as it's a duplication that comes from the plugins...?) ?

Again - build project goes without any error, only transformClassesWithJarMergingForDebug fails!

Here's the app's 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://stackoverflow.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://stackoverflow.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()
}
解决方案

Try to delete: compile 'org.jetbrains:annotations-java5:15.0'. If it won't work, follow these steps:

Go to your app/build.gradle file and add:

configurations {
    cleanedAnnotations

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

Change

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

with

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

Finally add:

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'
}

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

这篇关于Kotlin,Java,multidex,Dagger 2,Butterknife和Realm:transformClassesWithJarMergingForDebug:重复条目:org / jetbrains / annotations / NotNull.class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:59