How to build WebRTC for Android in Ubuntu ~21.04~ 22.04
Edit on GitHubGoogle used to provide
prebuild Android images of
libWebRTC
library, and in fact, it’s (still) the recomended way to use them on
its own documentation.
But starting on WebRTC M80 release (January 2020), they decided to
deprecate the binary mobile libraries,
and the reasons were that the builds were intended just only for development
purposses, and
users were already using building themselves with their own customizations, or using third party libraries that embeded them
(where have been left developers that just want to build a WebRTC enabled mobile
app?), and they just only provided another build in August 2020 (1.0.32006) to
fill some important security holes, in case someone (everybody?) was still using
the binary mobile libraries.
editor’s note: content updated on February 27nd 2023 to reflect the latest changes in the build process.
In addition to that, that binary libraries are available to use with Maven, but Bintray will be deprecated on May 1st 2021, and seems they will not be available after February 1st 2022, so it’s not clear if the binary mobile libraries would still be available when using Maven itself.
Due to both problems (no new libraries versions, and not sure about future
availability of current ones), and since I’ve not able to find other online
providers for the binary libraries, best option seems to waste some hard disk
space (16GB… it downloads all the Chrome source code and the build environment
tools) and compile them myself. The automated build script that generates the
binary mobile library .aar
file was published and
there are instructions to build it yourself,
that’s a huge advantage over needing to compile and build the libraries yourself
by hand. But while I was using it, I’ve found some issues and errors in the
instructions themselves when executing them on Ubuntu 21.04 (released just
yesterday :-) ), so here are the fixed instructions with some additional
comments:
-
Create a new folder where to work in. This seems obvious, but if not, you’ll end up filling with garbage your
$HOME
directory (I’m n00b). Maybe having there the tools and a bigsrc/
folder where to host all company projects makes sense inside Google organization itself…:mkdir webrtc_android cd webrtc_android
-
Download the Chromium
depot_tools
, and export them:git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git export PATH=$PATH:$PWD/depot_tools
-
Install Python.
depot_tools
makes use of thepython
executable, because it’s compatible with both Python 2 and 3. On Ubuntu 21.04 it’s not available since it was traditionally linked to the now deprecated Python 2, and to differenciate from it, it was being used thepython3
executable, so in a clean slate install,depot_tools
will fail because it’s not found. To fix that, we can install thepython-is-python3
package:sudo apt install python-is-python3
-
Install snap. I hate it (I’m old school, and an APT fan), but now Chromium build tools make use of bazel, and it’s only available as snap packages, at least for recent versions. I will need to review this in the future, but for now, just install it:
sudo apt install snap
-
Get the build environment tools and Chromium source code using
depot_tools
, andcd
inside it. This will a 16GB checkout and will take A LONG TIME. In my case, it lasted 4.5 hours for thefetch
command and more than one hour for thegclient
one…:fetch --nohooks webrtc_android gclient sync cd src
-
Update all system dependencies. It should not be needed, but the fact is that building the library has failed me in a slate install of Ubuntu 22.04. After updating the system to the latests packages, it worked fine. So, just in case, execute the following commands:
apt update apt upgrade apt dist-upgrade
-
Install build dependencies. This will download again more dependencies, in this case system wide build tools. One of them will be Python 2, so the previously installed
python-is-python3
package will be removed:./build/install-build-deps.sh
In case you are not using one of the Ubuntu LTS versions, you may need to use the
--unsupported
flag to by-pass the version checks and install the dependencies, but it didn’t worked for me. Your mileage may vary. -
Select the release to build. Until WebRTC M80 release, there were branches for each one of the Chromium releases, so it was easy to know what version was each one, but after that, Google started to create branches in a daily basis. Use what’s currently in
master
branch or from any of the daily branches is totally fine, but if you want to build the exact copy of the library of a particular release, you can search for your desired Milestion at Chromium data website, and match it with the WebRTC column to find the daily branch number. For example, to build thelibWebrtc
library version used by latest stable Chrome 110 (build number5481
) you would just need to change to thebranch-heads/5481
:git checkout branch-heads/5481
This will left the repository in detached mode, but it’s not something to worry about. Also, you can see all available branches in case you want to use another daily branch by executing
git branch -r
. At this moment, latest one is5620
(dev
branch is at5615
).Once changed to the desired daily build branch, we need to reset and sync the repository code, and download again more dependencies and code. It seems this is needed because previous
gclient
when we were inmaster
branch would have left some temporal files (maybe we could have changed branches before?) so the build could fail, so this way we make sure to have the correct ones:gclient revert gclient sync
-
Finally, compile the AAR file. This will compile
libWebrtc
library for all the Android native supported platforms (arm64-v8a
,armeabi-v7a
,x86
andx86_64
) and package them in alibwebrtc.aar
file in the root of thesrc
folder that can be used in our Android project as a local dependency or published to our own Maven repository:tools_webrtc/android/build_aar.py
-
Once we have build the library, to update the code and build newer versions is just a matter of run
git remote update
to get the new daily build branches, and compile again. In case of problems, repeat the steps 6 and 7 to fully download again the build tools and dependencies and compile once again.
Ideally, all this steps could be automated and run in a nightly basis, creating a new reference in Maven to this automated builds. Github Actions could do a good job for this, storing the nightly builds as project releases or also as a Github Packages Maven repository, but the Github Actions only provides 2000 minutes per month (a bit more than 1 hour per day), so without a cache it would be problematic to generate nightly builds, but checking only to run where there are new Milestones (similar to what I do in OS lifecycle repo), it could probably work… Maybe one day I’ll implement it :-) By the moment, the raw JSON info for the different Milestones can be found at https://chromiumdash.appspot.com/fetch_milestones.
Bonus update: add the library as a dependency
An important topic I’ve not covered before: how to use the compiled .aar
file.
Android documentation
has full info about how to create and use libraries as dependencies with
Android Studio, but the important steps
for our use case are:
-
Add the
libwebrtc.aar
file:- Click File > New > New Module.
- Click Import .JAR/.AAR Package, then click Next.
- Enter the location of the
libwebrtc.aar
file then click Finish.
Android Studio creates a module directory, copies the
libwebrtc.aar
file into the module, and generates abuild.gradle
file for it, with the following contents:configurations.maybeCreate("default") artifacts.add("default", file('libwebrtc.aar'))
-
Make sure the
libwebrtc
library is listed at the top of yoursettings.gradle
file, like this:include ':app', ':libwebrtc'
-
Open the app module’s
build.gradle
file and add a new line for thelibwebrtc
library to thedependencies
block as shown in the following snippet:dependencies { implementation project(":libwebrtc") }
In case there was a reference to the old
libwebrtc
library from the Maven registry, remove it too. -
Click Sync Project with Gradle Files.
From now on, next project builds will be using your build of the libwebrtc.aar
file located at libwebrtc/libwebrtc.aar
, instead of obsolete one provided by
Maven.
Comment on Twitter
You can leave a comment by replying this tweet.