diff --git a/.gitmodules b/.gitmodules index 71ff854bb03..844cd917892 100644 --- a/.gitmodules +++ b/.gitmodules @@ -28,9 +28,6 @@ [submodule "backends/xnnpack/third-party/pthreadpool"] path = backends/xnnpack/third-party/pthreadpool url = https://github.com/Maratyszcza/pthreadpool.git -[submodule "examples/third-party/fbjni"] - path = examples/third-party/fbjni - url = https://github.com/facebookincubator/fbjni.git [submodule "extension/llm/third-party/abseil-cpp"] path = extension/llm/third-party/abseil-cpp url = https://github.com/abseil/abseil-cpp.git diff --git a/examples/third-party/fbjni b/examples/third-party/fbjni deleted file mode 160000 index 52a14f0daa8..00000000000 --- a/examples/third-party/fbjni +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 52a14f0daa889a20d8984798b8d96eb03cebd334 diff --git a/extension/android/CMakeLists.txt b/extension/android/CMakeLists.txt index 075e4f21a40..cc080716111 100644 --- a/extension/android/CMakeLists.txt +++ b/extension/android/CMakeLists.txt @@ -21,9 +21,40 @@ include(${EXECUTORCH_ROOT}/build/Utils.cmake) set(_common_compile_options -Wno-deprecated-declarations -fPIC) set(_common_include_directories ${EXECUTORCH_ROOT}/..) -add_subdirectory( - ${EXECUTORCH_ROOT}/examples/third-party/fbjni - ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni +# We need to download fbjni library from maven, and use its "prefab" library +# and headers, and link executorch library against that fbjni library. +# We don't know which NDK is used to compile fbjni, and we need to link our +# executorch library to the version which Android APK links against for runtime +# to ensure the libc++ dependencies are consistent. +# WARNING # +# Users need to use the SAME fbjni version here and in app gradle dependency +# for runtime compatibility! +if(NOT FBJNI_VERSION) + set(FBJNI_VERSION 0.5.1) +endif() + +set(FBJNI_AAR_URL https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar) +set(FBJNI_DOWNLOAD_PATH ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar) + +if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}") + file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}") +endif() + +add_custom_command( + 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" + COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni + DEPENDS "${FBJNI_DOWNLOAD_PATH}" +) + +add_custom_target( + fbjni_prefab + 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" +) + +add_library(fbjni SHARED IMPORTED) +add_dependencies(fbjni fbjni_prefab) +set_target_properties(fbjni PROPERTIES + IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so" ) set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../lib/cmake/ExecuTorch) @@ -128,6 +159,7 @@ endif() target_include_directories( executorch_jni PRIVATE ${_common_include_directories} + "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" ) target_compile_options(executorch_jni PUBLIC ${_common_compile_options})