Chapters

Hide chapters

Kotlin Multiplatform by Tutorials

First Edition · Android 12, iOS 15, Desktop · Kotlin 1.6.10 · Android Studio Bumblebee

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

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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
Huk. K.0 - Ezhmaug Zhexii Tebuxwus Loxrub

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. :]

Installing the Kotlin Native Xcode Support plugin

To install the plugin, you need to first close Xcode. Then, open the command line and go to the repository root folder. There, enter:

./setup.sh
Creating new Kotlin plugin
Creating new Kotlin language spec
./setup-xcode11.sh
./gradlew createSwiftPackage
Fig. B.2 - Xcode Folder Hierarchy
Qov. M.3 - Cluma Celwib Ceohaznbj

Fig. B.3 - Xcode Halt At a Breakpoint
Guv. N.7 - Wcoqe Kiln Aq o Zceacmuayh

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
Kec. Q.6 - Bjemi Geliwnum Kevpix

Uninstalling the Kotlin Native Xcode Support plugin

There isn’t an option to uninstall a plugin directly from Xcode. The best solution is to close the IDE and go directly to the directory where they’re installed:

~/Library/Developer/Xcode/Plug-ins/

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 accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now