xref: /aosp_15_r20/external/pytorch/scripts/build_pytorch_android.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #!/bin/bash
2 set -eux
3 
4 ##############################################################################
5 # Master script to build PyTorch Android library with Java bindings.
6 ##############################################################################
7 # Example usage:
8 # - Build default AARs:
9 #   scripts/build_pytorch_android.sh
10 #
11 # - Build for specific ABI(s):
12 #   scripts/build_pytorch_android.sh armeabi-v7a
13 #   scripts/build_pytorch_android.sh arm64-v8a,x86,x86_64
14 #
15 # Script's workflow:
16 # 1. Builds libtorch for android for specified android abisi (by default for all 4).
17 # Custom list of android abis can be specified as a bash argument as comma separated list.
18 # For example just for testing on android x86 emulator we need only x86 build.
19 # ./scripts/build_pytorch_android.sh x86
20 # 2. Creates symbolic links to android/pytorch_android/src/main/jniLibs/${abi} for libtorch build output,
21 # android/pytorch_android/src/main/cpp/libtorch_include/${abi} for headers.
22 # 3. Runs pyotrch_android gradle build:
23 # gradle assembleRelease
24 
25 PYTORCH_DIR="$(cd $(dirname $0)/..; pwd -P)"
26 PYTORCH_ANDROID_DIR=$PYTORCH_DIR/android
27 
28 echo "PYTORCH_DIR:$PYTORCH_DIR"
29 
30 source "$PYTORCH_ANDROID_DIR/common.sh"
31 
32 check_android_sdk
33 check_gradle
34 parse_abis_list "$@"
35 build_android
36 
37 # To set proxy for gradle add following lines to ./gradle/gradle.properties:
38 # systemProp.http.proxyHost=...
39 # systemProp.http.proxyPort=8080
40 # systemProp.https.proxyHost=...
41 # systemProp.https.proxyPort=8080
42 
43 if [ "$CUSTOM_ABIS_LIST" = true ]; then
44   # Skipping clean task here as android gradle plugin 3.3.2 exteralNativeBuild has problems
45   # with it when abiFilters are specified.
46   $GRADLE_PATH -PABI_FILTERS=$ABIS_LIST -p $PYTORCH_ANDROID_DIR assembleRelease
47 else
48   $GRADLE_PATH -p $PYTORCH_ANDROID_DIR clean assembleRelease
49 fi
50 
51 find $PYTORCH_ANDROID_DIR -type f -name *aar | xargs ls -lah
52