我们有很多用跨平台 C++ 编写的业务逻辑。我们想为我们的应用程序编写跨平台 UI,并使用此业务逻辑来创建整个跨平台应用程序。
是否可以将纯 C++ 编写的原生模块暴露给 react-native? 我们不想用本地语言(Java 或 Objective-C)围绕 C++ 代码创建包装器。这样的包装器会增加更多的复杂性,使调试和调查变得更加困难。
最佳答案
我也在寻找一种直接在 C++ 中执行此操作的方法,而无需为 Android 编写 JNI 和为 iOS 编写 Obj-C。我在 native 源代码中找到了 CxxNativeModule
类。查看示例实现 SampleCxxModule .
最后要弄清楚的是如何在 C++ 中注册该模块。在 React Native Android 的 JNI 中,NativeModule
的实现说
NativeModule
s whose implementation is written in C++ must not provide any Java code (so they can be reused on other platforms), and instead should register themselves usingCxxModuleWrapper
.
然后执行CxxModuleWrapper
说
This does nothing interesting, except avoid breaking existing code.
因此,这可能会帮助您入门。最后要弄清楚的是如何注册一个 CxxNativeModule
以便它可以在 JS 中使用。
关于c++ - ReactNative 公开 C++ 本地模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34525781/