有 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 文件
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
解决方案。但它不起作用。
最佳答案
您遇到的问题是由冲突的依赖项引起的,您的两个依赖项定义了相同的类。如果您尝试使用
进行编译./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
关于java - Kotlin 编译库失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49332354/