Compiling Native WebRTC for Android
A comprehensive guide to building the WebRTC native stack for Android from source code, with detailed steps and requirements.
Build Machine Requirements
Hardware Requirements
  • Hard-Disk Space: 30-50GB (minimum 20GB)
  • RAM: 8+ GB recommended (minimum 4GB)
  • High-Speed Internet (wired ethernet preferred)
Operating System
  • Ubuntu 18+ recommended (Ubuntu 22.04 LTS used in guide)
  • Avoid WSL, use bare-metal or VM (VM preferred)
Development Tools
  • Git for version control
  • Python v3.7+ (3.10.6 used in guide)
  • Clang v9+ (v14 used in guide)
  • Vim (v8.2) for quick edits
Setting Up the Build Environment
Install OpenJDK
WebRTC bundles compiled Java sources as low-level RTC classes. Install JDK 8 or 11 with: sudo add-apt-repository ppa:openjdk-r/ppa && sudo apt-get install openjdk-8-jdk && sudo apt-get install pkg-config && sudo apt-get update
Setup Chromium Build Tools
Clone depot tools and update PATH: git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git followed by export PATH="$PATH:/path/to/depot_tools"
Pull WebRTC Source Code
Create a dedicated directory, then use fetch tool: mkdir webrtc_android && cd webrtc_android && fetch --nohooks webrtc_android followed by gclient sync
Building the WebRTC Library
Navigate to Source Directory
Change to the src folder: cd src/
Install dependencies: build/install-build-deps.sh
Select Branch
List branches: git branch -r
Checkout desired branch: git checkout branch-heads/5414
Sync Dependencies
Update to latest code: gclient sync
Build Android Library
Run the build script: tools_webrtc/android/build_aar.py
Post-Build Steps
Locate AAR File
Find the compiled library: ls | grep libwebrtc.aar
Publish to Local Maven
Install Maven and publish the AAR: sudo apt-get install maven && mvn install:install-file -Dfile=./google-webrtc-M108.aar -DgroupId=com.aar.app -DartifactId=google-webrtc -Dversion=108.0.0 -Dpackaging=aar -DlocalRepositoryPath=./libwebrtc-android -DcreateChecksum=true
Reference in Android Projects
Include in Gradle dependencies: implementation (name:'libwebrtc', ext:'aar')
Docker-Based Automation
Pull Docker Image
Get the WebRTC build image: docker pull piasy/webrtc-build
Run Docker Container
Start the container: docker run --rm -v :/webrtc -t -i piasy/webrtc-build
Build Inside Container
Use commands: get_webrtc and build_apprtc
Run AppRTC Server
Deploy server: docker pull piasy/apprtc-server followed by docker run --rm --net=host -e PUBLIC_IP= -it piasy/apprtc-server
Additional Resources
Official Documentation
WebRTC.org native code guides and Android-specific documentation
Source Code Repositories
Chromium WebRTC source at googlesource.com and reference implementations on GitHub
Tutorial Videos
Step-by-step compilation guides and implementation examples
Sample script for Automation
This bash script automates the WebRTC Android build process by fetching source code, checking out a specified branch, installing dependencies, and building AAR libraries for multiple CPU architectures (arm, arm64, x86).
#!/bin/bash # This script fetches WebRTC source code using Chromium depot tools, # checks out a specified branch, installs build dependencies, # and builds libwebrtc Android AAR libraries for armeabi-v7a, arm64-v8a, and x86 CPUs. # Usage: ./build_webrtc.sh <branch_name> set -e # Exit on error # Check if the script is being executed from the home directory if [ "$PWD" != "$HOME" ]; then echo "This script must be executed from the home directory" exit 1 fi # Input validation if [ $# -ne 1 ]; then echo "Usage: $0 <branch_name>" exit 1 fi BRANCH="$1" echo "Checking out branch '$BRANCH'" # Clone Chromium depot tools if it doesn't exist already if [ ! -d "$HOME/depot_tools" ]; then git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git "$HOME/depot_tools" fi # Add Chromium depot tools to the PATH export PATH="$HOME/depot_tools:$PATH" # Check if webrtc_android directory already exists if [ -d "$HOME/webrtc_android" ]; then echo "webrtc_android directory already exists. Updating the repository." cd "$HOME/webrtc_android/src" # Ensure we're on the main branch and pull the latest changes git checkout main git pull --rebase else # Fetch WebRTC source code mkdir -p "$HOME/webrtc_android" cd "$HOME/webrtc_android" fetch --nohooks webrtc_android fi # Sync the source code to the latest version cd "$HOME/webrtc_android/src" gclient sync # Install dependencies if [ -x build/install-build-deps.sh ]; then ./build/install-build-deps.sh else echo "Dependency installation script not found!" exit 1 fi # List remote branches echo "Available remote branches:" git branch -r # Check if the specified branch exists if ! git show-ref --verify --quiet "refs/remotes/origin/$BRANCH"; then echo "Branch '$BRANCH' does not exist on the remote repository." exit 1 fi # Checkout the specified branch git checkout "$BRANCH" gclient sync # Build libwebrtc AAR for armeabi-v7a, arm64-v8a, and x86 echo "Building libwebrtc AAR for armeabi-v7a, arm64-v8a, and x86" tools_webrtc/android/build_aar.py \ --arch=arm,arm64,x86 \ --target_arch=arm,arm64,x86 \ --toolchain=clang \ --extra-gn-args='target_os="android" target_cpu="arm" enable_stripping=true is_debug=false' \ --output-dir=out/android_libs/ echo "Successfully built libwebrtc AAR libraries."