From dfbdb2a4f598536cf4c6f8a73c915a3966d9530a Mon Sep 17 00:00:00 2001 From: karol-bisztyga Date: Tue, 2 Mar 2021 13:33:33 +0100 Subject: [PATCH] working --- App.js | 3 ++ android/app/CMakeLists.txt | 26 ++++++++++++----- android/app/build.gradle | 18 +++++++++--- android/app/src/cpp/empty.cpp | 0 android/app/src/cpp/sample.cpp | 26 +++++++++++++++++ .../main/java/com/rnfbjni/MainActivity.java | 29 ++++++++++++++++++- .../gradle/wrapper/gradle-wrapper.properties | 4 +-- 7 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 android/app/src/cpp/empty.cpp diff --git a/App.js b/App.js index 181f3ce..04955d9 100644 --- a/App.js +++ b/App.js @@ -3,9 +3,12 @@ import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; export default function App() { + const test = global.mytest ?? 'not working :( :( :('; + console.log('here', test) return ( Open up App.js to start working on your app! + HERE: { test } ); diff --git a/android/app/CMakeLists.txt b/android/app/CMakeLists.txt index b5c4522..370474b 100644 --- a/android/app/CMakeLists.txt +++ b/android/app/CMakeLists.txt @@ -24,7 +24,15 @@ set(PACKAGE_NAME "my_jni_module") #"fbjni_DIR" to a directory containing one of the above files. If "fbjni" #provides a separate development package or SDK, be sure it has been #installed. -#find_package(fbjni REQUIRED CONFIG) +find_package(fbjni REQUIRED CONFIG) + +include_directories( + ../../node_modules/react-native/React + ../../node_modules/react-native/React/Base + ../../node_modules/react-native/ReactCommon + ../../node_modules/react-native/ReactCommon/jsi + ./src/cpp/ +) add_library( # Sets the name of the library. ${PACKAGE_NAME} @@ -33,17 +41,21 @@ add_library( # Sets the name of the library. SHARED # Provides a relative path to your source file(s). + ../../node_modules/react-native/ReactCommon/jsi/jsi/jsi.cpp ./src/cpp/sample.cpp ) target_link_libraries(${PACKAGE_NAME} -# fbjni::fbjni + fbjni::fbjni android log ) -#target_link_libraries(my_jni_module -# android -# log -# fbjni::fbjni -#) \ No newline at end of file + +add_library( + turbomodulejsijni + # Sets the library as a shared library. + SHARED + # Provides a relative path to your source file(s). + ./src/cpp/empty.cpp +) diff --git a/android/app/build.gradle b/android/app/build.gradle index f96cafd..35d6bb6 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -134,13 +134,15 @@ repositories { } android { + buildFeatures { + prefab true + } // this also causes // Duplicate class com.facebook.jni.xxx found in modules jetified-fbjni-0.1.0-runtime (com.facebook.fbjni:fbjni:0.1.0) and jetified-fbjni-java-only-0.0.3 (com.facebook.fbjni:fbjni-java-only:0.0.3) // where xxx are 17 different classes - dependencies { - implementation 'com.facebook.fbjni:fbjni:0.1.0' - } - +// dependencies { +// implementation 'com.facebook.fbjni:fbjni:0.1.0' +// } compileSdkVersion rootProject.ext.compileSdkVersion compileOptions { @@ -184,6 +186,14 @@ android { } } + defaultConfig { + externalNativeBuild { + cmake { + arguments "-DANDROID_STL=c++_shared" + } + } + } + externalNativeBuild { cmake { path "CMakeLists.txt" diff --git a/android/app/src/cpp/empty.cpp b/android/app/src/cpp/empty.cpp new file mode 100644 index 0000000..e69de29 diff --git a/android/app/src/cpp/sample.cpp b/android/app/src/cpp/sample.cpp index e69de29..6a2dc06 100644 --- a/android/app/src/cpp/sample.cpp +++ b/android/app/src/cpp/sample.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +// #include "DraftNativeModule.h" + +using namespace facebook; +/**/ +extern "C" +{ + JNIEXPORT void JNICALL + Java_com_rnfbjni_MainActivity_install(JNIEnv *env, jobject thiz, jlong runtimePtr) + { + jsi::Runtime *runtime = (jsi::Runtime *)runtimePtr; + // std::shared_ptr nativeModule = std::make_shared(jsCallInvoker); + // + // runtime->global().setProperty( + // runtime, + // jsi::PropNameID::forAscii(*runtime, "commModule"), + // jsi::Object::createFromHostObject(runtime, nativeModule)); + runtime->global().setProperty( + *runtime, + jsi::PropNameID::forAscii(*runtime, "mytest"), + jsi::Value(*runtime, 777333)); + } +} +/**/ diff --git a/android/app/src/main/java/com/rnfbjni/MainActivity.java b/android/app/src/main/java/com/rnfbjni/MainActivity.java index dde6aa3..d98438c 100644 --- a/android/app/src/main/java/com/rnfbjni/MainActivity.java +++ b/android/app/src/main/java/com/rnfbjni/MainActivity.java @@ -10,7 +10,16 @@ import expo.modules.splashscreen.singletons.SplashScreen; import expo.modules.splashscreen.SplashScreenImageResizeMode; -public class MainActivity extends ReactActivity { +import com.facebook.react.ReactInstanceManager; +import com.facebook.react.turbomodule.core.CallInvokerHolderImpl; +import com.facebook.react.bridge.ReactContext; + +public class MainActivity extends ReactActivity implements ReactInstanceManager.ReactInstanceEventListener { + + static { + System.loadLibrary("my_jni_module"); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -19,7 +28,17 @@ protected void onCreate(Bundle savedInstanceState) { SplashScreen.show(this, SplashScreenImageResizeMode.CONTAIN, ReactRootView.class, false); } + @Override + public void onResume() { + super.onResume(); + getReactInstanceManager().addReactInstanceEventListener(this); + } + @Override + public void onPause() { + super.onPause(); + getReactInstanceManager().removeReactInstanceEventListener(this); + } /** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. @@ -38,4 +57,12 @@ protected ReactRootView createRootView() { } }; } + + @Override + public void onReactContextInitialized(ReactContext context) { + CallInvokerHolderImpl holder = (CallInvokerHolderImpl)context.getCatalystInstance().getJSCallInvokerHolder(); + install(context.getJavaScriptContextHolder().get()); + } + + public native void install(long jsContextNativePointer); } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index e8666dc..ebb1ee3 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon Mar 01 17:15:43 CET 2021 +#Tue Mar 02 12:48:36 CET 2021 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip