Is it possible to access extension functions from Java code?
I defined the extension function in a Kotlin file.
package com.test.extensions
import c
You need to duplicate your functions in the class files:
Create Kotlin file , for ex Utils.kt
Enter the code
class Utils {
companion object {
@JvmStatic
fun String.getLength(): Int {//duplicate of func for java
return this.length
}
}
}
fun String.getLength(): Int {//kotlin extension function
return this.length
}
OR
class Utils {
companion object {
@JvmStatic
fun getLength(s: String): Int {//init func for java
return s.length
}
}
}
fun String.getLength(): Int {//kotlin extension function
return Utils.Companion.getLength(this)//calling java extension function in Companion
}
In kotlin use:
val str = ""
val lenth = str.getLength()
In Java use this:
String str = "";
Integer lenth = Utils.getLength(str);