can i use java code in androidMain in a composeApp...
# multiplatform
p
can i use java code in androidMain in a composeApp ?
a
havent tried but you should be. AFAIK the android source set is just a normal android app which supports java. i know that you can use java packages in the common source set, only if you are only sharing across jvm targets (android is jvm). so maybe you could even share java code there?
p
tried, creating a sample app, which writes a string to a file failed with this error Caused by: java.lang.ClassNotFoundException: Didn't find class "org.example.cmp.FileWriterHelper" on path: DexPathList[[zip file "/data/app/~~gkXDabstW65WYSEJVrAiYQ==/org.example.cmp-tmwTjRc8miYZCLmOUlKjew==/base.apk"],nativeLibraryDirectories=[/data/app/~~gkXDabstW65WYSEJVrAiYQ==/org.example.cmp-tmwTjRc8miYZCLmOUlKjew==/lib/arm64, /system/lib64, /system/system_ext/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
@Alex Styl ?
a
@Priyansh Nama i don't know. as I said I haven't tried
c
c
Yes you can, AndroidTarget extends java target by default. (Well android SDK which is a subset of Java within reason)
you can't use java in commonMain, but you could say use java in a shared jvmMain which would be shared between androidMain and desktopMain. Just check you source tagets are setup correctly, which is pretty easy on new gradle dsl
p
ok thanks
if i use jvmMain, can java classes in jvmMain and kotlin classes in androidMain interact with each other, effortlessly ?
because i have an android app with both java and kotlin file, i need to add all these to compose app, using java classes without migrating to kotlin would save me some initial time
c
If I understand then yes. I would digest this doc: https://kotlinlang.org/docs/multiplatform-discover-project.html#platform-specific-source-sets It's basically a one way tree,
androidMain
can see
jvmMain
, but
jvmMain
can't see
androidMain
, same for
desktopMain
, and same that
desktopMain
can't see
androidMain
. You can make pretty complex source set tree's if you really need too, but for sanitiy sake I would always just do common -> platform targets where possible to save you the world of implementation detail hell
p
thanks @chrisjenx, but i don't see the jvmMain dir, can i manually create it ?
i am unable to achieve the following things 1. unable to add jvmMain as a dependency on androidMain 2. use android specific code (for example context) in jvmMain
m
you should not be able to achieve 2
c
Yes, 2 is not possible, android != jvm
m
jvmMain is for all JVM platforms, not just android
you should extract the common, JVM based parts of your code to one class, house it in jvmMain, then inject the platform specific parts into it either using expect/actual or interfaces/abstract classes
c
I haven't tested I just wrote this here, but you would create a common jvm group for dekstop and android like this:
Copy code
kotlin {
    jvm("desktop")
    android()

    applyDefaultHierarchyTemplate {
        // create a new group that
        // depends on `common`
        common {
            // Define group name without
            // `Main` as suffix
            group("dekstopAndAndroid") {
                // Provide which targets would
                // be part of this group
                withJvm("desktop")
                withAndroid()
            }
        }
    }

    // Now you will have `dekstopAndAndroidMain` source set
}