Compile and Build WebRtc Library for Android Platform | Latest Branch Head m112

Muhammad Usman Bashir
10 min readApr 14, 2022

--

All in All WebRtc

A Step by Step Guide to ** Compile and Build ** WebRTC Library for Android within 10 minutes. This library is based on the Complete WebRTC Native Stack for Android.

Make it easy, Make it simple, then taste the magic of WebRtc in Android.

I am going to initiate simple series of WebRtc Compilation and how we can debug Complete WebRtc Native Stack single-handedly. This post involves a Step by Step Guide to setting up WebRTC Compiler to build customized Android SDK based on WebRtc. As we all know, Android Programs run into Dalvik Virtual Machine. Native Development tool (NDK) allows users to execute some of the programs using native-code languages such as C/C++.

I have made this document pretty straightforward for those who are using WebRTC for the first time or for those who are still struggling to get the most out of it. To get more details on WebRTC you can also visit my Github Profile:

The easiest way to get started is using the official prebuilt libraries available at JCenter. These libraries are compiled from the tip-of-tree and are meant for development purposes only.

On Android Studio 3+, add the following SDK into your dependencies in App level Gradle:

implementation 'org.webrtc:google-webrtc:1.0.+'

Note: Android development is only supported on Linux Platform; Your Preference should be Ubuntu 22.04 LTS

So, I must suggest using any Linux Environment ( Ubuntu 22.04 ) for Android WebRtc Development. When you have set up your Linux Environment, then make sure of these important things beforehand:

Currently, The only supported distros are:

Ubuntu 14.04 LTS (trusty with EoL April 2022) Ubuntu 16.04 LTS (xenial with EoL April 2024) Ubuntu 18.04 LTS (bionic with EoL April 2028) Ubuntu 19.04 (disco) Ubuntu 19.10 (eoan) Debian 8 (jessie) or later

If you go for the latest Linux Environment available i.e. ( Ubuntu 22.04), It might not install most of the dependencies to build the library.

Strictly Recommended: Don’t Open Multiple Terminal Tabs/Windows to install any dependencies related to WebRTC Pre-requisites, Do Every of Your Task Related to WebRTC in One and Only One Terminal’s Tab/Window.

Before starting with WebRTC Native Stack, open your terminal, first install these modules, using the following Commands:

# Add OpenJDK repository
sudo add-apt-repository ppa:openjdk-r/ppa

# Update package list
sudo apt-get update

# Install OpenJDK 8
sudo apt-get install openjdk-8-jdk

# Install pkg-config
sudo apt-get install pkg-config

Most of the developers prefer, Inline commands as follows:

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

These are the only 11-Steps which are basically the cream of WebRTC Native Development for Android. I have explained each step below in this document. To get a basic understanding of these given steps see Explanation of Steps and then start with the following steps one by one.

Note: Every step takes its own time based on the machine specs and internet speed, so make sure every step is completed without interruption.

Here is a bash script to install all the necessary dependencies and set up WebRTC compilation for Android:

#!/bin/bash

# 1. Clone depot_tools
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

# 2. Add depot_tools to PATH
export PATH="$PATH:${HOME}/depot_tools"

# 3. Create and enter the webrtc_android directory
mkdir webrtc_android
cd webrtc_android

# 4. Fetch WebRTC Android source code
fetch --nohooks webrtc_android

# 5. Synchronize the source code
gclient sync

# 6. Enter the src directory
cd src

# 7. Install build dependencies
./build/install-build-deps.sh

# 8. List remote branches
git branch -r

# 9. Make sure you're in the origin/master branch
git checkout origin/master

# 10. Display current branch
git branch

# 11. Build the WebRTC AAR file
tools_webrtc/android/build_aar.py

Save this script as webrtc_android_setup.sh and run it using:

chmod +x webrtc_android_setup.sh
./webrtc_android_setup.sh

NOTE: If you perform all of the above steps in an AWS EC2 Virtual Machine with 30-50 CPU Cores and with the speed of the internet around, then all of the above processes might take around 5-6 minutes. Hurry!!!

Now, if you look in the webrtc_android/src/ directory, It turns out that you will end up with the compilation and building of libwebrtc.aar.

Explanation of 11-Steps:

The above mentioned 11-Steps involve these three procedures to deal with:

Getting the Code:

