flowkey/UIKit-cross-platform

How to debug the Swift source in Android Studio?

vinceplusplus opened this issue · 3 comments

Like setting a breakpoint inside a Swift source file? Or what is the suggested way to debug?

For the getting-started sample, I can't seem to set a breakpoint on any Swift source files, such as androidNativeInit.swift

The native debugger is currently not supported, unfortunately. We don't really understand why not, because lldb should be supported, but it's probably because Swift ships with its own version of lldb that is difficult to install on an Android device without enabling root mode on there

Debugging partially works, but no swift support in NDK's lldb.

  1. You need to configure debug in buildTypes
    buildTypes {
        release {
            minifyEnabled false
        }
        debug {
            // The doNotStrip option is not properly scoped to the "debug" build type
            // See https://issuetracker.google.com/issues/155215248.
            packagingOptions {
                doNotStrip '**/*.so'
            }
            debuggable true
            jniDebuggable true
        }
    }
  1. Change build type to Debug option in CMake. In getting-started sample it will be like this
def getSwiftBuildCommand(abi) {
    def cmakeBuildType = "Debug"
    def buildScript = "../../../../swift-android-toolchain/swift-build.sh"
    return "CMAKE_BUILD_TYPE=$cmakeBuildType ANDROID_ABI=$abi LIBRARY_OUTPUT_DIRECTORY='${projectDir}/src/main/jniLibs/${abi}' $buildScript ../../"
}
  1. In Android Studio go to Run -> Edit Configurations... -> Debugger tab
    Select Dual (Java + Native).
    Set starting lldb command: settings set target.language swift - maybe one day this will just work!

More useful reads on lldb https://github.com/rustymagnet3000/lldb_debugger

Btw, I was able to use xcode's lldb and lldb-server from apple's llvm fork.
Not the best experience but looks promising
Screenshot 2023-07-26 at 11 22 48
Screenshot 2023-07-26 at 11 35 42