问题
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