This procedure involves Steps from Step-1 to Step-5. To get the native code of WebRTC from Google Git, there is always a need

Chromium and Chromium OS use a package of scripts called depot_tools to manage checkouts and code reviews. The depot_tools package includes custom git binary, ninja build tools, gclient, gn, ninja, gcl, git-cl, repo, and others.

It is very easy to get these tools. Just take your first step by cloning the repository with this command:

1- git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

Now, it's time to add a path into Environment Variables PATH, so that you can easily access the binaries/dependencies that must be included in the repository. In this way, these binaries would be available in the current terminal:

2- export PATH="$PATH:${HOME}/depot_tools"

If you close the current instance of the Linux terminal, then before starting any step (except step-1), you have to repeat this step to get working binaries in that instance. To get more detailed intuition, you can visit Get Chromium Depot Tools

After this make a new directory and go into that directory using these commands:

3- mkdir webrtc_android 
3.1- cd webrtc_android

This is the directory, where all of your WebRTC Native Stack will be cloned and synced. Now, here comes the two giant steps of this getting code procedure which are:

  • Fetch command is used to get the regular WebRTC Native Source Code with the Android-specific parts added.
  • Gclient Sync command is used to update your checkout for underlying source code i.e WebRTC Native Code.

Make sure your current directory is webrtc_android/:

4- fetch --nohooks webrtc_android 
5- gclient sync

The gclient sync command will pull all dependencies of the Chromium src checkout. You will need to run this any time you update the main src checkout, including when you switch branches.

Try to avoid any kind of interruption as these processes may take enough time based on Machine Specs and Internet Speed because these processes require the android build chain tools for which;

Notice: It is to be noted that the Android-specific parts like the Android SDK and NDK are quite large (~8 GB), so the total checkout size will be about 16 GB.

After these steps, you will end up with an Updated and Synced WebRTC Native Code for Android.

Dependencies and Branch Selection:

To install all required dependencies for Linux, a script is provided for Ubuntu which is unfortunately only available after your first gclient sync:

Make sure your current directory is webrtc_android/src/

6- cd src/ 
7- ./build/install-build-deps.sh

Most of the libraries installed with this script are not needed since WebRTC Developers now build using Debian sysrootimages in build/linux, but there are still some tools needed for the build that is installed with install-build-deps.sh. These all dependencies are very important to building the Native Source Code.

Here I will explain Step-8,9. Some of the developers recommend looking for particular git branches before Compiling the Native Source Code. To list down all the available WebRTC Checkout Branches use this command:

You can see the available latest branches looks like this as follows:

To use some specific WebRTC Branch Head

Now you can checkout to the latest branch which is branch-heads/m112 .

Sometimes, the remote branch reference is not fetched properly. You can fetch it first and then checkout the branch. Please follow these steps:

  1. Fetch the remote branch reference:
9- git fetch origin refs/branch-heads/5615

2. Checkout the branch:

10- git checkout -b m112 FETCH_HEAD
10.1- gclient sync

But I will recommend you to checkout with the origin/master:

9- git checkout origin/master

This will help you to resolve most of the compilation issues. To get the details about your current branch you can simply use these commands:

10- git branch or 
10- git status

Compilation and Building:

There are two ways to compile the WebRTC Native Stack for Android to build the library i.e. libwebrtc.aar. You can use the following methods such as:

1- Using AAR Build Tools

2- Using Manual Compilation

Using AAR Build Tools:

This is the most beautiful process. It would compile the source code for all supported CPU types such as;

  • arm64-v8a,
  • armeabi-v7a,
  • x86,
  • x86_64

and at the end package all these native libraries and .jar library into *.aar file.

Make sure your current working directory is webrtc_android/src/ of your workspace. Then run:

11- tools_webrtc/android/build_aar.py

This process will take some time based on your machine specs and internet speed, so here we go:

Now, if you look in the webrtc_android/src/ directory, It turns out that you will end up with the compilation and building of libwebrtc.aar.

Using Manual Compilation:

This process will manually compile the source code for each particular CPU type. Manual Compiling involves these two steps:

1- Generate projects using GN.

2- Compile using Ninja

This step will compile the library for both Debug and Release Mode of Development.

Make sure your current working directory is webrtc_android/src/ of your workspace. Then run:

11- gn gen out/Debug --args='target_os="android" target_cpu="arm"' 
11- gn gen out/Release --args='is_debug=false is_component_build=false rtc_include_tests=false target_os="android" target_cpu="arm"'

