How to compile WebRTC library for Android
WebRTC project has stopped releasing official builds of the WebRTC library for Android on Maven Central. This means that if you want to use the WebRTC library in your Android application, you will need to build it yourself from the source code.
These are the steps to clone the WebRTC source code and build it into an Android Archive (AAR) library
As WebRTC Android development is only supported on Linux [Used ubuntu 20.04 for myself]. First of all, Install All the Required Dependencies
Before building the WebRTC library, you need to install the required dependencies. Run the following command to install them:
sudo apt-get update && sudo apt-get install -y git python python3 ninja-build openjdk-8-jdk-headless
Step 1: Clone the Depot Tools Repository
The first step is to clone the Depot Tools repository using the Git version control system. You can do this by running the following command in your terminal:
git clonehttps://chromium.googlesource.com/chromium/tools/depot_tools.git
This will clone the repository and create a directory named “depot_tools” in your home directory.
FYI: In the context of building the WebRTC library for Android, the Depot Tools repository is a set of command-line tools that are required to fetch, manage and build WebRTC’s source code. These tools are developed and maintained by the Chromium project and are used to manage dependencies across the Chromium project and its related projects.
When you clone the Depot Tools repository using Git, you download a set of scripts that help automate many of the tasks involved in building WebRTC. These scripts include “fetch”, which is used to download the WebRTC source code, and “gclient”, which is used to manage the dependencies of the WebRTC project. These script are used later in the process of building for aar file.
The Depot Tools repository is required to build the WebRTC library because it provides the necessary tools to download and manage the WebRTC source code and its dependencies. Without these tools, it would be difficult and time-consuming to manually download and manage all of the dependencies required to build the library.
Additionally, the Depot Tools repository is continuously updated to ensure that it works with the latest versions of the Chromium project and its related projects. By using the Depot Tools, you can be sure that you have the latest tools and scripts to build the WebRTC library.
Step 2: Add Depot Tools to Your PATH
Next, you need to add the “depot_tools” directory to your system’s PATH environment variable so that you can access the tools from anywhere in your terminal. You can do this by running the following command:
export PATH="$PATH:${HOME}/depot_tools"
Step 3: Create a Working Directory
Create a new directory to hold your WebRTC source code by running the following command:
mkdir webrtc_android
Then, navigate to this directory by running:
cd webrtc_android
Step 4: Fetch the WebRTC Code
To fetch the WebRTC source code, run the following command:
fetch --nohooks webrtc_android
This will download the WebRTC source code for android and its dependencies.
Step 5: Synchronize Your Code
Once the WebRTC code has been downloaded, you need to synchronize it by running the following command:
gclient sync
This command will ensure that you have the correct version of each dependency installed.
Step 6: Navigate to the Source Code Directory
Navigate to the “src” directory in your working directory by running the following command:
cd src/
Step 7: Install Build Dependencies
Before you can build the WebRTC AAR library, you need to install its build dependencies. You can do this by running the following command:
./build/install-build-deps.sh
FYI: this step is done to install the required dependencies and tools needed to build the WebRTC library on your machine.
WebRTC has several build dependencies that must be installed before you can build the library. These dependencies include build tools, libraries, and development headers. For example, some of the dependencies required to build the library include Python, Ninja, and the Android NDK.
This is required because without these dependencies, the build process will fail. This step ensures that all of the required dependencies are installed and that the build process can proceed smoothly.
By installing the required dependencies and tools, you can ensure that your machine is properly set up to build the WebRTC library, and that the build process will proceed smoothly without encountering any issues related to missing dependencies.
Step 8: Check Available Git Branches
To see the available Git branches, run the following command:
git branch -r
Step 9: Checkout a Git Branch
To checkout the master branch, run the following command:
git checkout origin/master
Step 10: Verify Your Git Branch
To verify that you’re in the origin/master branch, run the following command:
git branch
Step 11: Build the WebRTC AAR Library
Finally, you can build the WebRTC AAR library by running the following command:
tools_webrtc/android/build_aar.py
This will build the library and place it in the “src” directory i.e. your working directory. You can then use this library in your Android project.
FYI: This is used as the final step in building the WebRTC library for Android.
The script takes the compiled object files and builds them into an AAR (Android Archive) file, which is a binary distribution format that includes compiled Java code and resources, as well as a compiled native library. The AAR file can then be included in an Android application as a dependency.
The “build_aar.py” script has several arguments that can be used to configure the build process, such as specifying the target architecture and the location of the generated AAR file. By default, the script builds the library for all supported architectures and generates an AAR file in the “out” directory of the WebRTC source code.
In summary, the “tools_webrtc/android/build_aar.py” script is used as the final step in building the WebRTC library for Android, and it is responsible for building the AAR file that can be included in an Android application.
Note: Building WebRTC for Android can take a long time and requires a lot of disk space (around 30GB) and memory. Make sure you have enough resources available before starting the build.