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.
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.