You can specify a directory of your own choice instead of out/Debug, to enable managing multiple configurations in parallel.

  • To build for ARM64: use target_cpu=”arm64"
  • To build for 32-bit x86: use target_cpu=”x86"
  • To build for 64-bit x64: use target_cpu=”x64"

For compilation, you can simply use the following commands ( out/Debug, out/Release):

11.1- ninja -C out/Debug 
11.1- ninja -C out/Release

The output of the above process will be in the out/Debug or out/Release. Manually compilation of the source code is quite difficult and challenging. As with, manual compilation you also need to package all these libraries into .aar manually which is more time-consuming.

The Native *.so file will be in the lib.unstripped/, and the java .jar library will be in the lib.java/ directory.

The Native .so library is un-stripped, you can strip it to minimize file size using strip tools for a particular CPU type. Such tools are located in the following Directory:

webrtc_android/src/third_party/android_ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip (for x64 binary .so)

You can use this (.aar) library in your project using:

Use of the Library:

You can use the Libwebrtc.aar in Android using the following approaches;

  • Manual Package Inclusion (*.aar)
  • Publish to Maven Repository

Manual Package Inclusion:

First kept your libwebrtc.aar file into the libs directory and then open Project level build.gradle and include flatDir such as:

allprojects { 
repositories {
jcenter()
flatDir {
dirs 'libs'
}
google()
}
}

then, open Application Level build.gradle file and add *.aar file such as:

dependencies { 
compile(name:'libwebrtc', ext:'aar')
}

If everything goes well you will see library entry is made in build exploded-aar. Also note that if you are importing a .aar file from another project that has dependencies you’ll need to include these in your build.gradle, too.

Publish to Maven Repository:

You can use maven to add *.aar libraries into the existing maven repository using these commands:

# Install Maven
sudo apt-get update
sudo apt-get install maven

# Install the WebRTC AAR file for M112 branch into a local Maven repository
mvn install:install-file -Dfile=./google-webrtc-M112.aar -DgroupId=com.aar.app -DartifactId=google-webrtc -Dversion=M112 -Dpackaging=aar -DlocalRepositoryPath=./libwebrtc-android -DcreateChecksum=true

Then you can add a repository in the root Gradle file followed by adding it to your app module.

Compilation Issues:

Some of you guys may try different branch heads of WebRTC for their development. But I must suggest using the origin/master. Because, it turns out that while using different branch heads, you may end up with multiple issues regarding BUILD.gn or inside this class tools_webrtc/android/build_aar.py,

which may be an unhealthy experience. Some kind of the issues are as follows:

  1. Using AAR Build Tools:

2. Using Manual Compilation:

And you may end up with some kind of many more issues. But if you have properly carried out the above steps, then hopefully, you won’t be caught up with issues regarding Compilation and Building libraries.

Now, If you have already completed your task to step-7 with the whatever branch let's say in between

( -- branch-heads/5112 (m104)<--> branch-heads/5615 (m112))

then you can again select the branch of origin/master instead of any other branch-heads. To do that try the following steps:

Make sure you’re in this directory webrtc_android/src/:

- git checkout origin/master # To make sure you're using origin/master 
- git branch
- gclient revert
- gclient sync
- tools_webrtc/android/build_aar.py

And hopefully, you will have your issues sorted because currently, you are checking out a branch that is behind the origin/master and doesn't have all dependencies and modules over there which are necessary to build your libwebrtc.aar.

I have tried to get most out of it. Hopefully, If you will follow these steps properly, you will enjoy the development. Again thanks to WebRTC-Developers for such a huge module for pre-processing as well as post-processing of Real-time Communication.

Conclusion:

In conclusion, you now have the WebRTC Native Stack at your disposal, allowing you to make modifications to the native stack, rebuild the library, and use it either manually or through publishing. If you encounter any issues or have questions, don’t hesitate to visit the Discuss-webrtc forum. This group reviews and approves posts before they are displayed, so be patient. Rest assured, your issue or question will be addressed in due course.

Originally published at https://github.com.

--

--

Muhammad Usman Bashir

Mr. WebRTC, Endorsed by Antmedia | Leading Voice & Active Contributor in IETF & Discuss-Webrtc | WebRTC Patent Holder & Author | Media Server, UE & Unity Expert