Is it possible to use the C++ interop with the Android NDK?

I’m excited for the imminent release of Swift 5.9 and I was thinking about the new possibilities unlocked by C++ interop. Swift on Android has worked for a while, but you got little more than the stdlib and bridging was a pain.

With C++ interop, I assume we could at least write Swift code and use it on Android by bridging through C++, but I’m wondering if we could just skip the C++ intermediate layer and just call Swift directly from Kotlin through the NDK as if it were C++?

Also I’m guessing we can use the existing system C / C++ libraries normally from Swift? Though, if they are non-modular I guess we might have to use some kind of bridging header?

Not true, I have been distributing a full SDK for years, which can be used to compile many Swift packages for Android with minimal changes.

There is a wrapper to make this easier, been around even longer.

As in, without using whatever Kotlin<->C++ interop there is now, but using some new Kotlin<->Swift interop? Somebody would have to work on that, but I don't know of anybody who is.

For C, this has long been available through Swift's C interop. I don't believe there's much system C++ APIs, if any.

Keep in mind that google doesn't want you using C/C++ on Android, so what exists in the NDK is the bare minimum to port over legacy code. For example, you cannot access the Android GUI APIs directly from C++, only from the JVM, even though that GUI is implemented in C++.

The Android libc++ uses a modified version of the upstream LLVM modulemap, so that works, but the Bionic libc doesn't have one. I opened an issue for it years ago, but I doubt anybody will work on it anytime soon. Swift's C++ Interop is in a semi-broken state on Android, partially because of those modulemap issues, as we currently just use the glibc modulemap that comes with the Swift toolchain.

For other C system headers, yes, you may need a modulemap, just like that JNI wrapper has.

To sum up, Android has almost no system C++ APIs, Swift's C++ Interop is currently incomplete on Android (note all the Interop/*Cxx tests marked as unsupported on the community Android CI), and Swift already works fairly well on Android, so support for C++ interop would add almost nothing on that platform.

2 Likes

You can run a full C++ application in the NDK, it's just that interactions with the Android event loop have to be creatively managed.

Sure, that is mainly meant for porting existing games that have their own UI widgets rendering to OpenGLES. But you have to ship the C++ stdlib with your app, you can't even use the one in the system. If they were really trying to enable C++ devs, they could easily make available C or C++ headers/libraries for the native Android GUI, but they choose not to.

I can't blame them for trying to sunset use of C/C++ in mobile apps, I just wish they would provide a modern natively-compiled alternative like Apple is. I tried Kotlin/Native years ago, it was not a good dev experience. There is some interest in adding Rust, but the NDK devs say it "is not being worked on, nor is it planned."