【问题标题】:Kotlin fails to compile a libraryKotlin 编译库失败
【发布时间】:2018-08-26 04:56:53
【问题描述】:

我创建了 this 库,用于通过电子邮件报告异常。它适用于 Android Java 项目,但不适用于 Android Kotlin。当我为库 (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 文件

应用插件:'com.android.application' 应用插件:'kotlin-android' 应用插件:'kotlin-android-extensions' 安卓 { compileSdkVersion 27 默认配置 { applicationId "com.theapache64.calculator" minSdkVersion 15 targetSdkVersion 27 版本代码 1 版本名称“1.0” testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled 真 } 构建类型 { 发布 { 缩小启用假 multiDexEnabled 真 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dex 选项 { preDexLibraries = 假 javaMaxHeapSize "4g" } } 依赖{ 实现文件树(目录:'libs',包括:['*.jar']) 实施“org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version” 实施 'com.android.support:appcompat-v7:27.0.2' 实施 'com.android.support.constraint:constraint-layout:1.0.2' 实施 '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' 编译'com.theah64.bugmailer:bugmailer:1.2.0' }

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

【问题讨论】:

标签: java android gradle kotlin


【解决方案1】:

您遇到的问题是由冲突的依赖项引起的,您的两个依赖项定义了相同的类。如果您尝试使用

进行编译
./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 标准库和 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 的假设

【讨论】:

  • 精彩的解释。 (y)
  • @theapache64 我刚刚意识到你是 bugmailer 的所有者,不是吗?我可能已经给你发了一封电子邮件然后loool :)
  • 哈哈:D,是的,这是我的项目之一。我已经修好了图书馆。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多