Skip to content

Commit a0924f7

Browse files
authored
[Android docs] Add building instructions and code snippet (#8811)
Add more instructions, code snippet, link to example project
1 parent b06bd87 commit a0924f7

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

docs/source/using-executorch-android.md

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,85 @@ dependencies {
6868

6969
Now you can compile your app with the ExecuTorch Android library.
7070

71-
### Building from Source
71+
## Building from Source
7272

73-
TODO Instructions on re-creating and customizing the Android AAR.
73+
`build/build_android_library.sh` is a helper script to build the Java library (into .jar), native library (into .so), and the packaged AAR file. It can also build
74+
demo apps to showcase the AAR is integrated into a user app correctly.
75+
76+
You need Android [SDK](https://developer.android.com/studio) and [NDK](https://developer.android.com/ndk/downloads) to use it.
77+
78+
Current NDK version used in ExecuTorch CI: r27b.
79+
80+
You need to set `ANDROID_NDK` to the correct NDK root (containing NOTICE file).
81+
82+
```
83+
export ANDROID_NDK=/path/to/ndk
84+
sh build/build_android_library.sh
85+
```
86+
87+
### Optional environment variables
88+
89+
Optionally, set these environment variables before running `build_android_library.sh`.
90+
91+
#### ANDROID_ABIS
92+
Set environment variable `ANDROID_ABIS` to either `arm64-v8a` or `x86_64` if you only need to build the native library for one ABI only.
93+
```
94+
export ANDROID_ABIS=arm64-v8a
95+
# or
96+
# export ANDROID_ABIS=x86_64
97+
sh build/build_android_library.sh
98+
```
99+
100+
#### EXECUTORCH_CMAKE_BUILD_TYPE
101+
Set environment variable `EXECUTORCH_CMAKE_BUILD_TYPE` to `Release` or `Debug` based on your needs.
102+
103+
#### Using MediaTek backend
104+
105+
To use [MediaTek backend](https://pytorch.org/executorch/main/backends-mediatek.html),
106+
after installing and setting up the SDK, set `NEURON_BUFFER_ALLOCATOR_LIB` and `NEURON_USDK_ADAPTER_LIB` to the corresponding path.
107+
108+
#### Using Qualcomm AI Engine Backend
109+
110+
To use [Qualcomm AI Engine Backend](https://pytorch.org/executorch/main/backends-qualcomm.html#qualcomm-ai-engine-backend),
111+
after installing and setting up the SDK, set `QNN_SDK_ROOT` to the corresponding path
74112

75113
## Android Backends
76114

77115
TODO Describe commonly used backends, including XNN, Vulkan, and NPUs.
78116

79117
## Runtime Integration
80118

81-
TODO Code sample in Java
119+
Here is an example code sample in Java that demonstrates how to integrate ExecuTorch into an Android app:
120+
121+
```java
122+
import org.pytorch.executorch.EValue;
123+
import org.pytorch.executorch.Module;
124+
import org.pytorch.executorch.Tensor;
125+
126+
public class MainActivity extends Activity {
127+
private Module module;
128+
129+
@Override
130+
protected void onCreate(Bundle savedInstanceState) {
131+
super.onCreate(savedInstanceState);
132+
// Load the ExecuTorch module
133+
module = Module.load("/path/to/module.pte");
134+
}
135+
public void runInference(View view) {
136+
// Prepare input data
137+
Tensor input = Tensor.fromBlob(getInputData());
138+
// Run inference
139+
Tensor output = module.forward(EValue.from(input))[0].toTensor();
140+
// Process output data
141+
processOutput(output);
142+
}
143+
}
144+
```
145+
This example loads an ExecuTorch module, prepares input data, runs inference, and processes the output data.
146+
147+
Please use [ExecuTorchDemo](https://github.com/pytorch/executorch/tree/main/examples/demo-apps/android/ExecuTorchDemo)
148+
and [LlamaDemo](https://github.com/pytorch/executorch/tree/main/examples/demo-apps/android/LlamaDemo) for the code examples
149+
using ExecuTorch AAR package.
82150

83151
## Next Steps
84152

0 commit comments

Comments
 (0)