-
Notifications
You must be signed in to change notification settings - Fork 210
The C++ Turbo Module template uses a Kotlin implementation on Android #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think the fix is relatively straightforward to call the native multiply implementation from the Kotlin implementation package com.cpplibtest
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.annotations.ReactModule
@ReactModule(name = CppLibTestModule.NAME)
class CppLibTestModule(reactContext: ReactApplicationContext) :
NativeCppLibTestSpec(reactContext) {
override fun getName(): String {
return NAME
}
// Example method
// See https://reactnative.dev/docs/native-modules-android
override fun multiply(a: Double, b: Double): Double {
- return a * b
+ return nativeMultiply(a, b)
}
companion object {
const val NAME = "CppLibTest"
+ @JvmStatic
+ external fun nativeMultiply(a: Double, b: Double): Double
+ init {
+ System.loadLibrary("cpp-lib-test")
+ }
}
} I think it should be possible to use a pure C++ implementation as described here though whereas this methodology is more of a Kotlin implementation which calls into shared C++ code using the JNI. I'll try take a look into this. |
Oh I see this template has been removed: #818 Is there any plan to support a pure C++ module template in the future? With React Native |
I haven't managed to write a C++ library that works. The official docs only cover how to write one in existing app, https://reactnative.dev/docs/the-new-architecture/pure-cxx-modules, but trying to use same approach with a library doesn't seem to work with autolinking. If you figure out how then PR or a sample library I can turn into a template is welcome. |
I did manage to get iOS working by adding code to the consuming app's However, I stumbled across this youtube video https://www.youtube.com/watch?v=KURIqiCPbco and they used an Objective-C file in the library and registered the native module in the #import <Foundation/Foundation.h>
#import "MyCpp.h"
#import <ReactCommon/CxxTurboModuleUtils.h>
@interface OnLoad : NSObject
@end
@implementation OnLoad
using namespace facebook::react;
+ (void) load {
registerCxxModuleToGlobalModuleMap(
std::string(MyCpp::kModuleName),
[](std::shared_ptr<CallInvoker> jsInvoker) {
return std::make_shared<MyCpp>(jsInvoker);
}
);
}
@end This is working for me. I'm going to watch the Android version next and try implementing that. If I get both working I'll either share a repo or create a PR (if time allows). I'll also ask in the reactwg repo to check this is best practise. |
Ok I have Android working too thanks to the author's other video https://www.youtube.com/watch?v=ibgHObp8YwA&t=8s This repo has the changes for Android & iOS react-native-pure-cpp-turbo-module-library. If I get time I could attempt a PR to add it into the |
@Zach-Dean-Attractions-io thank you! i'll check it out |
@Zach-Dean-Attractions-io I was able to setup a sample with the latest template based on your example https://github.com/satya164/react-native-pure-cpp-turbo-module-library However, before we can include it as a template, we would want to disable |
Description
I'm not sure if I'm missing something obvious but when creating the library template for a Turbo Module using C++ for Android & iOS there is a default example implementing a
multiply
function. However, on Android the implementation for this function is in Kotlin and it doesn't use the shared C++ library.When the JS code calls
multiply
it comes into the code here.Is there any reason why the
OnLoad.cpp
file isn't used to register the turbo module in this case as described here: pure-cxx-modules#androidPackages
Selected options
npx create-react-native-library cppLibraryTest
Link to repro
N/A
Environment
The text was updated successfully, but these errors were encountered: