nitro icon indicating copy to clipboard operation
nitro copied to clipboard

[Feature] Allow using C++ HybridObjects from Swift/Kotlin

Open puelocesar opened this issue 2 months ago • 2 comments

I'm converting my old RCT_EXTERN_MODULE into Nitro, and almost everything have been wonderful. But I reached a blocker that I just cannot bypass.

In my old module, I had a Bridging-Header.h that imported a bunch of C files I needed for some specific parsing (and which I reused on Android and Swift implementation).

But in the Nitro module, I just cannot add it, because I get a bunch of errors:

Using bridging headers with framework targets is unsupported Unexpected input file: /Users/pauli/project-new-arch/modules/my-native-modules/ios/MyCoolC-Bridge.h

So instead, perhaps we could do something like this:

export interface NativeNmea
  extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
  parse(sentence: string): AnyMap;
}

Let nitrogen generate the headers, implement in cpp, and then just use it in Swift. Currently, I was only able to call the cpp method from Js, never from Swift.

puelocesar avatar Oct 29 '25 10:10 puelocesar

Hmm yea I think it'd be super cool to be able to use C++ HybridObjects from Swift/Kotlin - right now only the other way around is supported (using Swift/Kotlin HybridObjects from C++). This is a very complex feature to build though, and I think you'd be better off for now to just use a workaround - some that come to mind;

  • a) fixing the Bridging Header; this has to work somehow, lol.
  • b) Creating a C++ HybridObject that takes a Callback that does your desired business logic (the Callback is then something your Swift/Kotlin HybridObject created - very weird DX imo but that works)

mrousavy avatar Oct 29 '25 10:10 mrousavy

Only way I could make it work was adding a #include "Bridging.hpp" to MyNativeModules-umbrella.h. The problem is that it gets removed every time I run pod install.

I fixed that with some crazy post_install changes with an expo prebuild script.

Now let's see if I can make it work on Android as well! Hopefully I can just keep using multiple CMakeLists like I had before

--

But yeah, I thought about option b. Problem is my type is very complex, so I would have to use AnyMap. Would that be bad?

puelocesar avatar Oct 29 '25 13:10 puelocesar