Search code examples
androidc++tensorflowandroid-ndkbazel

Build tflite c++ lib for android with ndk 25 > with bazel


I'm building a huge project that requires ndk versions above 25 (I'm using 25.0.8775105 right now). And I would like to use TFLite on my project. Following the docs, to cross-compile it on my x86-64 machine to be suitable for arm64-v8a architectures the instructions goes as follows:

  1. Get tensorflow source
  2. Install bazel (I've tried both 6.0.0 and the 7.0.0-prebuild)
  3. Run ./configure and say y to Would you like to interactively configure ./WORKSPACE for Android builds?, then configure it.
  4. Run bazel build -c opt --config=android_arm64 //tensorflow/lite:libtensorflowlite.so
  5. Extract the .so file and get the header files than use like any other library.

When I run ./configure and say yes to configuring ./WORKSPACE, it wants me to provide an ndk directory. However, when I provide it (it being /home/tb/Android/Sdk/ndk/25.0.8775105 on my system, very similar to what's recommended) I get hit with this: WARNING: The NDK version in /home/tb/Android/Sdk/ndk/25.0.8775105 is 25, which is not supported by Bazel (officially supported versions: [19, 20, 21]). Please use another version. Compiling Android targets may result in confusing errors. followed by an error caused by this:

Traceback (most recent call last):
  File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 1363, in <module>
    main()
  File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 1336, in main
    create_android_ndk_rule(environ_cp)
  File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 653, in create_android_ndk_rule
    get_ndk_api_level(environ_cp, android_ndk_home_path))
  File "/home/tb/Desktop/tensorflow/tensorflow_src/./configure.py", line 747, in get_ndk_api_level
    api_levels = sorted(os.listdir(platforms))
FileNotFoundError: [Errno 2] No such file or directory: '/home/tb/Android/Sdk/ndk/25.0.8775105/platforms'

I've tried compiling it with bazel build -c opt //tensorflow/lite:libtensorflowlite.so --fat_apk_cpu=arm64-v8a which did in fact built the .so file, but when I try to build the code it says C/C++: ld: error: undefined symbol: tflite::impl::Interpreter::Invoke(), I did run my project on linux and it works fine, android uses a similar if not the same cmake file so I think the .so is broken.

What should I do? Does anyone have a link to, preferably official, pre-built tflite aarch64 / arm64-v8a .so file for c++?

Update: I've opened an issue detailing everything I've tried up to now.


Solution

  • To compile and use on my NDK 25 project, I've downloaded NDK 20, gave that as a path when I ran ./configure and used ndk 20 path when I configured WORKSPACE. It compiled and using the include files from the same source I managed to run it. For anyone struggling you might want to switch to a different branch. I switched between branches a bit to finally find one that works (mainly the main branch or the vx.x or rx.x branches.).