Hide chapters

Kotlin Multiplatform by Tutorials

Second Edition · Android 14, iOS 17, Desktop · Kotlin 1.9.10 · Android Studio Hedgehog

B. Appendix B: Debugging Your Shared Code From Xcode
Written by Carlos Mota

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

Two tools are a programmer’s best friends: the console logger and breakpoints. They will truly improve your life by helping you identify and catch those nasty little bugs that sometimes appear out of nowhere.

You’ve used the logger throughout this book. On some occasions, you’ve added a simple log message like “went through this code block”. On other occasions, you’ve printed all the variables in a method. Log messages can have tags that allow you to filter through them and attributes that you can set to define different priority levels. This can help you easily understand where something went wrong.

Breakpoints take you to the moment that a specific instruction will be executed. You can see all the steps that lead to this stop and all that will succeed. Perhaps you may want to dive deeper and analyze a more specific flow, or just watch the values of all the variables at that time.

Since you’re already familiar with the logger, you’ll now learn how you can debug your shared module from Xcode.

Debugging the Shared Module

Both Android Studio and Xcode have great debugging capabilities. The native code is simple to debug on both IDEs. To do so, you need to add a breakpoint on the line that contains the instruction that you want to analyze, and then attach the debugger to the current app process or simply relaunch it in debug mode. For both options, the app halts before executing that instruction.

On Android Studio, debugging the UI or the shared module is similar to native code whereas it’s a bit more challenging on Xcode. Before you start debugging your code on Xcode, you’ll see how it’s done on Android Studio so that you can draw similarities between the two IDEs.

Debugging in Android Studio

The steps required to debug the shared module are the exact steps described earlier — but instead of adding a breakpoint on the Android app, you need to add it on the shared module.

Fig. B.1 — Android Studio Debugger Window
Qip. Y.6 — Igdhaic Xxadoa Xarocmip Juyyav

Debugging in Xcode

As you can see, debugging the shared module from Android Studio is simple. If you want to do the same thing in Xcode, it’s more…challenging. :]

Uninstalling Previous Versions

If you have an older version installed, you’ll need to remove it and update to this new one, otherwise it won’t work with Xcode 15.0.

xcode-kotlin uninstall

Installing the Kotlin Native Xcode Support Plugin

To install the plugin, you need to first close Xcode. Due to some limitations with Xcode 15.0, you’ll need to enable the IDEPerformanceDebugger plugin before installing the xcode-kotlin.

defaults write IDEPerformanceDebuggerEnabled -bool true && killall -u $USER cfprefsd
defaults delete IDEPerformanceDebuggerEnabled && killall -u $USER cfprefsd
brew install xcode-kotlin
xcode-kotlin install
Installing 1.3.0.
Synchronizing plugin compatibility list.
Fig. B.2 — Xcode Folder Hierarchy
Zes. X.7 — Rkezo Serfey Caesuzqmk

Fig. B.3 — Xcode Halt At a Breakpoint
Saw. R.2 — Qhige Hiqx Aw u Swuenkeosn

Debugging Your iOS App

With the app state on hold, Xcode switches to debug mode and shows you a list of actions you can take. As you can see, they’re similar to the ones that Android Studio offers:

Fig. B.4 — Xcode Debugger Window
Riy. K.6 — Wlori Kulaxsuk Damwuh

Where to Go From Here?

Well done! Now that you’ve seen how you can debug your shared module, why not dive deeper into iOS or Android debugging through these video courses? Or if you’re looking for more advanced concepts, try the Advanced Apple Debugging & Reverse Engineering book.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now