Is there a way to use local shared (KMP) in xcode ...
# multiplatform
p
Is there a way to use local shared (KMP) in xcode that is configured with spm modules? Here is the pic of iOS project structure. I do not have problem with setting the KMP (using embedAndSignAppleFrameworkForXcode) for main module, but spm modules are prepared before the app and I need access shared KMP there as well.
p
I have been using binaryTarget() in Package.swift, sort of:
.binaryTarget("directory-where-shared-xcframework-is-located")
and I haven't found any major problems. The first time Xcode takes some time to import the
shared
.xcframework into the local package but once it does it starts working ok. What problems are you facing when using the above gradle task in the build phase script?
p
this gradle script is starting after these modules are checked, so it wont work 😞
p
Oh ok
p
if I understand your comment, you need to run the gradle outside of xcode and then indlude the build in package of spm module
p
Gradle is run from the build phase script.
p
the way our project is structured is that all these spm modules (Core, Common, Vendor) are compiled before the app project. The
build phase script
is run too late
p
Just replace the
embedAndSign…
with
assembleSharedDebug...
gradle tasks. See bellow:
I forgot to say, only
.xcframework
works, is a condition impose by SPM
the way our project is structured is that all these spm modules (Core, Common, Vendor) are compiled before the app project. The
build phase script
is run too late
Ahh I see, perhaps you can compile the kotlin code externally and just using the resulted xcframework binary. But the experince won’t be the same. Is the equivalent of development a library in java using
mavenLocal
to test in a demo App. It would add some delay but perhaps not terrible.
p
yea. I was thinking about that as well as a last resort
👍 1
I am also thinking about running bash script from
package.swift
p
Humm, interesting 😲. I haven't explored that but judging by the name sounds good.
p
dont know yet how to make sure it will be made synchronously 😛
🙂 1
t
@Piotr Prus I am curious to see what solution you have found. Is there anything new? I am also looking for a suitable solution and would be grateful for inspiration ☺️
p
Hey Thomas. Sorry for late reply, but I was OOO for a while. I have good news. I made it work and I am very satisfied with the result. I will make a blog post with all the details, but in short: 1. Since SPM are preconfigured and outside of project configuration, we need to build the framework locally. I am using specific gradle task with specific platform, so it do not generate all the targets, but just emulator. For example:
./gradlew spmDevBuild -PspmBuildTargets=ios_simulator_arm64
2. I have both shared and iOS projects in the same root:
Copy code
- MyProjects
 -- iosProject
 --- Packages/Core
 -- shared-kmp
3. In the
Package.swift
you have the dependencies array, something like:
Copy code
var coreDependencies: [Package.Dependency] = [
    .package(url: "<https://github.com/Alamofire/Alamofire>", exact: "5.8.1"),
    .package(url: "<https://github.com/kasketis/netfox>", exact: "1.21.0"),
    .package(url: "<https://github.com/evgenyneu/keychain-swift>", exact: "20.0.0"),
    .package(url: "<https://github.com/marmelroy/PhoneNumberKit>", exact: "3.7.6")
]
and you can add new item to that array using append:
Copy code
if remoteBuild {
    coreDependencies.append(.package(url: "<https://github.com/myapp/shared-kmp>", exact: "0.1.4"))
} else {
    // put shared-kmp repo in the same root as your iOS project
    coreDependencies.append(.package(name: "MyShared", path: "../../../shared-kmp"))
}
4. so, at the end I am changing just one boolean
remoteBuild
to work with remote build or local. The variable is default to TRUE, so I have no issues when running CI machine. Hope it makes sense 🙂
👍 1
❤️ 1
220 Views