| 1 | #!/bin/bash |
| 2 | # Exit immediately if any command fails. This ensures CI properly reports build |
| 3 | # failures instead of continuing to create empty/broken artifacts. |
| 4 | set -e |
| 5 | |
| 6 | # Copyright 2023 LiveKit, Inc. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | |
| 20 | |
| 21 | arch="" |
| 22 | profile="release" |
| 23 | |
| 24 | while [ "$#" -gt 0 ]; do |
| 25 | case "$1" in |
| 26 | --arch) |
| 27 | arch="$2" |
| 28 | if [ "$arch" != "arm" ] && [ "$arch" != "x64" ] && [ "$arch" != "arm64" ]; then |
| 29 | echo "Error: Invalid value for --arch. Must be 'arm', 'x64' or 'arm64'." |
| 30 | exit 1 |
| 31 | fi |
| 32 | shift 2 |
| 33 | ;; |
| 34 | --profile) |
| 35 | profile="$2" |
| 36 | if [ "$profile" != "debug" ] && [ "$profile" != "release" ]; then |
| 37 | echo "Error: Invalid value for --profile. Must be 'debug' or 'release'." |
| 38 | exit 1 |
| 39 | fi |
| 40 | shift 2 |
| 41 | ;; |
| 42 | *) |
| 43 | echo "Error: Unknown argument '$1'" |
| 44 | exit 1 |
| 45 | ;; |
| 46 | esac |
| 47 | done |
| 48 | |
| 49 | if [ -z "$arch" ]; then |
| 50 | echo "Error: --arch must be set." |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | echo "Building LiveKit WebRTC - Android" |
| 55 | echo "Arch: $arch" |
| 56 | echo "Profile: $profile" |
| 57 | |
| 58 | if [ ! -e "$(pwd)/depot_tools" ] |
| 59 | then |
| 60 | git clone --depth 1 https://chromium.googlesource.com/chromium/tools/depot_tools.git |
| 61 | fi |
| 62 | |
| 63 | export COMMAND_DIR=$(cd $(dirname $0); pwd) |
| 64 | export PATH="$(pwd)/depot_tools:$PATH" |
| 65 | export OUTPUT_DIR="$(pwd)/src/out-$arch-$profile" |
| 66 | export ARTIFACTS_DIR="$(pwd)/android-$arch-$profile" |
| 67 | |
| 68 | if [ ! -e "$(pwd)/src" ] |
| 69 | then |
| 70 | gclient sync -D --no-history |
| 71 | fi |
| 72 | |
| 73 | source "$COMMAND_DIR/apply_patch.sh" |
| 74 | |
| 75 | cd src |
| 76 | apply_patch "$COMMAND_DIR/patches/add_licenses.patch" |
| 77 | apply_patch "$COMMAND_DIR/patches/fix_license_json_parsing.patch" |
| 78 | apply_patch "$COMMAND_DIR/patches/ssl_verify_callback_with_native_handle.patch" |
| 79 | apply_patch "$COMMAND_DIR/patches/add_deps.patch" |
| 80 | apply_patch "$COMMAND_DIR/patches/android_use_libunwind.patch" |
| 81 | apply_patch "$COMMAND_DIR/patches/external_audio_source.patch" |
| 82 | # livekit prefixed jni |
| 83 | apply_patch "$COMMAND_DIR/patches/jni_prefix.patch" |
| 84 | |
| 85 | cd third_party/libyuv |
| 86 | apply_patch "$COMMAND_DIR/patches/disable_sme_for_libyuv.patch" |
| 87 | |
| 88 | cd ../../.. |
| 89 | |
| 90 | mkdir -p "$ARTIFACTS_DIR/lib" |
| 91 | |
| 92 | debug="false" |
| 93 | if [ "$profile" = "debug" ]; then |
| 94 | debug="true" |
| 95 | fi |
| 96 | |
| 97 | # Note: use_clang_modules=false is required to avoid C++ module compilation issues. |
| 98 | # Without this flag, the build may fail partway through, resulting in missing |
| 99 | # artifacts like libwebrtc.jar. |
| 100 | args="is_debug=$debug \ |
| 101 | is_java_debug=$debug \ |
| 102 | target_os=\"android\" \ |
| 103 | target_cpu=\"$arch\" \ |
| 104 | rtc_enable_protobuf=false \ |
| 105 | treat_warnings_as_errors=false \ |
| 106 | rtc_include_tests=false \ |
| 107 | rtc_build_tools=false \ |
| 108 | rtc_build_examples=false \ |
| 109 | rtc_libvpx_build_vp9=false \ |
| 110 | is_component_build=false \ |
| 111 | enable_stripping=true \ |
| 112 | rtc_use_h264=false \ |
| 113 | rtc_use_h265=true \ |
| 114 | rtc_use_pipewire=false \ |
| 115 | symbol_level=0 \ |
| 116 | enable_iterator_debugging=false \ |
| 117 | android_package_prefix=\"livekit\" \ |
| 118 | use_custom_libcxx=false \ |
| 119 | use_clang_modules=false \ |
| 120 | use_rtti=true" |
| 121 | |
| 122 | if [ "$debug" = "true" ]; then |
| 123 | args="${args} is_asan=true is_lsan=true"; |
| 124 | fi |
| 125 | |
| 126 | # generate ninja files |
| 127 | gn gen "$OUTPUT_DIR" --root="src" --args="${args}" |
| 128 | |
| 129 | # build shared library |
| 130 | autoninja -C "$OUTPUT_DIR" :default \ |
| 131 | sdk/android:native_api \ |
| 132 | sdk/android:libwebrtc \ |
| 133 | sdk/android:libjingle_peerconnection_so |
| 134 | |
| 135 | # make libwebrtc.a |
| 136 | # don't include nasm |
| 137 | ar -rc "$ARTIFACTS_DIR/lib/libwebrtc.a" `find "$OUTPUT_DIR/obj" -name '*.o' -not -path "*/third_party/nasm/*"` |
| 138 | |
| 139 | # License generation is optional - may fail with some Python versions |
| 140 | # Use vpython3 from depot_tools for consistent Python version |
| 141 | vpython3 "./src/tools_webrtc/libs/generate_licenses.py" \ |
| 142 | --target :default "$OUTPUT_DIR" "$OUTPUT_DIR" || echo "Warning: License generation failed (non-critical)" |
| 143 | |
| 144 | cp "$OUTPUT_DIR/obj/webrtc.ninja" "$ARTIFACTS_DIR" |
| 145 | cp "$OUTPUT_DIR/libjingle_peerconnection_so.so" "$ARTIFACTS_DIR/lib" |
| 146 | cp "$OUTPUT_DIR/args.gn" "$ARTIFACTS_DIR" |
| 147 | |
| 148 | cp "$OUTPUT_DIR/LICENSE.md" "$ARTIFACTS_DIR" |
| 149 | |
| 150 | mkdir -p "$COMMAND_DIR/prefixed-jni/libs" |
| 151 | cp "$OUTPUT_DIR/lib.java/sdk/android/libwebrtc.jar" "$COMMAND_DIR/prefixed-jni/libs/classes.jar" |
| 152 | # Upstream compiles the Android Java sources with --release 21 since m150 (class file |
| 153 | # major version 65). Consumers on Android Gradle Plugin 7.x (e.g. Unity 2022.3) reject |
| 154 | # those class files. Stamp them back to Java 17 (major 61) before shadowing, like |
| 155 | # webrtc-sdk/android does for its Maven artifacts. |
| 156 | python3 "$COMMAND_DIR/tools/downgrade_android_class_version.py" "$COMMAND_DIR/prefixed-jni/libs/classes.jar" |
| 157 | cd "$COMMAND_DIR/prefixed-jni" && ./gradlew shadowJar |
| 158 | cp "$COMMAND_DIR/prefixed-jni/build/libs/prefixed-jni-all.jar" "$ARTIFACTS_DIR/libwebrtc.jar" |
| 159 | |
| 160 | cd .. |
| 161 | |
| 162 | cp "src/sdk/android/AndroidManifest.xml" "$ARTIFACTS_DIR" |
| 163 | |
| 164 | cd src |
| 165 | find . -name "*.h" -print | cpio -pd "$ARTIFACTS_DIR/include" |
| 166 | find . -name "*.inc" -print | cpio -pd "$ARTIFACTS_DIR/include" |