Executorch as a native c++ lib in Android

Hi,
I’m trying to build an application that uses executorch with oboe in Android. As oboe is a native c++ lib in android, it makes sense to use the c++ version of executorch, do all the processing in c++ and then have a common jni layer that exposes the necessary handles.

Its just that cmake and linking is tricky :blush:

I’ve tried the following:

  1. cross-compiling executorch on a linux system following step one from the android guide
  2. create a new android project with native lib support (in macos with intel cpu – hence the need for a linux compile due to torch versions)
  3. copy cmake-android-out into src/main/executorch in the android project
  4. copy Utils.cmake to src/main/executorch/build
  5. modify CMakeList.txt in src/main/cpp to contain the following:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("test-project")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED
        # List C/C++ source files with relative paths to this CMakeLists.txt.
        native-lib.cpp
        jni_bridge.cpp)

find_package (oboe REQUIRED CONFIG)

set(EXECUTORCH_ROOT "${CMAKE_CURRENT_LIST_DIR}/../..")
set(_common_include_directories ${EXECUTORCH_ROOT}/executorch)

include(${EXECUTORCH_ROOT}/executorch/build/Utils.cmake)
set( executorch_DIR "${EXECUTORCH_ROOT}/executorch/lib/cmake/ExecuTorch/")
find_package (executorch REQUIRED)
include_directories( "${EXECUTORCH_ROOT}/executorch/include" )
target_link_options_shared_lib(executorch)

set(link_libraries)
list(APPEND link_libraries extension_data_loader extension_module executorch)
list(APPEND link_libraries portable_ops_lib portable_kernels)
target_link_options_shared_lib(portable_ops_lib)
target_link_options_shared_lib(xnnpack_backend)

list(APPEND link_libraries xnnpack_backend XNNPACK pthreadpool cpuinfo)
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${_common_include_directories}
)

#add_library(executorch_no_prim_ops STATIC IMPORTED)
#set_target_properties(executorch_no_prim_ops PROPERTIES IMPORTED_LOCATION "${_root}/executorch/libexecutorch_no_prim_ops.a")

# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
        # List libraries link to the target library
        android
        oboe::oboe
        executorch
        log)

Some of the steps are copied from executorch/extension/android/CMakeLists.txt at v0.3.0 · pytorch/executorch · GitHub.

With this process, I can build the android app, but it seems that I don’t have access to all the headers - trying to #include <executorch/extension/module/module.h> in a header file results in all kinds of errors when building the app.

Any thoughts about how I should go about using executorch as a c++ lib in android?

Hi,

Maybe try replace

include_directories( "${EXECUTORCH_ROOT}/executorch/include" )

with

include_directories( "${EXECUTORCH_ROOT}/.." )

Does it help?

Also feel free to open an issue in Issues · pytorch/executorch · GitHub

Hi, it seems like you need to do:

include_directories( "{EXECUTORCH_ROOT}/../)

This is a bit hacky but it enables you to do #include <executorch/...>