Kotlin无法编译库 [英] Kotlin fails to compile a library
问题描述
我创建了一个此库,用于通过电子邮件报告异常.它可以与Android Java项目一起很好地工作,但不能与Android Kotlin一起工作.当我为库(compile 'com.theah64.bugmailer:bugmailer:1.1.9')
添加编译脚本并尝试构建APK时,出现以下错误.
There's this library I created to report exceptions via email. It works well with the Android Java project but fails with Android Kotlin. When I add the compile script for the libary (compile 'com.theah64.bugmailer:bugmailer:1.1.9')
and tries to build the APK, am getting below error.
Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
这是我应用程序的build.gradle文件
This is my app's build.gradle file
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
解决方案.但这是行不通的.
I've googled a lot and tried the multiDexEnabled
solution. but it doesn't work.
推荐答案
您遇到的问题是由相互依赖的依赖项引起的,其中两个依赖项定义了相同的类.如果您尝试使用
The problem you are having is caused by conflicting dependencies, 2 of your dependencies are defining the same classes. If you try to compile with
./gradlew --stacktrace app:assembleDebug
您会看到此错误
Caused by: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/MagicConstant;
现在,您可以使用来分析所有依赖关系树
Now, you can analyze all the dependency trees with
./gradlew app:dependencies
并查看这些内容(在此处简化):
And see these (simplified here):
+--- 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)被定义了两次,我认为重复的条目会更多.
So, both Kotlin std lib and bugmailer are using org.jetbrains annotation, but from 2 different modules. This causes a problem because the same class (MagicConstant in that case) is being defined twice, I think that the duplicate entries would be even more.
解决方案是排除两个传递依赖项之一,例如
The solution would be to exclude one of the 2 transitive dependencies, for instance
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
上正常使用
You will be able to compile the app, but, keep in mind that this solution is based on the assumption that bugmailer will work just fine with org.jetbrains:annotations:13.0
instead of org.jetbrains:annotations-java5:15.0
这篇关于Kotlin无法编译库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!