Debugging Kotlin on iOS with Xcode
Tags: kotlin, kotlinmultiplatform, android, ios
Published: 2020-03-19 18:59:36

---
title: Debugging Kotlin on iOS with Xcode
published: true
description: Using the Xcode Kotlin plugin, you can debug Kotlin code shared with your iOS project. However, configuration is important.
tags: kotlin, kotlinmultiplatform, android, ios
---

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](https://www.infoworld.com/article/3509457/jetbrains-bringing-ios-device-support-to-android-studio.html).

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](https://github.com/touchlab/xcode-kotlin) 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](https://github.com/apollographql/xcode-graphql) [these](https://github.com/mtak-/rust-xcode-plugin) 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](https://github.com/touchlab/xcode-kotlin/blob/master/README.md).

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.

{% github touchlab/KotlinCocoapods no-readme %}

For a live example, there's a [PR to KaMP Kit](https://github.com/touchlab/KaMPKit/pull/76) 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](https://github.com/touchlab/KotlinCocoapods/issues/1) and will look into it.

## Kotlin Files in Xcode

Previously we recommended to use [our KotlinXcodeSync plugin](https://github.com/touchlab/KotlinXcodeSync) to sync xcode with Kotlin files. As it turns out, there's [a much easier way](https://github.com/touchlab/xcode-kotlin/issues/16) 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.

{% youtube p_xdEOiqvSA %}

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](https://touchlab.co/)!