0

I am dealing with the same error described in the following questions, but all the solutions proposed didn't solve my problem.

Why kotlin gradle plugin cannot build with 1.8 target?

Android Studio throws build error in Kotlin project which calls static method in java interface

Intellij refuses to set the Kotlin target jvm to 1.8?

In my case there is a small difference. I am using Android Studio 3.5 and I recently upgraded the Kotlin version to 1.3.61 Initially I got the usual error message:

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

I changed File -> Settings -> Kotlin Compiler -> Target JVM Version and after this change it compiled without errors, but later when I tried to to deploy on the emulator the build failed again. So, when it builds the app it works, when it does the full build to deploy it fails, but in theory they are using the same configuration. When I click on settings within the gradle window, whatever the node is selected it opens the same window of the main settings. What can be the difference? I checked all possible settings and configurations, I checked also the project structure and the build/run/debug templates, but I couldn't find other places where I could set the property. I tried to set in the gradle build the JvmTarget, but with this Kotlin version the property does no longer exist. I thought about a download issue and I added mavenCentral to the repositories, but nothing changed. If something changed recently this page does not seem to be up to date: https://kotlinlang.org/docs/reference/using-gradle.html What else could I try? Why only the full build fails?

UPDATE:

For the moment I worked around the issue by adding non static methods, but it will cost in term of performance. So I hope a solution will pop out sooner or later. In the meanwhile I am adding the build file, although I don't think it will add a lot to the description of the issue:

//Originally the plugins were declared with the syntax "apply plugin ...."
//Changing to this form did not change the result
plugins {
    id('com.android.application')
    id('org.jetbrains.kotlin.android')
    id('org.jetbrains.kotlin.android.extensions')
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId ..........
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }

// Commented out because the property JvmTarget is not found with Kotlin 1.3.61
//    compileReleaseKotlin {
//        JvmTarget='1.8'
//    }

//    compileDebugKotlin {
//        JvmTarget='1.8'
//    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //I tried also JDK 7
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    //Follow other dependencies which are irrelevant
    // ...
}
FluidCode
  • 271
  • 2
  • 10
  • Can you share your build file? If I remember correctly, you can also specify versions directly with strings ("1.8") – Allan W Dec 28 '19 at 02:50

2 Answers2

1

Update

Recent versions of Kotlin plugin allow you to put kotlinOptions inside the android block. Tested this with Kotlin 1.4.20 and Gradle 6.7.

android {
    // ...

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Original answer

Place this outside of the android block in your build.gradle file:

Gradle
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
Gradle KTS
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Note the small j in jvmTarget and absence of variant names - this will apply to all build variants.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

Make sure you are setting jvmTarget correctly:

For source, it would be:

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

For test, it would be:

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
Vedsar Kushwaha
  • 313
  • 2
  • 11