Are there any examples/demos of Swift Code Bundlin...
# touchlab-tools
m
Are there any examples/demos of Swift Code Bundling using SKIE?
t
We've got documentation here: https://skie.touchlab.co/features/swift-code-bundling Are you experiencing issues?
👍 1
m
How about adding remote SPM packages?
t
That's currently not supported. Would you need to use that from the bundled Swift or Kotlin?
m
either way whether if it were a SPM package which interop to integrate, or Swift bundle to interface
t
Could you share more details about your usecase?
m
very happy to! currently when using remote iOS dependencies we need to use Cocoapods
there’s a few mentions on this Slack regarding using SPM as a replacement to Cocoapods
usecase such as adding Firebase or any other Analytics SDK, or any of the multitude of open source repo’s which can be added as remote Swift packages
in my particular use case, I wish to integrate a MediaPlayer remote repo which is shared between iOS projects, or even add a local Swift package which may have it’s own remote dependencies
@Tadeas Kriz back to original post. I'm afraid the link you shared regarding Swift bundling is not comprehensive enough. I've added the plugin:
id("co.touchlab.skie") version "0.8.2"
I've added a swift file to my directory
iosArm64Main/swift
My project
BUILD SUCCESSFUL
, but how do import the bundled Swift in my Kotlin code?
f
Hi! Just import the Kotlin framework and the Swift code should be accessible in the given file (as long as it has a public visibility which is my favorite mistake to make 😄)
oh, sorry I misread the question. You want to access the Swift code inside the Kotlin code
In that case you will need to make the Swift code callable from Obj-C (using the @objc annotation) and then you can access it like it would be in any other Obj-C framework using Kotlin external declarations/cinterop.
m
yes all my code has appropriate @objc annotations
when running
./gradlew skieProcessSwiftSourcesIosArm64
it does appear to copy to build directoy:
but there is no generated
.def
f
yes that is expected
the .def file are currently not generated automatically and must be written manually
m
okay, can do that. but then what is the
libraryPaths
that it should be pointing to?
is skie not building the source set?
t
You should just write the expected ObjC header code inside the
.def
file below
---
m
I must be confused about what exactly Swift Code Bundling does?
t
It's primary purpose is to bundle Swift code inside the framework so it can then be used by the code depending on the KMP framework
m
my impression was it would build the source in
src/iosArm64Main/swift
which can then be used?
the ObjC header will be generated when building the source, so where is the output for this header?
t
As a side-effect, you can use it from Kotlin too, but since we build Swift after Kotlin, we currently don't give Kotlin the necessary information to use the Swift directly.
f
The issue you’ve run into is that this compilation process generates a header that is understood by the Swift compiler but not the Kotlin compiler. The Kotlin compiler needs the .def file
t
You can take a look at
Headers/{modulename}-Swift.h
Then you can copy from that header into the
.def
file below
---
For example, for the Swift code:
Copy code
@objc(Foo)
@objcMembers
class Foo: NSObject {
  func something() { ... }
}
you'd write:
Copy code
@interface Foo

-(void)something;

@end
And then you'll be able to use
Foo
from Kotlin
m
Yes that's exactly what I want and have got so far
but I'm confused about when Skie compiles this code and generates the
Headers/{modulename}-Swift.h
which I can add to my
.def
file
t
You can't point to the
.h
file, you have to copy its contents. It's generated when you run a
link
gradle task. You have to copy part of the
-Swift.h
file's contents
👀 1
Basically want to copy stuff between
#if defined(__OBJC__)
and
#endif
f
you need to run the link task
👀 1
m
ah okay, I've run
./gradlew linkDebugFrameworkIosArm64
and now I have the output:
skie/binaries/debugFramework/DEBUG/iosArm64/swift-compiler/headers/radioplayer-Swift.h
right thanks guys, got it working!
here's my
.def
file:
Copy code
package = com.mt.player
language = Objective-C
headers = /radioplayer/radioplayer/build/bin/iosArm64/debugFramework/radioplayer.framework/Headers/radioplayer-Swift.h
t
As I said, don't do that
The header is updated after your Kotlin code is already compiled, so it won't work
m
alright, well I'm gonna hit pause on this
but the o.p remains. there is insufficient documentation on how to use this
t
It's not supposed to be used like this just yet
m
especially no mention on needing to
linkDebugFrameworkIosArm64
alright, well I'll circle back
thanks for all your help anyway!
I was hoping to update that repo to use Skie
t
As you experienced, because you need to keep the
.def
file updated manually, it's usable, but it's not meant to be used the same way you could use Java+Kotlin in Kotlin/JVM environment.
❤️ 1
m
understood
sorry for being a badger!
t
No worries, it's nice to see people experimenting with this.
m
yeah really keen 😅
t
We use this when we need to make a single call to Swift that'd configure logic, as the Swift code can use anything exposed from Kotlin easily. And that's something where we don't have many changes happening in the Swift interface so there's no maintenance cost
m
ah understood