@@ -49,21 +49,30 @@ export function createHybridObjectIntializer() {
4949${ createFileMetadataString ( `${ autolinkingClassName } .hpp` ) }
5050
5151#include <jni.h>
52+ #include <functional>
5253#include <NitroModules/NitroDefines.hpp>
5354
5455namespace ${ cxxNamespace } {
5556
57+ [[deprecated("Use registerNatives() instead.")]]
58+ int initialize(JavaVM* vm);
59+
5660 /**
57- * Initializes the native (C++) part of ${ cppLibName } , and autolinks all Hybrid Objects.
58- * Call this in your \`JNI_OnLoad\` function (probably inside \`cpp-adapter.cpp\`).
61+ * Register the native (C++) part of ${ cppLibName } , and autolinks all Hybrid Objects.
62+ * Call this in your \`JNI_OnLoad\` function (probably inside \`cpp-adapter.cpp\`),
63+ * inside a \`facebook::jni::initialize(vm, ...)\` call.
5964 * Example:
6065 * \`\`\`cpp (cpp-adapter.cpp)
6166 * JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
62- * return ${ cxxNamespace } ::initialize(vm);
67+ * return facebook::jni::initialize(vm, []() {
68+ * // register all ${ cppLibName } HybridObjects
69+ * ${ cxxNamespace } ::registerNatives();
70+ * // any other custom registrations go here.
71+ * });
6372 * }
6473 * \`\`\`
6574 */
66- int initialize(JavaVM* vm );
75+ void registerAllNatives( );
6776
6877} // namespace ${ cxxNamespace }
6978
@@ -86,17 +95,20 @@ ${includes}
8695namespace ${ cxxNamespace } {
8796
8897int initialize(JavaVM* vm) {
98+ return facebook::jni::initialize(vm, []() {
99+ ::${ cxxNamespace } ::registerAllNatives();
100+ });
101+ }
102+
103+ void registerAllNatives() {
89104 using namespace margelo::nitro;
90105 using namespace ${ cxxNamespace } ;
91- using namespace facebook;
92106
93- return facebook::jni::initialize(vm, [] {
94- // Register native JNI methods
95- ${ indent ( jniRegistrations . join ( '\n' ) , ' ' ) }
107+ // Register native JNI methods
108+ ${ indent ( jniRegistrations . join ( '\n' ) , ' ' ) }
96109
97- // Register Nitro Hybrid Objects
98- ${ indent ( cppRegistrations . join ( '\n' ) , ' ' ) }
99- });
110+ // Register Nitro Hybrid Objects
111+ ${ indent ( cppRegistrations . join ( '\n' ) , ' ' ) }
100112}
101113
102114} // namespace ${ cxxNamespace }
0 commit comments