Kotlin无法编译库

时间:2018-03-17 03:59:08

标签: java android gradle kotlin

我创建的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'
}

我已经搜索了很多内容并尝试了multiDexEnabled解决方案。但它不起作用。

1 个答案:

答案 0 :(得分: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'
}

您将能够编译该应用,但请注意,此解决方案基于以下假设:使用org.jetbrains:annotations:13.0代替org.jetbrains:annotations-java5:15.0

,错误编制程序可以正常工作
相关问题