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

Zoltan Demant

02/23/2023, 3:34 PM
I was surprised to see
android.content.Context
being suggested/autocompleted in commonMain code earlier today. Just to verify, I created a new KMP project, and that was no longer the case. After some digging, Ive found that a couple of ios related lines in the gradle file is what changes this behavior. Is this expected given that I only have commonMain and androidMain sourcesets?
Copy code
listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64(),
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
        }
    }
l

Landry Norris

02/23/2023, 3:36 PM
As of right now, commonMain allows anything that is available in all defined targets. If you only have an android() target set up, then you can use android.content.Context, but once you add ios targets, this should not happen.
z

Zoltan Demant

02/23/2023, 3:38 PM
Gotcha! I thought it might be something along those lines. I guess keeping an extra empty sourceSet available just to stop this from happening for now would be suitable?
l

Landry Norris

02/23/2023, 3:39 PM
I usually add a linuxX64 or iosArm64 to prevent this.
You could also start off by defining the source sets you know you’ll want eventually. If you just build android, anything missing in other targets doesn’t matter.
z

Zoltan Demant

02/23/2023, 3:49 PM
Arent dependencies resolved for all targets, even if theyre just placeholders? For example, declaring a linux sourceset results in the following: • Could not resolve androidx.annotationannotation1.5.0 for featurepreset:linuxX64Main This might just be a result of the way Ive set things up as well, Im not sure yet!
l

Landry Norris

02/23/2023, 3:50 PM
androidx.annotation stable is not available for multiplatform (the dev01 version is, though). If you add a non-android target, you can’t use it in commonMain.
Each source set can only use the intersection of what’s supported on each target that source set is tied to.
z

Zoltan Demant

02/23/2023, 4:00 PM
Youre right! Looks like I have some rethinking to do now that my understanding of how this works has changed 😅 Thanks for the quick responses Landry, I appreciate that!
6 Views