Skip to content

Commit 61c501c

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Use fbjni from maven (#6163)
Summary: TL;DR: We need to make sure the runtime so deps is the same as compile time so library. We can't build the so from source, because runtime uses another one. Instead of importing fbjni repo into executorch, and building from executorch as a subdir, we need to use a pre-built (on maven repo) fbjni.so together with its headers. We need to link our executorch JNI library against that fbjni.so. We can't link against fbjni as a static library, because the Java layer needs to load it, and we can't have duplicated symbol from static and shared lib. For Android apk developer, they need to import executorch as AAR, as well as fbjni AAR from maven. Inside fbjni AAR, we don't know which NDK is the fbjni.so built. If the NDK version between executorch and fbjni is different, we may have incompatible c++ library. This caused issues like ``` java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZTVNSt6__ndk114basic_ifstreamIcNS_11char_traitsIcEEEE" referenced by "/data/app/~~enBXzd5mY8qQNSRdnGW4vg==/com.example.executorchllamademo-u8anctHQVTL1jVJsZsEqRQ==/lib/arm64/libexecutorch.so"... ``` in the past. Therefore, we need to always make sure we have a specific fbjni version for a given executorch release (say 0.5.1 for 20241010). We download the AAR from maven, which provides "prefab" artifacts, including the so library and header for us to compile ET jni code. With this change, we can build ET JNI with any NDK version, regardless of how fbjni built their library. Pull Request resolved: #6163 Reviewed By: larryliu0820 Differential Revision: D64249736 Pulled By: kirklandsign fbshipit-source-id: 0e415d6f1780ef35253507899366d302df80c82b
1 parent d094b09 commit 61c501c

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
[submodule "backends/xnnpack/third-party/pthreadpool"]
2929
path = backends/xnnpack/third-party/pthreadpool
3030
url = https://github.com/Maratyszcza/pthreadpool.git
31-
[submodule "examples/third-party/fbjni"]
32-
path = examples/third-party/fbjni
33-
url = https://github.com/facebookincubator/fbjni.git
3431
[submodule "extension/llm/third-party/abseil-cpp"]
3532
path = extension/llm/third-party/abseil-cpp
3633
url = https://github.com/abseil/abseil-cpp.git

examples/third-party/fbjni

Lines changed: 0 additions & 1 deletion
This file was deleted.

extension/android/CMakeLists.txt

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,40 @@ include(${EXECUTORCH_ROOT}/build/Utils.cmake)
2121
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
2222
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
2323

24-
add_subdirectory(
25-
${EXECUTORCH_ROOT}/examples/third-party/fbjni
26-
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
24+
# We need to download fbjni library from maven, and use its "prefab" library
25+
# and headers, and link executorch library against that fbjni library.
26+
# We don't know which NDK is used to compile fbjni, and we need to link our
27+
# executorch library to the version which Android APK links against for runtime
28+
# to ensure the libc++ dependencies are consistent.
29+
# WARNING #
30+
# Users need to use the SAME fbjni version here and in app gradle dependency
31+
# for runtime compatibility!
32+
if(NOT FBJNI_VERSION)
33+
set(FBJNI_VERSION 0.5.1)
34+
endif()
35+
36+
set(FBJNI_AAR_URL https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar)
37+
set(FBJNI_DOWNLOAD_PATH ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar)
38+
39+
if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}")
40+
file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}")
41+
endif()
42+
43+
add_custom_command(
44+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
45+
COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
46+
DEPENDS "${FBJNI_DOWNLOAD_PATH}"
47+
)
48+
49+
add_custom_target(
50+
fbjni_prefab
51+
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
52+
)
53+
54+
add_library(fbjni SHARED IMPORTED)
55+
add_dependencies(fbjni fbjni_prefab)
56+
set_target_properties(fbjni PROPERTIES
57+
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
2758
)
2859

2960
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../lib/cmake/ExecuTorch)
@@ -128,6 +159,7 @@ endif()
128159

129160
target_include_directories(
130161
executorch_jni PRIVATE ${_common_include_directories}
162+
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
131163
)
132164

133165
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})

0 commit comments

Comments
 (0)