Skip to content

Commit a6ea11c

Browse files
Zach DeanZach Dean
Zach Dean
authored and
Zach Dean
committed
Attempt at pure C++ module for Android
1 parent 84cc053 commit a6ea11c

File tree

8 files changed

+147
-81
lines changed

8 files changed

+147
-81
lines changed

android/CMakeLists.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
cmake_minimum_required(VERSION 3.4.1)
2-
project(PureCppTurboModuleLibrary)
1+
cmake_minimum_required(VERSION 3.13)
32

4-
set (CMAKE_VERBOSE_MAKEFILE ON)
5-
set (CMAKE_CXX_STANDARD 14)
3+
project(PureCppTurboModuleLibrary)
64

7-
add_library(pure-cpp-turbo-module-library SHARED
8-
../cpp/pure-cpp-turbo-module-library.cpp
9-
cpp-adapter.cpp
10-
)
5+
add_library(pure-cpp-turbo-module-library SHARED ../cpp/PureCppImpl.cpp)
116

12-
# Specifies a path to native header files.
7+
# Specifies paths to native header files.
138
include_directories(
149
../cpp
10+
generated/jni
1511
)

android/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ dependencies {
121121
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
122122
}
123123

124-
react {
125-
jsRootDir = file("../src/")
126-
libraryName = "PureCppTurboModuleLibrary"
127-
codegenJavaPackageName = "com.purecppturbomodulelibrary"
128-
}
124+
//react {
125+
// jsRootDir = file("../src/")
126+
// libraryName = "PureCppTurboModuleLibrary"
127+
// codegenJavaPackageName = "com.purecppturbomodulelibrary"
128+
//}

android/cpp-adapter.cpp

Lines changed: 0 additions & 8 deletions
This file was deleted.

android/src/main/java/com/purecppturbomodulelibrary/PureCppTurboModuleLibraryModule.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

android/src/main/java/com/purecppturbomodulelibrary/PureCppTurboModuleLibraryPackage.kt

Lines changed: 0 additions & 33 deletions
This file was deleted.

