Skip to content

Instantly share code, notes, and snippets.

@JnCrMx
Last active August 10, 2022 15:24
Show Gist options
  • Save JnCrMx/144f3169bde1da6856815b3419122ab0 to your computer and use it in GitHub Desktop.
Save JnCrMx/144f3169bde1da6856815b3419122ab0 to your computer and use it in GitHub Desktop.
Build tensorflow-lite Java libraries for desktop rather than for Android

Required software:

  • Bazel version 3.0.0
  • Android SDK with NDK installed

First of all you need to clone tensorflow's repository from GitHub using git clone:

git clone https://github.com/tensorflow/tensorflow.git

Then cd into the directory you just cloned the repository to:

cd tensorflow/

Next, execute ./configure, answer yes when prompted Would you like to interactively configure ./WORKSPACE for Android builds? and enter to correct values for using your Android SDK and NDK.

Using Android SDK seems to be contradictory to the goal, but it is required to build the JAR, which we can do like this:

bazel build //tensorflow/lite/java:libtensorflowlite.jar

You can now find the built library at bazel-bin/tensorflow/lite/java/libtensorflowlite.jar.

Now, you need to build the JNI native libraries:

For that you first need to disable Android by executing ./configure again and answering no to Would you like to interactively configure ./WORKSPACE for Android builds?

To build the native library for the host system execute the following bazel command:

bazel build //tensorflow/lite/java:libtensorflowlite_jni.so

If you instead want to build it for ARM (works e.g. on Raspberry Pi) the command gets a tiny bit more complicated:

bazel build --config elinux_armhf //tensorflow/lite/java:libtensorflowlite_jni.so

Building for ARM64 also seems to be possible:

bazel build --config elinux_aarch64 //tensorflow/lite/java:libtensorflowlite_jni.so

After the build completed you can find the shared object at bazel-bin/tensorflow/lite/java/libtensorflowlite_jni.so.

To use this in your Java project add libtensorflowlite.jar to your classpath and load libtensorflowlite_jni.so using System.load(String) ( https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#load(java.lang.String) ) before using any of the library's functions.

@minoskt
Copy link

minoskt commented Nov 15, 2021

Hi! That's super useful. I successfully compiled it but cannot use the produced jar. While it compiles successfully, whenever I run it I see a java.lang.NoClassDefFoundError exception.

Any ideas?
Thank you for your help in advance.

@JnCrMx
Copy link
Author

JnCrMx commented Nov 24, 2021

Sadly, the newer version of TensorFlow seems to have a quite different Java API that the instructions won't work for. I will try to have a look and update them, but I currently don't have too much time.

For now you could try to use an older version of TensorFlow. Version 2.2.0 or something like that might work.

@AngryDuckFTW
Copy link

trying to build the JNI libs for arm64 on version 2.2.0 and I get:

ERROR: Config value elinux_aarch64 is not defined in any .rc file

any ideas why it wont recognise the config value for arm64?

Building on M1 Mac

@JnCrMx
Copy link
Author

JnCrMx commented Aug 10, 2022

No idea why this is happening. You could try building in a Docker image or VM.
Alternatively, I have an arm64 lib lying around:
https://files.jcm.re/libtensorflowlite_jni.so and https://files.jcm.re/libtensorflowlite.jar

I can try to create a Docker image for the build, but I don't have much time (and not enough space :P) for that right now, so that would take quite a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment