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

Bailey Pollard

06/20/2021, 7:59 PM
Hey folks, wondering if there is any way to eliminate the need for duplicated code in both iosX64Main/iosArm64Main directories? Tried to find some documentation on this with no luck
b

Bailey Pollard

06/20/2021, 8:07 PM
I read that, seems a bit questionable needing to duplicate the same code, just having it build for different archs
m

mbonnin

06/20/2021, 8:07 PM
What we're doing is that we're creating a
apple
sourceSet and sourcing it from the different targets: https://github.com/apollographql/apollo-android/blob/dev-3.x/build-logic/src/main/kotlin/Mpp.kt#L24
a

Arkadii Ivanov

06/20/2021, 8:13 PM
The
ios
shortcut defines the
iosMain
source set, so you can place your iOS code there. It should compile even without HMPP, which just adds IDE support (imports are resolved and not red).
b

Bailey Pollard

06/20/2021, 8:15 PM
I should've mentioned this in the original post, I added an external ios framework to my project without the use of cocoapods. I can only import this framework in the iosX64Main/iosArm64Main directories. The import does not compile correctly in the iosMain directory. Is this expected behaviour for external frameworks?
b

Big Chungus

06/20/2021, 8:45 PM
What I usually do is add custom sourceRoot to both sourceSets
That way it's part (not just a dependency) of both sourceSets
b

Bailey Pollard

06/20/2021, 8:46 PM
Any open source examples of this?
b

Big Chungus

06/20/2021, 8:48 PM
kotlin.sourceSets["iosX64Main"].kotlin.srcDir("src/sharedIosMain/kotlin") Do this in gradle for both and place your code there. Or even better yet, just add one's kotlin root to the other's
b

Bailey Pollard

06/20/2021, 8:48 PM
Let me try that
b

Big Chungus

06/20/2021, 8:48 PM
Beware that this is a hack, not standard practice
b

Bailey Pollard

06/20/2021, 9:27 PM
How does one use code in
iosMain
that was declared in
iosX64Main
?
r

rnett

06/20/2021, 10:03 PM
You can't, (well, if you put an
expect
in
iosMain
and the
actual
in each sub-source set you kind of can). The setup with HMPP is you have some "more common" source set like
iosMain
that each of the
iosX64Main
,
iosArm64Main
, etc depend on. It's essentially the same as a common source set, just only shared between a few modules
2 Views