-
Notifications
You must be signed in to change notification settings - Fork 212
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 |
Hi @satya164 I did see in this PR #819 it was planned to remove However, that doesn't matter really as the library template should work with |
I haven't had much time to look into it sorry. However, from what I can see there is a file generated called
Then in the Gradle task
The first I manually set it to
But then I run into another error.
The generated CMake file adds a library However, some of the paths here such as Another example is
I might have to ask this in the react working group because I can't quite see that it is possible but I am sure that's more my lack of understanding. |
Ok I think I have found the problem. I took a look at the recent version of This isn't happening in this project and so the build fails. I think this is due to the fact there is no If you clone this repo you should be able to remove You may not need the files within android/src/main but I think you need the I think it is this code that lets the React Native Plugin know our library is using codegen.
|
@Zach-Dean-Attractions-io thanks a lot for the investigation. I'll check it out. ideally it shouldn't need a |
Ah right I see! I'm not sure in that case sorry. Let me know what you find. |
@satya164 I tried out your repo with the fixes but still the same error exists, am I missing any configs https://github.com/satya164/react-native-pure-cpp-turbo-module-library
|
You can revert the last commit to have a working implementation with |
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: