AV Development

Building Android WebRTC on macOS

According to the official WebRTC build guide, the Android version of WebRTC is only supported on Ubuntu. Building Android WebRTC on Ubuntu can be cumbersome—you often end up running a VM, which makes code changes very inconvenient. To simplify the build process, you can migrate the build environment to macOS and apply appropriate modifications for M1 or x86 architectures.

Park

Park

AV Development

Written on

Share
Building Android WebRTC on macOS

According to the official WebRTC build guide, the Android version of WebRTC is only supported on Ubuntu. Building Android WebRTC on Ubuntu can be cumbersome—you often end up running a VM, which makes code changes very inconvenient. To simplify the build process, you can migrate the build environment to macOS and apply appropriate modifications for M1 or x86 architectures.

1. Code Preparation

First, prepare two codebases. There are plenty of guides online, so this is not the focus of this article and will not be covered in detail:

fetch webrtc_android
fetch webrtc_ios

The overall migration approach is as follows:

The Android and iOS WebRTC codebases are essentially the same—the main differences are third-party dependencies and toolchains. The purpose of preparing webrtc_ios is to download and configure the basic WebRTC build environment, including dependencies and toolchains. Then replace the Ubuntu toolchain with the macOS version.

2. Dependencies and Tools

2.1. Copy the Android Directory

Copy all directories under webrtc_android/third_party that start with "android" into webrtc_ios/third_party, overwriting any directories with the same name.

2.2. R8 Directory

Copy the webrtc_android/third_party/r8 directory to webrtc_ios/third_party/r8.

2.3. NDK

Copy the webrtc_android/third_party/android_ndk directory to webrtc_ios/third_party/android_ndk.

Download the macOS version of the NDK, then replace the webrtc_ios/third_party/android_ndk directory. Note that you must keep the BUILD.gn file in the android_ndk subdirectory.

2.4. JDK

Copy the webrtc_android/third_party/jdk/current directory to webrtc_ios/third_party/jdk/current.

Manually download JDK version 11 or higher, then replace the contents of the webrtc_ios/third_party/jdk/current directory.

2.5. LLVM

Copy the following macOS files into the third_party/llvm-build/Release+Asserts/lib/clang/15.0.0/lib directory:

android_ndk/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/12.0.9/lib/linux

2.6. Build Script Modifications

Modify the third_party/ijar/BUILD.gn file and add the || is_apple condition to the if (is_linux || is_chromeos || is_apple) block.

if (is_linux || is_chromeos || is_apple) {
  config("ijar_compiler_flags") {
    if (is_clang) {
      cflags = [

Modify the following file names in the third_party/llvm-build/Release+Asserts/bin directory:

llvm-nm
llvm-strip
llvm-objcopy

2.7. Other Errors

After the modifications above, errors caused by environment configuration should be minimal. If errors still occur, analyze the specific issue and apply the corresponding fix. Overall, the required changes are not extensive.

3. Build

Use the following command to build. WebRTC has many build parameters and can be quite complex—a dedicated article on WebRTC builds may follow when time permits.

gn gen out/armv8 --args='target_os="android" target_cpu="arm64" is_component_build=false is_debug=false rtc_enable_protobuf=false rtc_include_tests=false rtc_build_examples=false rtc_enable_sctp=false rtc_enable_libevent=false rtc_build_tools=false disable_android_lint=false use_errorprone_java_compiler=false use_custom_libcxx=false android32_ndk_api_level=18'

4. References

  1. OSX(Macos) 下编译 WebRTC - Android - 简书
  2. 在 macOS 中编译 webrtc android 端代码