Samuel Gammon
08/08/2024, 2:12 AMalexandre mommers
08/08/2024, 8:37 AMSkaldebane
08/09/2024, 7:20 AMjvmMain
doesn't target Android, because it include some desktop-only javax
APIs.
I usually deal with this by creating an jvmAndAndroidMain
(or whatever name you desire) source set, and making the (desktop) JVM and Android source sets depend on it. KMP then automatically allows me to use only the shared APIs.
Regarding builds working but the IDE still showing errors, it's because there's currently a disconnect between what the compiler sees at compile-time, and what is considered to be "correct". The IDE is right about the errors, but the compiler doesn't care about that, and if it finds all the APIs that are used, then the build will succeed. In fact, you could use platform-specific code in commonMain
, and the build will succeed with no warning when you build for that particular platform, while the IDE will rightfully point out the error.
JetBrains is working on making the compiler aware of platform boundaries, so errors (or at least warnings) will be raised on improper usage.Skaldebane
08/09/2024, 7:25 AMkotlin {
...
// using dependsOn() disables the default hierarchy, so we need to re-apply it manually.
applyDefaultHierarchyTemplate()
sourceSets {
commonMain.dependencies { ... }
val jvmAndAndroidMain by creating {
dependsOn(commonMain.get())
}
androidMain {
dependsOn(jvmAndAndroidMain)
dependencies { ... }
}
val jvmMain by getting {
dependsOn(jvmAndAndroidMain)
dependencies { ... }
}
}
}
Note that because we're using dependsOn
, the default KMP hierarchy gets disabled, so we have to explicitly apply it using applyDefaultHierarchyTemplate
Skaldebane
08/09/2024, 7:26 AMjvmMain
to desktopMain
, keeping the jvmMain
name for the one shared between desktopMain
and androidMain
.Samuel Gammon
08/09/2024, 9:27 AMSamuel Gammon
08/09/2024, 9:28 AMSamuel Gammon
08/09/2024, 9:28 AMSamuel Gammon
08/09/2024, 9:28 AMSkaldebane
08/09/2024, 12:21 PMalexandre mommers
08/11/2024, 5:50 PMSkaldebane
08/11/2024, 9:58 PMalexandre mommers
08/11/2024, 10:09 PMSkaldebane
08/11/2024, 10:11 PMSkaldebane
08/11/2024, 10:14 PMSkaldebane
08/11/2024, 10:15 PMiosMain
and wasmJsMain
intentionally disabled)Skaldebane
08/11/2024, 10:16 PMSkaldebane
08/11/2024, 10:16 PM