https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

Jeff Lockhart

02/04/2021, 1:13 AM
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

russhwolf

02/04/2021, 1:20 AM
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

Jeff Lockhart

02/04/2021, 1:34 AM
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

Big Chungus

02/04/2021, 7:35 AM
Maybe try creating a jvm module for these things and then jyst consume it as a dependency in your main android module?
a

Animesh Sahu

02/04/2021, 9:11 AM
Did you mean
actual typealias SomeExpectClass = package.of.JavaClass
blob thinking upside down?
j

Jeff Lockhart

02/05/2021, 4:07 AM
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.