Kotlin cannot access kotlin.jvm.functions.Function1 when calling kotlin function with java lambda

筅森魡賤 提交于 2021-02-18 20:52:08

问题


I am trying to call the following Kotlin function from Java

override fun First(list: LinqList<ElementType>, condition: (ElementType) -> Boolean) : ElementType

like this

int first = list.First(list,(x) -> x == 5);

but i get the following error

Error java: cannot access kotlin.jvm.functions.Function1
  class file for kotlin.jvm.functions.Function1 not found

I have tried googling it but i can not find the answer anywhere

Thanks in advance


回答1:


My problem got fixed when I configured Kotlin compiler and runtime for my Java module with the latest stable version (currently 1.3.30)

Just go to Tools > Kotlin > Configure Kotlin in Project > Android with Gradle and choose your Java module with Single module radio button selected then select your version and OK.




回答2:


Another Solution:

If you have several modules in your android project, please make sure you have added the below config to every module that uses the kotlin:

Step(1)- Project build.gradle:

// Project build.gradle file.
buildscript {
    ext.kotlin_version = '1.3.30'
    ...
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Step(2)- Inside each module using kotlin:

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

...

dependencies {
   implementation "androidx.core:core-ktx:1.0.1"
   implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Reference: Add Kotlin to an existing app




回答3:


Method 1) Search for Function1 in your project file and rename it to First.

Method 2) Search for Function1 in your project file and remove all its occurrences.



来源:https://stackoverflow.com/questions/35161913/kotlin-cannot-access-kotlin-jvm-functions-function1-when-calling-kotlin-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!