Skip to content

Commit 818efd3

Browse files
committed
[Android docs] Add building instructions and code snippet
1 parent b06bd87 commit 818efd3

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

docs/source/using-executorch-android.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,47 @@ 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

@@ -80,6 +118,35 @@ TODO Describe commonly used backends, including XNN, Vulkan, and NPUs.
80118

81119
TODO Code sample in Java
82120

121+
Here is an example code sample in Java that demonstrates how to integrate ExecuTorch into an Android app:
122+
123+
```
124+
import org.pytorch.executorch.EValue;
125+
import org.pytorch.executorch.Module;
126+
import org.pytorch.executorch.Tensor;
127+
128+
public class MainActivity extends Activity {
129+
private Module module;
130+
131+
@Override
132+
protected void onCreate(Bundle savedInstanceState) {
133+
super.onCreate(savedInstanceState);
134+
// Load the ExecuTorch module
135+
module = Module.load("/path/to/module.pte");
136+
}
137+
public void runInference(View view) {
138+
// Prepare input data
139+
Tensor input = Tensor.fromBlob(getInputData());
140+
// Run inference
141+
Tensor output = module.forward(EValue.from(input))[0].toTensor();
142+
// Process output data
143+
processOutput(output);
144+
}
145+
}
146+
```
147+
This example loads an ExecuTorch module, prepares input data, runs inference, and processes the output data.
148+
149+
83150
## Next Steps
84151

85152
TODO Link to Java API reference and other relevant material

0 commit comments

Comments
 (0)