You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _mobile/android.md
+23-29Lines changed: 23 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,7 +203,7 @@ After that, the code processes the output, finding classes with the highest scor
203
203
204
204
## Building PyTorch Android from Source
205
205
206
-
In some cases you might want to use a local build of pytorch android, for example you may build custom libtorch binary with another set of operators or to make local changes.
206
+
In some cases you might want to use a local build of PyTorch android, for example you may build custom libtorch binary with another set of operators or to make local changes, or try out the latest PyTorch code.
207
207
208
208
For this you can use `./scripts/build_pytorch_android.sh` script.
209
209
```
@@ -218,11 +218,11 @@ The workflow contains several steps:
218
218
219
219
2\. Create symbolic links to the results of those builds:
220
220
`android/pytorch_android/src/main/jniLibs/${abi}` to the directory with output libraries
221
-
`android/pytorch_android/src/main/cpp/libtorch_include/${abi}` to the directory with headers. These directories are used to build `libpytorch.so` library that will be loaded on android device.
221
+
`android/pytorch_android/src/main/cpp/libtorch_include/${abi}` to the directory with headers. These directories are used to build `libpytorch_jni.so` library, as part of the `pytorch_android-release.aar` bundle, that will be loaded on android device.
222
222
223
223
3\. And finally run `gradle` in `android/pytorch_android` directory with task `assembleRelease`
224
224
225
-
Script requires that Android SDK, Android NDK and gradle are installed.
225
+
Script requires that Android SDK, Android NDK, Java SDK, and gradle are installed.
226
226
They are specified as environment variables:
227
227
228
228
`ANDROID_HOME` - path to [Android SDK](https://developer.android.com/studio/command-line/sdkmanager.html)
@@ -231,17 +231,20 @@ They are specified as environment variables:
231
231
232
232
`GRADLE_HOME` - path to [gradle](https://gradle.org/releases/)
233
233
234
+
`JAVA_HOME` - path to [JAVA JDK](https://www.oracle.com/java/technologies/javase-downloads.html#javasejdk)
234
235
235
-
After successful build you should see the result as aar file:
236
+
237
+
After successful build, you should see the result as aar file:
It can be used directly in android projects, as a gradle dependency:
245
+
## Using the PyTorch Android Libraries Built from Source or Nightly
246
+
247
+
First add the two aar files built above, or downloaded from the nightly built PyTorch Android repos at [here](https://oss.sonatype.org/#nexus-search;quick~pytorch_android) and [here](https://oss.sonatype.org/#nexus-search;quick~torchvision_android), to the Android project's `lib` folder, then add in the project's `build.gradle` file:
At the moment for the case of using aar files directly we need additional configuration due to packaging specific (`libfbjni.so` is packaged in both `pytorch_android_fbjni.aar` and `pytorch_android.aar`).
Also we have to add all transitive dependencies of our aars. As `pytorch_android` depends on `com.android.support:appcompat-v7:28.0.0`and `com.facebook.soloader:nativeloader:0.8.0`, we need to add them. (In case of using maven dependencies they are added automatically from `pom.xml`).
273
+
Also we have to add all transitive dependencies of our aars. As `pytorch_android` depends on `com.android.support:appcompat-v7:28.0.0`or `androidx.appcompat:appcompat:1.2.0`, we need to one of them. (In case of using maven dependencies they are added automatically from `pom.xml`).
Open terminal and navigate to the PyTorch root directory. Run the following command
165
+
Open terminal and navigate to the PyTorch root directory. Run the following command (if you already build LibTorch for iOS devices (see below), run `rm -rf build_ios` first):
@@ -171,7 +171,7 @@ After the build succeeds, all static libraries and header files will be generate
171
171
172
172
### Build LibTorch for arm64 Devices
173
173
174
-
Open terminal and navigate to the PyTorch root directory. Run the following command
174
+
Open terminal and navigate to the PyTorch root directory. Run the following command (if you already build LibTorch for iOS simulators, run `rm -rf build_ios` first):
@@ -180,14 +180,25 @@ After the build succeeds, all static libraries and header files will be generate
180
180
181
181
### XCode Setup
182
182
183
-
Open your project in XCode, copy all the static libraries as well as header files to your project. Navigate to the project settings, set the value **Header Search Paths** to the path of header files you just copied.
183
+
Open your project in XCode, go to your project Target's `Build Phases` - `Link Binaries With Libraries`, click the + sign and add all the library files located in `build_ios/install/lib`. Navigate to the project `Build Settings`, set the value **Header Search Paths** to `build_ios/install/include` and **Library Search Paths** to `build_ios/install/lib`.
184
184
185
185
In the build settings, search for **other linker flags**. Add a custom linker flag below
186
186
187
187
```
188
188
-all_load
189
189
```
190
-
Finally, disable bitcode for your target by selecting the Build Settings, searching for **Enable Bitcode**, and set the value to **No**.
190
+
191
+
To use the custom built libraries the project, replace `#import <LibTorch/LibTorch.h>` (in `TorchModule.mm`) which is needed when using LibTorch via Cocoapods with the code below:
192
+
```
193
+
#include "ATen/ATen.h"
194
+
#include "caffe2/core/timer.h"
195
+
#include "caffe2/utils/string_utils.h"
196
+
#include "torch/csrc/autograd/grad_mode.h"
197
+
#include "torch/csrc/jit/serialization/import.h"
198
+
#include "torch/script.h"
199
+
```
200
+
201
+
Finally, disable bitcode for your target by selecting the Build Settings, searching for **Enable Bitcode**, and set the value to **No**.
0 commit comments