Skip to content

Commit 73de356

Browse files
authored
Merge pull request #2250 from amalaugustinejose/build-android-chat-app-using-onnxruntime
Update to ALP "Build and run an Android chat app"
2 parents 5f8fee9 + 2c65e94 commit 73de356

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

content/learning-paths/mobile-graphics-and-gaming/build-android-chat-app-using-onnxruntime/1-dev-env-setup.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ In this learning path, you will learn how to build and deploy a simple LLM-based
1313
Your first task is to prepare a development environment with the required software:
1414

1515
- Android Studio (latest version recommended)
16-
- Android NDK (tested with version 27.0.12077973)
17-
- Python 3.11
18-
- CMake (tested with version 3.28.1)
19-
- Ninja (tested with version 1.11.1)
16+
- Android NDK (tested with version 27.3.13750724)
17+
- Python 3.13
18+
- CMake (tested with version 4.1.0)
19+
- Ninja (tested with version 1.12.1)
2020

2121
The following instructions were tested on an x86 Windows machine with at least 16GB of RAM.
2222

@@ -34,9 +34,9 @@ Follow these steps to install and configure Android Studio:
3434

3535
5. Click **OK** and **Apply**.
3636

37-
## Install Python 3.11
37+
## Install Python 3.15
3838

39-
Download and install [Python version 3.11](https://www.python.org/downloads/release/python-3110/)
39+
Download and install [Python version 3.13](https://www.python.org/downloads/release/python-3135/)
4040

4141
## Install CMake
4242

@@ -45,7 +45,7 @@ CMake is an open-source tool that automates the build process for software proje
4545
[Download and install CMake](https://cmake.org/download/)
4646

4747
{{% notice Note %}}
48-
The instructions were tested with version 3.28.1
48+
The instructions were tested with version 4.1.0
4949
{{% /notice %}}
5050

5151
## Install Ninja
@@ -55,7 +55,7 @@ Ninja is a minimalistic build system designed to efficiently handle incremental
5555
[Download and install Ninja]( https://github.com/ninja-build/ninja/releases)
5656

5757
{{% notice Note %}}
58-
The instructions were tested with version 1.11.1
58+
The instructions were tested with version 1.12.1
5959
{{% /notice %}}
6060

6161
You now have the required development tools installed to follow this learning path.

content/learning-paths/mobile-graphics-and-gaming/build-android-chat-app-using-onnxruntime/2-build-onnxruntime.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@ Open up a Windows PowerShell and checkout the source tree:
2121
cd C:\Users\$env:USERNAME
2222
git clone --recursive https://github.com/Microsoft/onnxruntime.git
2323
cd onnxruntime
24-
git checkout 9b37b3ea4467b3aab9110e0d259d0cf27478697d
24+
git checkout 5630b081cd25e4eccc7516a652ff956e51676794
2525
```
2626

2727
{{% notice Note %}}
28-
You might be able to use a later commit. These steps have been tested with the commit `9b37b3ea4467b3aab9110e0d259d0cf27478697d`.
28+
You might be able to use a later commit. These steps have been tested with the commit `5630b081cd25e4eccc7516a652ff956e51676794`. This corresponds to ORT 1.22.2
2929
{{% /notice %}}
3030

3131
### Build for Android CPU
3232

33-
You use the Ninja generator to build on Windows for Android. First, set JAVA_HOME to the path to your JDK install. You can point to the JDK from Android Studio, or a standalone JDK install.
33+
You use the Ninja generator to build on Windows for Android. First, set JAVA_HOME to the path to your JDK install. ONNX Runtime compiles well with JDK 17. If you face issuses with compilation please check your JDK version.
3434

3535
```bash
36-
$env:JAVA_HOME="C:\Program Files\Android\Android Studio\jbr"
36+
$env:JAVA_HOME="C:\Program Files\Microsoft\jdk-17.0.16.8-hotspot\"
3737
```
3838
3939
Now run the following command:
4040
4141
```bash
4242
43-
./build.bat --config Release --build_shared_lib --android --android_sdk_path C:\Users\$env:USERNAME\AppData\Local\Android\Sdk --android_ndk_path C:\Users\$env:USERNAME\AppData\Local\Android\Sdk\ndk\27.0.12077973 --android_abi arm64-v8a --android_api 27 --cmake_generator Ninja --build_java
43+
./build.bat --config Release --build_shared_lib --android --android_sdk_path C:\Users\$env:USERNAME\AppData\Local\Android\Sdk --android_ndk_path C:\Users\$env:USERNAME\AppData\Local\Android\Sdk\ndk\27.3.13750724 --android_abi arm64-v8a --android_api 27 --cmake_generator Ninja --build_java
4444
4545
```
4646
@@ -49,7 +49,7 @@ An Android Archive (AAR) file, which can be imported directly in Android Studio,
4949
When the build is complete, confirm the shared library and the AAR file have been created:
5050
5151
```
52-
ls build\Windows\Release\onnxruntime.so
52+
ls build\Windows\Release\libonnxruntime.so
5353
ls build\Windows\Release\java\build\android\outputs\aar\onnxruntime-release.aar
5454
```
5555

content/learning-paths/mobile-graphics-and-gaming/build-android-chat-app-using-onnxruntime/3-build-onnxruntime-generate-api.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ Within your Windows PowerShell prompt, checkout the source repo:
2424
C:\Users\$env:USERNAME
2525
git clone https://github.com/microsoft/onnxruntime-genai
2626
cd onnxruntime-genai
27-
git checkout 1e4d289502a61265c3b07efb17d8796225bb0b7f
27+
git checkout 5ba9fce5b52452a82b12ac343d941765c430d996
2828
```
2929

3030
{{% notice Note %}}
31-
You might be able to use later commits. These steps have been tested with the commit `1e4d289502a61265c3b07efb17d8796225bb0b7f`.
31+
You might be able to use later commits. These steps have been tested with the commit `5ba9fce5b52452a82b12ac343d941765c430d996`. This corresponds to ORT Gen API 0.9.0
3232
{{% /notice %}}
3333

3434
### Build for Android CPU
@@ -37,11 +37,12 @@ Ninja generator is used to build on Windows for Android. Make sure you have set
3737

3838
```bash
3939
python -m pip install requests
40-
python3.11 build.py --build_java --android --android_home C:\Users\$env:USERNAME\AppData\Local\Android\Sdk --android_ndk_path C:\Users\$env:USERNAME\AppData\Local\Android\Sdk\ndk\27.0.12077973 --android_abi arm64-v8a --config Release
40+
python3.13 build.py --skip_wheel --build_java --android --android_home C:\Users\$env:USERNAME\AppData\Local\Android\Sdk --android_ndk_path C:\Users\$env:USERNAME\AppData\Local\Android\Sdk\ndk\27.3.13750724 --android_abi arm64-v8a --config Release
4141
```
4242

4343
When the build is complete, confirm the shared library has been created:
4444

4545
```output
46-
ls build\Android\Release\onnxruntime-genai.so
46+
ls build\Android\Release\libonnxruntime-genai.so
47+
ls build\Android\Release\src\java\build\android\outputs\aar\onnxruntime-genai-release.aar
4748
```

content/learning-paths/mobile-graphics-and-gaming/build-android-chat-app-using-onnxruntime/4-run-benchmark-on-android.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ cd onnxruntime-genai
1919
copy src\ort_genai.h examples\c\include\
2020
copy src\ort_genai_c.h examples\c\include\
2121
cd examples\c
22-
mkdir build
23-
cd build
2422
```
2523
Run the `cmake` command as shown:
2624

2725
```bash
28-
cmake -DCMAKE_TOOLCHAIN_FILE=C:\Users\$env:USERNAME\AppData\Local\Android\Sdk\ndk\27.0.12077973\build\cmake\android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-27 -DCMAKE_BUILD_TYPE=Release -G "Ninja" ..
29-
ninja
26+
cmake -G "Ninja" -S . -B build -DMODEL_QA=ON -DCMAKE_TOOLCHAIN_FILE=C:\Users\$env:USERNAME\AppData\Local\Android\Sdk\ndk\27.3.13750724\build\cmake\android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-27 -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXE_LINKER_FLAGS="-L../lib"
27+
cmake --build build --parallel --config Release
28+
cd build
3029
```
3130

32-
After successful build, a binary program called `phi3` will be created.
31+
After successful build, a binary program called `model_qa` will be created.
3332

3433
### Prepare Phi-3-mini model
3534

@@ -63,7 +62,7 @@ You should see your device listed to confirm it is connected.
6362

6463
``` bash
6564
adb push cpu-int4-rtn-block-32-acc-level-4 /data/local/tmp
66-
adb push .\phi3 /data/local/tmp
65+
adb push .\model_qa /data/local/tmp
6766
adb push onnxruntime-genai\build\Android\Release\libonnxruntime-genai.so /data/local/tmp
6867
adb push onnxruntime\build\Windows\Release\libonnxruntime.so /data/local/tmp
6968
```
@@ -75,9 +74,9 @@ Use the runner to execute the model on the phone with the `adb` command:
7574
``` bash
7675
adb shell
7776
cd /data/local/tmp
78-
chmod 777 phi3
77+
chmod 777 model_qa
7978
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp
80-
./phi3 cpu-int4-rtn-block-32-acc-level-4
79+
./model_qa cpu-int4-rtn-block-32-acc-level-4
8180
```
8281

8382
This will allow the runner program to load the model. It will then prompt you to input the text prompt to the model. After you enter your input prompt, the text output by the model will be displayed. On completion, performance metrics similar to those shown below should be displayed:

content/learning-paths/mobile-graphics-and-gaming/build-android-chat-app-using-onnxruntime/5-build-android-chat-app.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ You can use the Android demo application included in the [onnxruntime-inference-
1616
``` bash
1717
git clone https://github.com/microsoft/onnxruntime-inference-examples
1818
cd onnxruntime-inference-examples
19-
git checkout 009920df0136d7dfa53944d06af01002fb63e2f5
19+
git checkout 7a635daae48450ff142e5c0848a564b245f04112
2020
```
2121

2222
{{% notice Note %}}
23-
You could probably use a later commit but these steps have been tested with the commit `009920df0136d7dfa53944d06af01002fb63e2f5`.
23+
You could probably use a later commit but these steps have been tested with the commit `7a635daae48450ff142e5c0848a564b245f04112`.
2424
{{% /notice %}}
2525

2626
### Build the app using Android Studio
@@ -29,10 +29,11 @@ Open the `mobile\examples\phi-3\android` directory with Android Studio.
2929

3030
#### (Optional) In case you want to use the ONNX Runtime AAR you built
3131

32-
Copy ONNX Runtime AAR you built earlier in this learning path:
32+
Copy ONNX Runtime AAR and ONNX Runtime GenAI AAR you built earlier in this learning path:
3333

3434
```bash
35-
Copy onnxruntime\build\Windows\Release\java\build\android\outputs\aar\onnxruntime-release.aar mobile\examples\phi-3\android\app\libs
35+
Copy onnxruntime\build\Windows\Release\java\build\android\outputs\aar\onnxruntime-release.aar onnxruntime-inference-examples\mobile\examples\phi-3\android\app\libs\
36+
Copy onnxruntime-genai\build\Android\Release\src\java\build\android\outputs\aar\onnxruntime-genai-release.aar onnxruntime-inference-examples\mobile\examples\phi-3\android\app\libs\
3637
```
3738

3839
Update `build.gradle.kts (:app)` as below:
@@ -41,6 +42,8 @@ Update `build.gradle.kts (:app)` as below:
4142
// ONNX Runtime with GenAI
4243
//implementation("com.microsoft.onnxruntime:onnxruntime-android:latest.release")
4344
implementation(files("libs/onnxruntime-release.aar"))
45+
//implementation(files("libs/onnxruntime-genai-android-0.8.1.aar"))
46+
implementation(files("libs/onnxruntime-genai-release.aar"))
4447
```
4548

4649
Finally, click **File > Sync Project with Gradle**

0 commit comments

Comments
 (0)