1

I need to use the onnxruntime library in an Android project, but I can't understand how to configure CMake to be able to use C++ headers and *.so from AAR. I created a new Android Native Library module and put onnxruntime-mobile-1.11.0.aar into libs but wen I do #include "onnxruntime_cxx_api.h" in my C++ code I encounter with an error:

In file included from /Users/ash/AndroidStudioProjects/cv-demo/android/src/nativelib/src/main/cpp/OrtInferenceSession.cpp:8: /Users/ash/AndroidStudioProjects/cv-demo/android/src/nativelib/src/main/cpp/OrtInferenceSession.hpp:20:10: fatal error: 'onnxruntime_cxx_api.h' file not found

I see that the files I need are packed in AAR:

enter image description here

How to specify paths to headers and *.so inside onnxruntime-mobile-1.11.0.aar in my CMakeLists.txt to use them in my C++ code?

codeling
  • 11,056
  • 4
  • 42
  • 71

1 Answers1

1

I've extracted the libonnxruntime.so (no need for the jni stuff) and the headers and put them in a new libs/ folder next to src/. Hence :

libs
└── onnxruntime
    ├── include
    │   ├── cpu_provider_factory.h
    │   ├── nnapi_provider_factory.h
    │   ├── onnxruntime_c_api.h
    │   ├── onnxruntime_cxx_api.h
    │   └── onnxruntime_cxx_inline.h
    └── lib
        ├── arm64-v8a
        │   └── libonnxruntime.so
        ├── armeabi-v7a
        │   └── libonnxruntime.so
        ├── x86
        │   └── libonnxruntime.so
        └── x86_64
            └── libonnxruntime.so

Then, I've added this library in CMakeList.txt to link the library:

set(LIBS_DIR ${CMAKE_SOURCE_DIR}/../../../libs)

add_library(onnxruntime SHARED IMPORTED)
set_target_properties(
        onnxruntime
        PROPERTIES
        IMPORTED_LOCATION ${LIBS_DIR}/onnxruntime/lib/${ANDROID_ABI}/libonnxruntime.so
        INTERFACE_INCLUDE_DIRECTORIES ${LIBS_DIR}/onnxruntime/include
)

And finally, with a gradle plugin version greater than 4.0, you have to add these line in the android section of build.gradle:

packagingOptions {
    jniLibs {
        pickFirsts += ['**']
    }
}

The official documentation states

If you are using Android Gradle Plugin 4.0, move any libraries that are used by IMPORTED CMake targets out of your jniLibs directory to avoid this error.

But it was not sufficient in my case, also have to add the gradle configuration to avoid the infamous 2 files found with path 'lib/arm64-v8a/libonnxruntime.so' from inputs: error.

NiziL
  • 5,068
  • 23
  • 33