DEV Community

Kevin Galligan for Touchlab

Posted on

Debugging Kotlin on iOS with Xcode

Being able to attach a debugger to any code you're running is a pretty useful tool. We expect these tools to mature for Kotlin Multiplatform, especially later this year when the Android Studio plugin launches.

For sharing Kotlin code with iOS projects, it would seem pretty important to be able to debug directly in Xcode. We built the Kotlin Xcode plugin to make that possible.

Plugins are no longer officially supported in Xcode, of course. However, they haven't been entirely disabled for source code formatting and custom languages. There are several of these out in the wild which are doing roughly the same thing. Color source code, and tell Xcode you can debug these files.

From there, Xcode just hooks into lldb, so as long as your source type supports lldb, you should be able to debug!

Installing

Installing the Xcode plugin is a little tricky. As plugins aren't officially supported, Xcode doesn't make the process easy, and it changed somewhat between v10 and v11. Follow the instructions in the readme.

Some things to note. If you have the plugin installed already, but it seems to have stopped working, you may need to completely remove the plugin and reinstall. That means close Xcode, open ~/Library/Developer/Xcode/Plug-ins/ and remove Kotlin.ideplugin, open Xcode again, close, and run setup.sh.

Why that happens is unknown, but that seems to solve the issue. We may eventually make an installer process to automate that, but we need more testing.

Configure Kotlin

Currently you'll need a dynamic framework to debug. We are working on getting debugging to work with static frameworks, but at present, that doesn't work right.

If you aren't using the Kotlin Cocoapods plugin, you should have a dynamic framework by default. If you are using the Cocoapods plugin, that generates static frameworks, and there's no way to disable that directly. We've forked the Cocoapods plugin so you can generate dynamic frameworks for Kotlin with Cocoapods.

For a live example, there's a PR to KaMP Kit pending to enable debugging.

Some notes. You need to do a clean build to get changes to show up in Xcode. Not a full clean of Kotlin's build, but in Xcode, run "Clean Build Folder". We have an issue for this and will look into it.

Kotlin Files in Xcode

Previously we recommended to use our KotlinXcodeSync plugin to sync xcode with Kotlin files. As it turns out, there's a much easier way to see your Kotlin in Xcode. You can just add a folder reference, which will make the files available but won't try to compile them directly or put them in the app bundle.

We'll be deprecating the Xcode Sync gradle plugin, and writing up some better docs for all of this soon.

What's Next?

We're waiting on Kotlin 1.3.71 to finish pushing some libraries, and will be revisiting the dev tools and process at that point to streamline things a bit.

Reach Out

If your org is looking for expert support to get started on your KMP journey, of course, reach out!

Top comments (1)

Collapse
 
alex009 profile image
Aleksey Mikhailov

hi!
you can check our multiplatform gradle plugin with cocoapods support - github.com/icerockdev/mobile-multi...
with it you can build shared module by gradle, without xcode. and also you can configure project to build faster in multi-module project with cocoapods in different modules (where not required cinterop with cocoapod you can just pass cocoapod to linker, it save some of compilation time)