WebRTC Android源码编译

对于WebRTC的学习来说,首先需要搞定的是源码的编译,由于国内的特殊环境和WebRTC本身的复杂性,导致WebRTC源码编译成了WebRTC学习的第一道门槛。这里把自己在编译WebRTC源码过程中遇到的一些坑进行总结分享,希望对大家有帮助。主要参考官网和WebRTC的编译配置脚本,整个操作过程需要具备科学上学的环境,第一次下载好环境和源码后,后面就不需要了。这里以Android端为例,编译环境为Ubuntu 18.04(再高版本会提示不支持),WebRTC官网有提到Android端暂时只支持在Linux下编译。虽然网上也有资源介绍在Mac环境下编译的,但是用虚拟机装个Ubuntu还是挺方便的,咱就不折腾了。

安装depots_tools工具包

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

将depot_tools目录添加到环境变量PATH中

1
2
3
vim ~/.profile
export PATH="$PATH:/path/to/depot_tools"
source ~/.profile

下载WebRTC源码

1
fetch --nohooks webrtc_android

由于WebRTC源码比较大,中间网络问题可能会出错,出错的时候用gclient sync继续就可以了

下载编译所需依赖包和工具

1
2
./build/install-build-deps.sh --no-chromeos-fonts
. /build/install-build-deps-android.sh

下载指定分支

1
2
$ git checkout -b my_branch refs/remotes/branch-heads/83
$ gclient sync

源码编译

第一条命令是生成编译工程的,可以添加一些参数来控制编译生成,这里可以研究下源码里的webrtc.gni文件,里面有可以指定的参数,这里主要介绍三个:

target_os:因为是在Android平台上运行的,因此这里指定为”android”
target_cpu: 这里指定运行的硬件平台,arm平台则是”arm”,如果是arm64平台则是”arm64”
is_debug:表示生成是否是debug包

第二条命令是启动ninja开始编译,编译成功后,会在out_arm/debug目录生成对应的jar包和so库文件

1
2
gn gen out_arm/debug --args='is_debug=true target_os="android" target_cpu="arm" rtc_include_tests=false rtc_build_tools=false rtc_build_examples=false'
ninja -C out_arm/debug

编译问题解决

  • 问题一:gn.py运行失败

    gn.py: Could not find checkout in any parent of the current path.
    This must be run inside a checkout.

这个问题通常发生在,移动了WebRTC源码目录的时候。这里需要看下是否已经把下载的WebRTC源码都完整拷贝了,进到下载WebRTC源码的目录可以看到这个目录还有几个隐藏目录和文件,这几个文件也是需要一起拷贝过去的,后面用gn命令生成编译工程的时候会去检查这几个文件。

.cipd
.gclient
.gclient_entries

  • 问题二:

chromium style问题
clang.gni中关闭chromium style检查,这里只是暂时关闭,为了代码风格的统一介绍还是按照chromium style还添加自己的代码

1
2
3
4
5
6
7
declare_args() {
 # Indicates if the build should use the Chrome-specific plugins for enforcing
 # coding guidelines, etc. Only used when compiling with Clang.

 clang_use_chrome_plugins = false # is_clang && !is_nacl && !use_xcode_clang
 clang_base_path = default_clang_base_path
}

参考:

  1. WebRTC官网