Java 8 java8和Kotlin出现错误

Java 8 java8和Kotlin出现错误,java-8,kotlin,Java 8,Kotlin,我有一个应用程序,我正在使用retrolambda,所以在build.gradle中我有 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } 除非我添加对Kotlin的支持,否则一切都很好。 添加Kotlin插件时出现以下错误: 错误:将字节码转换为索引时出错: 原因:Dex无法解析版本52字节代码。 这是由使用Jav

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

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
除非我添加对Kotlin的支持,否则一切都很好。 添加Kotlin插件时出现以下错误:

错误:将字节码转换为索引时出错: 原因:Dex无法解析版本52字节代码。 这是由使用Java8或更高版本编译的库依赖项引起的。 如果您在库子模块中使用“java”gradle插件,请添加 targetCompatibility='1.7' sourceCompatibility='1.7' 到该子模块的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'
    }
}
而这

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'
    }
}
更新 对于kotlin使用1.8的上一个版本,我现在得到一个不同的错误:

错误:任务“:app:compiledBugkotlin”的执行失败。 编译错误。有关更多详细信息,请参阅日志

我正在调查

这是我的顶级身材

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"
}
这记录在

对于测试,还应添加:

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.6"
}

您应该提及您尝试的哪些解决方案无效。“但没有一个解决方案适用”意味着人们可能会在不知道他们是什么的情况下再次告诉你同样的事情。@JaysonMinard很容易看出他们不适用,因为我已经在做他们在代码中建议的事情了