android/src/main/jni/OnLoad.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// This C++ file is part of the default configuration used by apps and is placed
9+
// inside react-native to encapsulate it from user space (so you won't need to
10+
// touch C++/Cmake code at all on Android).
11+
//
12+
// If you wish to customize it (because you want to manually link a C++ library
13+
// or pass a custom compilation flag) you can:
14+
//
15+
// 1. Copy this CMake file inside the `android/app/src/main/jni` folder of your
16+
// project
17+
// 2. Copy the OnLoad.cpp (in this same folder) file inside the same folder as
18+
// above.
19+
// 3. Extend your `android/app/build.gradle` as follows
20+
//
21+
// android {
22+
// // Other config here...
23+
// externalNativeBuild {
24+
// cmake {
25+
// path "src/main/jni/CMakeLists.txt"
26+
// }
27+
// }
28+
// }
29+
30+
#include <DefaultComponentsRegistry.h>
31+
#include <DefaultTurboModuleManagerDelegate.h>
32+
#include <autolinking.h>
33+
#include <fbjni/fbjni.h>
34+
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
35+
#include <rncore.h>
36+
37+
#include "PureCppImpl.h"
38+
39+
#ifdef REACT_NATIVE_APP_CODEGEN_HEADER
40+
#include REACT_NATIVE_APP_CODEGEN_HEADER
41+
#endif
42+
#ifdef REACT_NATIVE_APP_COMPONENT_DESCRIPTORS_HEADER
43+
#include REACT_NATIVE_APP_COMPONENT_DESCRIPTORS_HEADER
44+
#endif
45+
46+
namespace facebook::react {
47+
48+
void registerComponents(
49+
std::shared_ptr<const ComponentDescriptorProviderRegistry> registry) {
50+
// Custom Fabric Components go here. You can register custom
51+
// components coming from your App or from 3rd party libraries here.
52+
//
53+
// providerRegistry->add(concreteComponentDescriptorProvider<
54+
// MyComponentDescriptor>());
55+
56+
// We link app local components if available
57+
#ifdef REACT_NATIVE_APP_COMPONENT_REGISTRATION
58+
REACT_NATIVE_APP_COMPONENT_REGISTRATION(registry);
59+
#endif
60+
61+
// And we fallback to the components autolinked
62+
autolinking_registerProviders(registry);
63+
}
64+
65+
std::shared_ptr<TurboModule> cxxModuleProvider(
66+
const std::string& name,
67+
const std::shared_ptr<CallInvoker>& jsInvoker) {
68+
// Here you can provide your CXX Turbo Modules coming from
69+
// either your application or from external libraries. The approach to follow
70+
// is similar to the following (for a module called `NativeCxxModuleExample`):
71+
//
72+
if (name == PureCppImpl::kModuleName) {
73+
return std::make_shared<PureCppImpl>(jsInvoker);
74+
}
75+
76+
// And we fallback to the CXX module providers autolinked
77+
return autolinking_cxxModuleProvider(name, jsInvoker);
78+
79+
return nullptr;
80+
}
81+
82+
std::shared_ptr<TurboModule> javaModuleProvider(
83+
const std::string& name,
84+
const JavaTurboModule::InitParams& params) {
85+
// Here you can provide your own module provider for TurboModules coming from
86+
// either your application or from external libraries. The approach to follow
87+
// is similar to the following (for a library called `samplelibrary`):
88+
//
89+
// auto module = samplelibrary_ModuleProvider(name, params);
90+
// if (module != nullptr) {
91+
// return module;
92+
// }
93+
// return rncore_ModuleProvider(name, params);
94+
95+
// We link app local modules if available
96+
#ifdef REACT_NATIVE_APP_MODULE_PROVIDER
97+
auto module = REACT_NATIVE_APP_MODULE_PROVIDER(name, params);
98+
if (module != nullptr) {
99+
return module;
100+
}
101+
#endif
102+
103+
// We first try to look up core modules
104+
if (auto module = rncore_ModuleProvider(name, params)) {
105+
return module;
106+
}
107+
108+
// And we fallback to the module providers autolinked
109+
if (auto module = autolinking_ModuleProvider(name, params)) {
110+
return module;
111+
}
112+
113+
return nullptr;
114+
}
115+
116+
} // namespace facebook::react
117+
118+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
119+
return facebook::jni::initialize(vm, [] {
120+
facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider =
121+
&facebook::react::cxxModuleProvider;
122+
facebook::react::DefaultTurboModuleManagerDelegate::javaModuleProvider =
123+
&facebook::react::javaModuleProvider;
124+
facebook::react::DefaultComponentsRegistry::
125+
registerComponentDescriptorsFromEntryPoint =
126+
&facebook::react::registerComponents;
127+
});
128+
}

cpp/PureCppImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <RNPureCppTurboModuleLibrarySpecJSI.h>
3+
#include <react/renderer/components/RNPureCppTurboModuleLibrarySpec/RNPureCppTurboModuleLibrarySpecJSI.h>
44

55
#include <memory>
66
#include <string>

example/ios/PureCppTurboModuleLibraryExample.xcodeproj/project.pbxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@
384384
"-DFOLLY_CFG_NO_COROUTINES=1",
385385
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
386386
);
387-
OTHER_LDFLAGS = "$(inherited) ";
387+
OTHER_LDFLAGS = (
388+
"$(inherited)",
389+
" ",
390+
);
388391
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
389392
SDKROOT = iphoneos;
390393
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
@@ -453,7 +456,10 @@
453456
"-DFOLLY_CFG_NO_COROUTINES=1",
454457
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
455458
);
456-
OTHER_LDFLAGS = "$(inherited) ";
459+
OTHER_LDFLAGS = (
460+
"$(inherited)",
461+
" ",
462+
);
457463
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
458464
SDKROOT = iphoneos;
459465
USE_HERMES = true;

0 commit comments

Comments
 (0)