Create an LCE inference binary for Android¶
Important
This guide explains how to build a standalone inference binary that can be executed on an Android device using the Android developer tools. If you'd rather use LCE for inference as part of an Android app, please check out this guide instead.
To build Larq Compute Engine (LCE) for Android, you must have the Android NDK and SDK installed on your system. Below we explain how to install the Android prerequisites in the LCE Docker container and how to configure the LCE Bazel build settings accordingly. Before proceeding with the next steps, please follow these instructions to setup the Docker container and these instructions to setup Bazel.
Note
We recommend using the Docker volume as described here to be able to easily transfer files in-between the container and the host machine.
Install prerequisites¶
We provide a bash script which uses the sdkmanager
tool to install the Android NDK and SDK inside the Docker container. Please run the script by executing the following command from the LCE root directory:
./third_party/install_android.sh
After executing the bash script, please accept the Android SDK licence agreement. The script will download and unpack the Android NDK and SKD under the directory /tmp/lce_android
in the LCE Docker container.
Custom Android version¶
The Android NDK and SDK versions used in LCE are currently hard-coded in the install script. To build LCE against a different NDK and SDK versions, you can manually modify ANDROID_VERSION
and ANDROID_NDK_VERSION
variables in the install script. Additionally, the following configurations in the .bazelrc
file need to be adjusted:
build --action_env ANDROID_NDK_HOME="/tmp/lce_android/ndk/18.1.5063045"
build --action_env ANDROID_NDK_API_LEVEL="21"
build --action_env ANDROID_BUILD_TOOLS_VERSION="28.0.3"
build --action_env ANDROID_SDK_API_LEVEL="29"
build --action_env ANDROID_SDK_HOME="/tmp/lce_android"
Build an LCE inference or benchmark binary¶
To build an LCE inference binary for Android (see here for creating your own LCE binary) the Bazel target needs to built with --config=android_arm64
flag. For example, to build the minimal example for Android, run the following command from the LCE root directory:
bazel build -c opt --config=android_arm64 //examples:lce_minimal
To build the LCE benchmark tool for Android, simply build the bazel target //larq_compute_engine/tflite/benchmark:lce_benchmark_model
.
The resulting binaries will be stored at bazel-bin/larq_compute_engine/tflite/benchmark/lce_benchmark_model
and bazel-bin/examples/lce_minimal
. The bazel-bin
directory inside the lce-volume
is a soft link to the build artifacts directory outside the lce-volume
. As a result, in order to be able to access the inference binaries on the host machine, you need to manually copy them from the bazel-bin
to the lce-volume
directory. To do so, run the following command from the LCE root directory:
cp bazel-bin/larq_compute_engine/tflite/benchmark/lce_benchmark_model .