与java8和Kotlin错误

我有一个应用程序,我在使用retrolambda,所以在build.gradle我有

compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } 

一切都很好,除非我加入对Kotlin的支持。 添加Kotlin插件我得到以下错误:

错误:将字节码转换为dex时出错:原因:Dex无法解析版本52字节码。 这是由使用Java 8或更高版本编译的库依赖性引起的。 如果在库子模块中使用“java”gradle插件,则将targetCompatibility ='1.7'sourceCompatibility ='1.7'添加到该子模块的build.gradle文件中。

我发现许多类似于这个问题的答案,但没有解决方案适用于我的情况。

这是我的build.gradle:

 buildscript { repositories { maven { url 'https://maven.fabric.io/public' } mavenCentral() } dependencies { classpath 'me.tatarka:gradle-retrolambda:3.3.1' classpath 'io.fabric.tools:gradle:1.+' } } repositories { // Required because retrolambda is on maven central mavenCentral() maven { url 'https://maven.fabric.io/public' } maven { url 'https://maven.google.com' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric' apply plugin: 'realm-android' apply plugin: 'com.neenbedankt.android-apt' apply plugin: 'kotlin-android' plugins { id "me.tatarka.retrolambda" version "3.3.1" } android { compileSdkVersion 25 buildToolsVersion '25.0.2' compileOptions.incremental = false compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig { multiDexEnabled true applicationId "mypackage" minSdkVersion 17 targetSdkVersion 25 versionCode 1 versionName "0.1" vectorDrawables.useSupportLibrary = true jackOptions { enabled false } testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" dexOptions { preDexLibraries = false javaMaxHeapSize "4g" } } compileOptions { incremental true } buildTypes { release { debuggable false minifyEnabled true zipAlignEnabled true renderscriptDebuggable false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } debug { debuggable true signingConfig signingConfigs.debug minifyEnabled false versionNameSuffix "_dev" } } } def supportVersion = '25.3.1' dependencies { def daggerVer = 2.8 apt "com.google.dagger:dagger-compiler:$daggerVer" compile "com.google.dagger:dagger:$daggerVer" compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:multidex:1.0.1' compile "com.android.support:appcompat-v7:$supportVersion" compile "com.android.support:design:$supportVersion" compile "com.android.support:cardview-v7:$supportVersion" compile "com.android.support:recyclerview-v7:$supportVersion" compile "com.android.support:gridlayout-v7:$supportVersion" compile "com.android.support:support-annotations:$supportVersion" compile "com.android.support:preference-v7:$supportVersion" compile "com.android.support:support-v4:$supportVersion" compile "com.android.support:support-vector-drawable:$supportVersion" compile "com.android.support:palette-v7:$supportVersion" def firebase = '10.2.0' compile "com.google.firebase:firebase-crash:$firebase" compile "com.google.firebase:firebase-auth:$firebase" compile 'com.firebaseui:firebase-ui-auth:1.2.0' def rxbinding = '2.0.0' compile "com.jakewharton.rxbinding2:rxbinding:$rxbinding" compile "com.jakewharton.rxbinding2:rxbinding-support-v4:$rxbinding" compile "com.jakewharton.rxbinding2:rxbinding-appcompat-v7:$rxbinding" compile "com.jakewharton.rxbinding2:rxbinding-design:$rxbinding" compile "com.jakewharton.rxbinding2:rxbinding-recyclerview-v7:$rxbinding" compile "com.jakewharton.rxbinding2:rxbinding-leanback-v17:$rxbinding" compile 'joda-time:joda-time:2.5' compile 'com.ryanharter.auto.value:auto-value-parcel-adapter:0.2.5' compile 'com.jakewharton.timber:timber:4.3.1' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.github.clans:fab:1.6.4' compile 'me.relex:circleindicator:1.2.2@aar' compile 'com.github.paolorotolo:appintro:4.1.0' //architecture compile "android.arch.lifecycle:runtime:1.0.0-alpha1" compile "android.arch.lifecycle:extensions:1.0.0-alpha1" annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1" def retrofit2 = '2.2.0' def okhttp3 = '3.4.1' compile "com.squareup.retrofit2:retrofit:$retrofit2" compile "com.squareup.retrofit2:adapter-rxjava2:$retrofit2" compile "com.squareup.retrofit2:converter-gson:$retrofit2" compile "com.squareup.retrofit2:converter-scalars:$retrofit2" compile "com.squareup.okhttp3:okhttp:$okhttp3" compile "com.squareup.okhttp3:okhttp-urlconnection:$okhttp3" compile "com.squareup.okhttp3:logging-interceptor:$okhttp3" compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1' compile 'com.github.MFlisar:RxBus2:0.1' def butter_knife = '8.4.0' apt "com.jakewharton:butterknife-compiler:$butter_knife" compile "com.jakewharton:butterknife:$butter_knife" def leak_canary = '1.4' debugCompile "com.squareup.leakcanary:leakcanary-android:$leak_canary" releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:$leak_canary" testCompile "com.squareup.leakcanary:leakcanary-android-no-op:$leak_canary" apt 'com.gabrielittner.auto.value:auto-value-with:1.0.0' apt 'com.google.auto.value:auto-value:1.2' apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.5' apt 'com.ryanharter.auto.value:auto-value-gson:0.3.2-rc1' provided 'javax.annotation:jsr250-api:1.0' provided 'com.google.auto.value:auto-value:1.2' compile 'com.braintreepayments.api:drop-in:3.0.6' //other dependencies for testing def hamcrestVersion = '1.3' testCompile "org.hamcrest:hamcrest-core:$hamcrestVersion" testCompile "org.hamcrest:hamcrest-library:$hamcrestVersion" testCompile "org.hamcrest:hamcrest-integration:$hamcrestVersion" androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'org.mockito:mockito-core:1.10.19' testCompile 'junit:junit:4.12' compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') { transitive = true; } compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'org.jetbrains:annotations:15.0' compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" } apply plugin: 'com.google.gms.google-services' android.packagingOptions { exclude 'LICENSE.txt' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/rxjava.properties' } configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> def requested = details.requested if (requested.group == 'com.android.support') { if (!requested.name.startsWith("multidex")) { details.useVersion "$supportVersion" } } } } 

没有成功,我试了这个

 tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { sourceCompatibility = JavaVersion.VERSION_1_6 targetCompatibility = JavaVersion.VERSION_1_6 kotlinOptions { jvmTarget = '1.6' apiVersion = '1.1' languageVersion = '1.1' } } 

和这个

 tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 kotlinOptions { jvmTarget = '1.8' apiVersion = '1.1' languageVersion = '1.1' } } 

更新与使用1.8 kotlin的最后一个版本我现在得到一个不同的错误:

错误:执行任务':app:compileDebugKotlin'失败。 编译错误。 查看日志了解更多详情

我正在调查

这是我的顶级build.gradle

 buildscript { ext.kotlin_version = '1.1.2-4' repositories { jcenter() } dependencies { classpath('com.android.tools.build:gradle:2.3.0') { force = true } // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle file classpath 'com.google.gms:google-services:3.0.0' classpath "io.realm:realm-gradle-plugin:2.1.1" classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { jcenter() maven { url 'https://maven.fabric.io/public' } maven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir } 

Retrolambda不处理Kotlin字节码,您应该将Kotlin编译器设置为专门针对1.6字节码。 这不会破坏当前版本的Kotlin中的任何内容,因为它可以为相同的功能生成较旧的字节码。

 compileKotlin { kotlinOptions.jvmTarget = "1.6" } 

这在Kotlin Gradle插件属性中有记录

对于测试,还可以添加:

 compileTestKotlin { kotlinOptions.jvmTarget = "1.6" }