Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with java8 and Kotlin

Tags:

java-8

kotlin

I have an app where I'm using retrolambda so in the build.gradle I have

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

Everything is fine unless I add the support for Kotlin. Adding the Kotlin plugin I get the following error:

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.

I have found many questions and answers similar to this one but non of the solutions apply in my case.

This is my 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"
            }
        }
    }
}

With no success I tried this

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'
    }
}

and this

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'
    }
}

Update with the last version that uses 1.8 for kotlin I now get a different error:

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

I'm investigating now

This is my top level 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
}
like image 204
kingston Avatar asked May 28 '17 15:05

kingston


People also ask

Is Kotlin Java compatible?

Yes. Kotlin is 100% interoperable with the Java programming language and major emphasis has been placed on making sure that your existing codebase can interact properly with Kotlin.

Is Kotlin Transpiled Java?

Kotlin aims to help improve the experience of developing with Java within IntelliJ IDEA. One of the ways Kotlin does this is by being compatible with the Java source code. The Kotlin code transpiles to Java, enabling developers to use Kotlin as needed within their larger Java projects.

Does Kotlin compile to Java bytecode?

For the most part, Kotlin compiles into the same bytecode as Java. Classes, functions, function arguments, and standard flow control operators, such as if and for, work in the same way. However, there are differences between Kotlin and Java. For example, Kotlin has inline functions.

Why should we use Kotlin?

67% of professional developers who use Kotlin say Kotlin has increased their productivity. Safer code: Kotlin has many language features to help you avoid common programming mistakes such as null pointer exceptions. Android apps that contain Kotlin code are 20% less likely to crash.


1 Answers

Retrolambda is not processing the Kotlin bytecode, and you should set the Kotlin compiler to specifically target 1.6 bytecode. This will not break anything in current version of Kotlin as it can generate the older bytecode for the same functionality.

compileKotlin {
    kotlinOptions.jvmTarget = "1.6"
}

This is documented in the Kotlin Gradle Plugin attributes

For tests, also add:

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.6"
}
like image 183
Jayson Minard Avatar answered Sep 30 '22 19:09

Jayson Minard