误差java8和科特林

误差java8和科特林

问题描述:

我有哪里我使用retrolambda应用程序以便在的build.gradle我误差java8和科特林

compileOptions { 
    sourceCompatibility JavaVersion.VERSION_1_8 
    targetCompatibility JavaVersion.VERSION_1_8 
} 

一切都很好,除非我添加了科特林支持。 添加科特林插件我得到以下错误:

Error:Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code. This is caused by library dependencies that have been compiled using Java 8 or above. If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle file.

我发现与此类似,但解决方案的非很多问题和答案适用于我的情况。

这是我的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:[email protected]' 
    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:[email protected]') { 
     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科特林我现在得到一个不同的错误的最后一个版本:

Error:Execution failed for task ':app:compileDebugKotlin'. Compilation error. See log for more details

我现在调查

这是我最顶层的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 
} 
+0

你应该提及你尝试过哪些解决方案没有奏效。 “但所有解决方案都不适用”意味着人们可能会再次告诉你同样的事情,而不知道它们是什么。 –

+0

@JaysonMinard很容易看出他们不适用,因为我已经按照他们的建议去做,正如你可以在代码中看到的一样 – kingston

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

compileKotlin { 
    kotlinOptions.jvmTarget = "1.6" 
} 

这是记录在Kotlin Gradle Plugin attributes

做检查,还加:

compileTestKotlin { 
    kotlinOptions.jvmTarget = "1.6" 
}