Skip to content

Commit

Permalink
fix: Fix android build (#112)
Browse files Browse the repository at this point in the history
* fix: upgrade to support 0.76+

* fix: migrate to lite-rt

* chore: remove submodule
  • Loading branch information
bang9 authored Dec 11, 2024
1 parent bde0f6f commit ccbe815
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
6 changes: 0 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ updates:
interval: "weekly"
labels:
- "dependencies"
- package-ecosystem: "gitsubmodule"
directory: "/"
schedule:
interval: "monthly"
labels:
- "dependencies"
# - package-ecosystem: "npm"
# directory: "./"
# schedule:
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "tensorflow"]
path = tensorflow
url = https://github.com/tensorflow/tensorflow
19 changes: 15 additions & 4 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ find_package(fbjni REQUIRED CONFIG)
find_library(
TFLITE
tensorflowlite_jni
PATHS "./src/main/cpp/lib/tensorflow/jni/${ANDROID_ABI}"
PATHS "./src/main/cpp/lib/litert/jni/${ANDROID_ABI}"
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)

find_library(
TFLITE_GPU
tensorflowlite_gpu_jni
PATHS "./src/main/cpp/lib/tensorflow/jni/${ANDROID_ABI}"
PATHS "./src/main/cpp/lib/litert/jni/${ANDROID_ABI}"
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
Expand All @@ -42,7 +42,7 @@ target_include_directories(
PRIVATE
"../cpp"
"src/main/cpp"
"../tensorflow/"
"src/main/cpp/lib/litert/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 @@ -54,8 +54,19 @@ target_link_libraries(
${PACKAGE_NAME}
android # <-- log
ReactAndroid::jsi # <-- jsi.h
ReactAndroid::reactnativejni # <-- CallInvokerImpl
fbjni::fbjni # <-- fbjni.h
${TFLITE}
${TFLITE_GPU}
)

if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::reactnative # <-- RN merged so
)
else()
target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::reactnativejni # <-- CallInvokerImpl
)
endif()
28 changes: 24 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ android {
"**/libc++_shared.so",
"**/libfbjni.so",
"**/libjsi.so",
"**/libreactnative.so",
"**/libreactnativejni.so",
"**/libturbomodulejsijni.so",
"**/libreact_nativemodule_core.so",
"**/libtensorflowlite_jni.so",
"**/libtensorflowlite_gpu_jni.so",
]
}
buildTypes {
Expand Down Expand Up @@ -123,12 +126,28 @@ dependencies {
implementation "com.facebook.react:react-native:+"

// Tensorflow Lite .aar (includes C API via prefabs)
implementation "org.tensorflow:tensorflow-lite:2.16.1"
extractSO("org.tensorflow:tensorflow-lite:2.16.1")
implementation "com.google.ai.edge.litert:litert:1.0.1"
extractSO("com.google.ai.edge.litert:litert:1.0.1")
extractHeaders("com.google.ai.edge.litert:litert:1.0.1")

// Tensorflow Lite GPU delegate
implementation "org.tensorflow:tensorflow-lite-gpu:2.16.1"
extractSO("org.tensorflow:tensorflow-lite-gpu:2.16.1")
implementation "com.google.ai.edge.litert:litert-gpu:1.0.1"
extractSO("com.google.ai.edge.litert:litert-gpu:1.0.1")
extractHeaders("com.google.ai.edge.litert:litert-gpu:1.0.1")
}

task extractAARHeaders {
doLast {
configurations.extractHeaders.files.each {
def file = it.absoluteFile
def packageName = file.name.tokenize('-')[0]
copy {
from zipTree(file)
into "src/main/cpp/lib/$packageName/"
include "**/*.h"
}
}
}
}

task extractSOFiles {
Expand Down Expand Up @@ -166,5 +185,6 @@ def nativeBuildDependsOn(dependsOnTask) {
}

afterEvaluate {
nativeBuildDependsOn(extractAARHeaders)
nativeBuildDependsOn(extractSOFiles)
}
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"src",
"lib",
"android/src",
"!android/src/main/cpp/lib",
"android/build.gradle",
"android/CMakeLists.txt",
"android/gradle.properties",
Expand All @@ -26,14 +27,11 @@
"typecheck": "tsc --noEmit",
"lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
"lint-cpp": "scripts/clang-format.sh",
"prepare": "git submodule update --init --recursive",
"update-submodule": "git submodule update --remote --merge",
"check-all": "scripts/check-all.sh",
"prepack": "bob build",
"release": "release-it",
"example": "yarn --cwd example",
"configure": "git submodule update --init --recursive && cd tensorflow && ./configure",
"bootstrap": "yarn && yarn example && yarn example pods && yarn configure"
"bootstrap": "yarn && yarn example && yarn example pods"
},
"keywords": [
"react-native",
Expand Down
1 change: 0 additions & 1 deletion tensorflow
Submodule tensorflow deleted from e960f1

0 comments on commit ccbe815

Please sign in to comment.