In this section, you will use a Android demo application to demonstrate local inference with ExecuTorch.
Open a terminal window and navigate to the root directory of the executorch repository.
If you haven’t already, set the following environment variables:
export ANDROID_NDK=$ANDROID_HOME/ndk/29.0.14206865/
export ANDROID_ABI=arm64-v8a
<path_to_android_ndk> is the root for the NDK, which is usually under ~/Library/Android/sdk/ndk/XX.Y.ZZZZZ for macOS, and contains NOTICE and README.md. Make sure you can confirm <path_to_android_ndk>/build/cmake/android.toolchain.cmake is available for CMake to cross-compile.
Run the following command to set up the required JNI library:
sh scripts/build_android_library.sh
Make sure the exported model and tokenizer are copied to the Android phone:
adb shell "ls -la /data/local/tmp/llama/"
adb shell mkdir -p /data/local/tmp/llama
adb push <model.pte> /data/local/tmp/llama/
adb push <tokenizer.bin> /data/local/tmp/llama/
Figure 1. Android Studio Device Explorer
If the files are not on the device, use the device explorer to copy them.
Figure 2. Android Studio upload files using Device Explorer
Before starting, you need to obtain the example app by cloning executorch-examples:
git clone https://github.com/meta-pytorch/executorch-examples.git
This is the recommended option.
Open Android Studio and select Open an existing Android Studio project and navigate to open executorch-examples/llm/android/LlamaDemo.
Run the app (^R). This builds and launches the app on the phone.
Without Android Studio UI, you can run gradle directly to build the app. You need to set up the Android SDK path and invoke gradle. Navigate to the newly cloned executorch-examples repository.
pushd llm/android/LlamaDemo
./gradlew :app:installDebug
popd
You should now see a running app on your phone that looks like this:
