Is it possible to use Java code as part of an actu...
# multiplatform
j
Is it possible to use Java code as part of an actual implementation in android/jvm source set? I have the Java class implemented and my code compiles, but at runtime the app crashes with
java.lang.NoClassDefFoundError: Failed resolution of: Lpackage/of/my/JavaClass;
I need to workaround a Kotlin/Java interop bug in a library where an enum is not accessible in Kotlin because it is part of a non-accessible parent class.
r
You can use
jvm { withJava() }
in your target configuration. See https://kotlinlang.org/docs/reference/mpp-configure-compilations.html#include-java-sources-in-jvm-compilations. Last I was aware though you couldn’t do this if you also had an
android
target.
j
This is exactly what I need, except I do have an android target, and not a jvm target. (This is an iOS/Android shared module.) Unfortunately
Copy code
android {
    withJava()
}
doesn’t work.
Might have to resort to some ugly reflection as a workaround until the library gets a fix out.
b
Maybe try creating a jvm module for these things and then jyst consume it as a dependency in your main android module?
a
Did you mean
actual typealias SomeExpectClass = package.of.JavaClass
blob thinking upside down?
j
No, I’m not typealiasing the actual directly to the Java implementation. I just need to use a Java shim to access one specific dependency as part of the android actual implementation. I ended up writing some reflection code to access the inaccessible type in the Java dependency from Kotlin.