16

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

5
  • 3
    First off, the function you defined does not have the same name as the one you're calling (First vs FirstOrDefault). Commented Feb 2, 2016 at 21:50
  • How are you compiling your Java project? Commented Feb 2, 2016 at 22:31
  • Using gradle and IntelliJ 15 and I fixed the typo Commented Feb 3, 2016 at 10:44
  • 2
    Does your Java module include the Kotlin runtime in its dependencies? It needs to. Commented Feb 3, 2016 at 12:07
  • Yes it does all three jars Commented Feb 3, 2016 at 14:17

4 Answers 4

19

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't setting it . I try to create one kotlin.kt file and then do follow IDE ,fix my problem. [AndroidStudio3.4/kotlin1.3.41/2019-07-08 10:26:19]
This useful when your project is based on java but you need to use a kotlin based and use its functions inside your java code.
6

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

Comments

0

Android Studio:

in my case, I have change the signature of the inner function which is making the cause of this exception. try to clean your project and run the app, exception will disappear

Comments

-7

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.

2 Comments

Sorry, this is not helpful. The Function1 class is part of the Kotlin standard library, so neither of your suggestions is applicable.
This is not the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.