Skip to content

Commit

Permalink
fix: ⚠️ Stub out Android implementation (TODO: Get it to build, then …
Browse files Browse the repository at this point in the history
…revert this)
  • Loading branch information
mrousavy committed Aug 22, 2023
1 parent adfb632 commit 4578795
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
23 changes: 12 additions & 11 deletions android/CMakeLists.txt
Expand Up @@ -8,12 +8,13 @@ set (CMAKE_CXX_STANDARD 17)
find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)

find_library(
TFLITE
tensorflowlite_jni
PATHS "./src/main/cpp/lib/tensorflow/jni/${ANDROID_ABI}"
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH)
# TODO: Fix linking TFLite
# find_library(
# TFLITE
# tensorflowlite_jni
# PATHS "./src/main/cpp/lib/tensorflow/jni/${ANDROID_ABI}"
# NO_DEFAULT_PATH
# NO_CMAKE_FIND_ROOT_PATH)

string(APPEND CMAKE_CXX_FLAGS " -DANDROID")

Expand All @@ -22,8 +23,9 @@ add_library(
SHARED
../cpp/jsi/Promise.cpp
../cpp/jsi/TypedArray.cpp
../cpp/TensorflowPlugin.cpp
../cpp/TensorHelpers.cpp
# TODO: Uncomment this when tensorflow-lite C/C++ API can be successfully built/linked here
#../cpp/TensorflowPlugin.cpp
#../cpp/TensorHelpers.cpp
src/main/cpp/Tflite.cpp
)

Expand All @@ -33,8 +35,7 @@ target_include_directories(
PRIVATE
"../cpp"
"src/main/cpp"
"src/main/cpp/lib/tensorflow/headers/"
"../tensorflow/"
# "src/main/cpp/lib/tensorflow/headers/"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule" # <-- CallInvokerHolder JNI wrapper
Expand All @@ -48,5 +49,5 @@ target_link_libraries(
ReactAndroid::jsi # <-- jsi.h
ReactAndroid::reactnativejni # <-- CallInvokerImpl
fbjni::fbjni # <-- fbjni.h
${TFLITE}
# ${TFLITE}
)
12 changes: 12 additions & 0 deletions android/build.gradle
Expand Up @@ -73,6 +73,18 @@ android {
path "CMakeLists.txt"
}
}
packagingOptions {
excludes = [
"META-INF",
"META-INF/**",
"**/libc++_shared.so",
"**/libfbjni.so",
"**/libjsi.so",
"**/libreactnativejni.so",
"**/libturbomodulejsijni.so",
"**/libreact_nativemodule_core.so",
]
}
buildTypes {
release {
minifyEnabled false
Expand Down
22 changes: 19 additions & 3 deletions android/src/main/cpp/Tflite.cpp
Expand Up @@ -4,7 +4,8 @@
#include <exception>
#include <memory>

#include "TensorflowPlugin.h"
// TODO: Uncomment this when tensorflow-lite C/C++ API can be successfully built/linked here
// #include "TensorflowPlugin.h"
#include <ReactCommon/CallInvoker.h>
#include <ReactCommon/CallInvokerHolder.h>

Expand All @@ -28,7 +29,8 @@ struct TfliteModule : public jni::JavaClass<TfliteModule> {
}
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();

auto fetchByteDataFromUrl = [](std::string url) {
// TODO: Uncomment this when tensorflow-lite C/C++ API can be successfully built/linked here
/*auto fetchByteDataFromUrl = [](std::string url) {
static const auto cls = javaClassStatic();
static const auto method = cls->getStaticMethod<jbyteArray(std::string)>("fetchByteDataFromUrl");
Expand All @@ -42,9 +44,23 @@ struct TfliteModule : public jni::JavaClass<TfliteModule> {
.size = size
};
};
*/

try {
TensorflowPlugin::installToRuntime(*runtime, jsCallInvoker, fetchByteDataFromUrl);
// TODO: Uncomment this when tensorflow-lite C/C++ API can be successfully built/linked here
// TensorflowPlugin::installToRuntime(*runtime, jsCallInvoker, fetchByteDataFromUrl);

// TODO: Remove this when tensorflow-lite C/C++ API can be successfully built/linked here
auto func = jsi::Function::createFromHostFunction(*runtime,
jsi::PropNameID::forAscii(*runtime, "__loadTensorflowModel"),
1,
[=](jsi::Runtime& runtime,
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value {
throw jsi::JSError(runtime, "vision-camera-tflite is not yet supported on Android! I couldn't manage to get TFLite to build for NDK/C++ :/");
});
runtime->global().setProperty(*runtime, "__loadTensorflowModel", func);
} catch (std::exception &exc) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/tflite/TfliteModule.java
Expand Up @@ -20,7 +20,7 @@
/** @noinspection JavaJniMissingFunction*/
@ReactModule(name = TfliteModule.NAME)
public class TfliteModule extends ReactContextBaseJavaModule {
public static final String NAME = "vision-camera-tflite";
public static final String NAME = "Tflite";

public TfliteModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -54,7 +54,7 @@ public static byte[] fetchByteDataFromUrl(String url) {
public boolean install() {
try {
Log.i(NAME, "Loading C++ library...");
System.loadLibrary("vision-camera-tflite");
System.loadLibrary("VisionCameraTflite");

JavaScriptContextHolder jsContext = getReactApplicationContext().getJavaScriptContextHolder();
CallInvokerHolderImpl callInvoker = (CallInvokerHolderImpl) getReactApplicationContext().getCatalystInstance().getJSCallInvokerHolder();
Expand Down

0 comments on commit 4578795

Please sign in to comment.