Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

intellij idea - Why kotlin gradle plugin cannot build with 1.8 target?

I have the simplest gradle project configured using intellij for kotlin 1.2.10. Here is my build.gradle file:

buildscript {
    ext.kotlin_version = '1.2.10'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

group 'com.ali'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

And I have a simple java interface:

public interface MyMath {
    static int myAbs(int input) {
        return Math.abs(input);
    }
}

When I import this interface and try to call myAbs method it fails with this error:

Error:(6, 12) Kotlin: Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'

I have created an intellij kotlin app and it was working correctly. Is it a bug in new Kotlin gradle plugin?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this could be helpful for those using Android Studio 3.2 on Mac.

To change the Kotlin Compiler Target JVM version you should go to Android Studio -> Preferences -> Kotlin Compiler and then change the Target JVM version choosing from the dropdown.

Anyway, I'm still getting the following error

Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8'

SOLVED

Adding the following to my build.gradle solved the problem:

android {
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

About this and other Gradle configuration options: https://kotlinlang.org/docs/reference/using-gradle.html


With Kotlin Gradle DSL:

import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions

(kotlinOptions as KotlinJvmOptions).apply {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...