Skip to content

Commit 3b85946

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
update tutorial and provide a SDK setup tutorial (#2879)
Summary: Steps to repro app: 1. Follow https://docs-preview.pytorch.org/pytorch/executorch/2817/getting-started-setup.html to set repo and install requirements. 2. If you haven't set up Java/SDK/NDK already, you can follow https://github.com/pytorch/executorch/blob/170956a8d3e7c811bfd788c15676f3a912779510/examples/demo-apps/android/LlamaDemo/SDK-quick-setup-guide.md to set up them. You can also use Android Studio to install them. 3. Follow https://github.com/pytorch/executorch/blob/7ddd415ed9f375908687da910d9dd1feb429f5dc/examples/demo-apps/android/LlamaDemo/README.md to build the app. Pull Request resolved: #2879 Reviewed By: mergennachin Differential Revision: D55820449 Pulled By: kirklandsign fbshipit-source-id: 4a4ac4669cad3ed9ce2659654a93e3b3b506a45b
1 parent 3945100 commit 3b85946

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

examples/demo-apps/android/LlamaDemo/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
This app demonstrates the use of the LLaMA chat app demonstrating local inference use case with ExecuTorch.
44

55
## Prerequisites
6-
* Refer to [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) to set up the repo and dev environment.
7-
* Download and install [Android Studio and SDK](https://developer.android.com/studio).
6+
* Set up your ExecuTorch repo and environment if you haven’t done so by following the [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) to set up the repo and dev environment.
7+
* Install [Java 17 JDK](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html).
8+
* Install the [Android SDK API Level 34](https://developer.android.com/about/versions/14/setup-sdk) and
9+
[Android NDK 25.0.8775105](https://developer.android.com/studio/projects/install-ndk).
10+
* If you have Android Studio set up, you can install them with
11+
* Android Studio Settings -> Language & Frameworks -> Android SDK -> SDK Platforms -> Check the row with API Level 34.
12+
* Android Studio Settings -> Language & Frameworks -> Android SDK -> SDK Tools -> Check NDK (Side by side) row.
13+
* Alternatively, you can follow [this guide](https://github.com/pytorch/executorch/blob/856e085b9344c8b0bf220a97976140a5b76356aa/examples/demo-apps/android/LlamaDemo/SDK.md) to set up Java/SDK/NDK with CLI.
814
* Supported Host OS: CentOS, macOS Sonoma on Apple Silicon.
915

1016
```{note}
11-
This demo app and tutorial has only been validated with arm64-v8a [ABI](https://developer.android.com/ndk/guides/abis), with NDK 25.
17+
This demo app and tutorial has only been validated with arm64-v8a [ABI](https://developer.android.com/ndk/guides/abis), with NDK 25.0.8775105.
1218
```
1319

1420
## Getting models
@@ -28,6 +34,11 @@ The demo app searches in `/data/local/tmp/llama` for .pte and .bin files as LLAM
2834
## Build JNI library
2935
1. Open a terminal window and navigate to the root directory of the `executorch`.
3036
2. Set the following environment variables:
37+
```{note}
38+
<path_to_android_ndk> is the root for the NDK, which is usually under
39+
~/Library/Android/sdk/ndk/XX.Y.ZZZZZ for macOS, and contains NOTICE and README.md.
40+
We use <path_to_android_ndk>/build/cmake/android.toolchain.cmake for CMake to cross-compile.
41+
```
3142
```bash
3243
export ANDROID_NDK=<path_to_android_ndk>
3344
export ANDROID_ABI=arm64-v8a
@@ -40,7 +51,7 @@ popd
4051
```
4152
This is running the shell script [setup.sh](./setup.sh) which configures the required core ExecuTorch, LLAMA2, and Android libraries, builds them, and copy to jniLibs.
4253

43-
## Build Java app
54+
## Build APK
4455
### Alternative 1: Android Studio (Recommended)
4556
1. Open Android Studio and select "Open an existing Android Studio project" to open examples/demo-apps/android/LlamaDemo.
4657
2. Run the app (^R). This builds and launches the app on the phone.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Guide to set up Java/SDK/NDK for Android
2+
3+
Follow this doc if you haven't set up Java/SDK/NDK for Android development
4+
already.
5+
This doc provides a CLI tutorial to set them up. Otherwise, you can do the same
6+
thing with Android Studio GUI.
7+
8+
## Set up Java 17
9+
1. Download the archive from Oracle website.
10+
Make sure you have read and agree with the terms and conditions from the website before downloading.
11+
```bash
12+
export DEV_HOME=<path-to-dev>
13+
cd $DEV_HOME
14+
```
15+
Linux:
16+
```bash
17+
curl https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz -o jdk-17.0.10.tar.gz
18+
```
19+
macOS:
20+
```bash
21+
curl https://download.oracle.com/java/17/archive/jdk-17.0.10_macos-aarch64_bin.tar.gz -o jdk-17.0.10.tar.gz
22+
```
23+
2. Unzip the archive. The directory named `jdk-17.0.10` is the Java root directory.
24+
```bash
25+
tar xf jdk-17.0.10.tar.gz
26+
```
27+
3. Set `JAVA_HOME` and update `PATH`.
28+
29+
Linux:
30+
```bash
31+
export JAVA_HOME="$DEV_HOME"/jdk-17.0.10
32+
export PATH="$JAVA_HOME/bin:$PATH"
33+
```
34+
macOS:
35+
```bash
36+
export JAVA_HOME="$DEV_HOME"/jdk-17.0.10.jdk/Contents/Home
37+
export PATH="$JAVA_HOME/bin:$PATH"
38+
```
39+
40+
Note: Oracle has tutorials for installing Java on
41+
[Linux](https://docs.oracle.com/en/java/javase/17/install/installation-jdk-linux-platforms.html#GUID-4A6BD592-1840-4BB4-A758-4CD49E9EE88B)
42+
and [macOS](https://docs.oracle.com/en/java/javase/17/install/installation-jdk-macos.html#GUID-E8A251B6-D9A9-4276-ABC8-CC0DAD62EA33).
43+
Some Linux distributions has JDK package in package manager. For example, Debian users can install
44+
openjdk-17-jdk package.
45+
46+
## Set up Android SDK/NDK
47+
Android has a command line tool [sdkmanager](https://developer.android.com/tools/sdkmanager) which
48+
helps users managing SDK and other tools related to Android development.
49+
50+
1. Go to https://developer.android.com/studio and download the archive from "Command line tools
51+
only" section. Make sure you have read and agree with the terms and conditions from the website.
52+
53+
Linux:
54+
```bash
55+
curl https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -o commandlinetools.zip
56+
```
57+
macOS:
58+
```bash
59+
curl https://dl.google.com/android/repository/commandlinetools-mac-11076708_latest.zip -o commandlinetools.zip
60+
```
61+
2. Unzip.
62+
```bash
63+
unzip commandlinetools.zip
64+
```
65+
3. Specify a root for Android SDK. For example, we can put it under `$DEV_HOME/sdk`.
66+
67+
```
68+
mkdir -p $DEV_HOME/sdk
69+
export ANDROID_HOME="$(realpath $DEV_HOME/sdk)"
70+
# Install SDK 34
71+
./cmdline-tools/bin/sdkmanager --sdk_root="${ANDROID_HOME}" --install "platforms;android-34"
72+
# Install NDK
73+
./cmdline-tools/bin/sdkmanager --sdk_root="${ANDROID_HOME}" --install "ndk;25.0.8775105"
74+
# The NDK root is then under `ndk/<version>`.
75+
export ANDROID_NDK="$ANDROID_HOME/ndk/25.0.8775105"
76+
```
77+
78+
### (Optional) Android Studio Setup
79+
If you want to use Android Studio and never set up Java/SDK/NDK before, or if
80+
you use the newly installed ones, follow these steps to set Android Studio to use
81+
them.
82+
83+
Copy these output paths to be used by Android Studio
84+
```bash
85+
echo $ANDROID_HOME
86+
echo $ANDROID_NDK
87+
echo $JAVA_HOME
88+
```
89+
90+
Open a project in Android Studio. In Project Structure (File -> Project
91+
Structure, or `⌘;`) -> SDK Location,
92+
* Set Android SDK Location to the path of $ANDROID_HOME
93+
* Set Android NDK Location to the path of $ANDROID_NDK
94+
* Set JDK location (Click Gradle Settings link) -> Gradle JDK -> Add JDK... to the path of $JAVA_HOME

0 commit comments

Comments
 (0)