首页 文章

Kotlin无法编译库

提问于
浏览
2

我创建了this库来通过电子邮件报告异常 . 它适用于Android Java项目但与Android Kotlin失败 . 当我为libary (compile 'com.theah64.bugmailer:bugmailer:1.1.9') 添加编译脚本并尝试构建APK时,我得到以下错误 .

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

这是我的应用程序的build.gradle文件

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.theapache64.calculator"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            multiDexEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:27.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.theah64.bugmailer:bugmailer:1.2.0'
}

我已经google了很多,并尝试了 multiDexEnabled 解决方案 . 但它不起作用 .

1 回答

  • 3

    您遇到的问题是由相互冲突的依赖引起的,您的2个依赖项定义了相同的类 . 如果你尝试编译

    ./gradlew --stacktrace app:assembleDebug
    

    你会看到这个错误

    Caused by: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/MagicConstant;
    

    现在,您可以使用分析所有依赖关系树

    ./gradlew app:dependencies
    

    并看到这些(简化在这里):

    +--- com.theah64.bugmailer:bugmailer:1.2.0
    |    +--- org.jetbrains:annotations-java5:15.0
    

    +--- org.jetbrains.kotlin:kotlin-stdlib:1.2.30
     |    \--- org.jetbrains:annotations:13.0
    

    因此,Kotlin std lib和bugmailer都使用org.jetbrains注释,但是来自2个不同的模块 . 这会导致一个问题,因为同一个类(在这种情况下是MagicConstant)被定义了两次,我认为重复的条目会更多 .

    例如,解决方案是排除2个传递依赖项中的一个

    compile('com.theah64.bugmailer:bugmailer:1.2.0') {
        exclude group: 'org.jetbrains', module: 'annotations-java5'
    }
    

    您将能够编译该应用程序,但是,请记住,此解决方案基于假设bugmailer将与 org.jetbrains:annotations:13.0 而不是 org.jetbrains:annotations-java5:15.0 正常工作

相关问题