Build Chromium Net For Android

##前言

由于项目中需要使用验证QUIC协议,针对目前的QUIC协议的客户端支持,唯一的选择就是Chromium的Net库,所以方便后续的功能定制开发,就花了大部分时间来进行Net模块的编译和开发。

一些坑的感受

Chromium作为一个庞大的工程,其文档和工具应该都非常完善,所以需要开发Chromium最好的就是Chromium本身提供的说明文档。

Chromium工程和依赖非常大,我这边去到了25GB之多,这还是不包括系统本身的依赖。

Chromium本身不支持Mac下的编译,所以我选择了使用Ubuntu来进行编译开发

Chromium

站点:Chromium

代码搜索功能:代码搜索

代码的搜索功能是非常有用的功能,能够快速的找到相关的代码引用,当然如果自己去搭建也是可以的(目前我已经编译出Ubuntu的Chromium和Android的Chromium,能够顺利的进行debug,后续文章介绍如果操作)

针对Android的代码获取

这里面有详细的操作流程

获取代码

获取代码需要能够访问Google的网络,由于不具备续传,所以断了就GG了。

Install depot_tools

获取depot_tools repository:

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

添加depot_tools到PATH

1
export PATH="$PATH:/path/to/depot_tools"

获取代码

1
2
mkdir ~/chromium && cd ~/chromium
fetch --no-history android

这样会快一点,另外这样的是最新的版本,如果需要切换到指定的版本,

1
git fetch origin tag 73.0.3657.0

这个步骤会比较漫长,耐心等待吧。。。

之后就参考官方的说明就可以了。

编译Cronet模块

Chromium 采用 gn + ninja来构建的,通过一些配置选项来支持构建的。
如果clone下代码后并且已经安装好依赖后,Cronet模块位于

1
src/components/cronet

这个是针对Android和iOS提供了net模块的支持,android文件夹里面有jni模块和Java层的api方法,jni不需要自己手动去编译,Chromium是通过注解来自动生成的。

需要编译这个也很简单,在这个目录中有一个编译介绍的文档,我这边贴出来, 很详细了吧

Cronet build instructions

[TOC]

Checking out the code

Follow all the
Get the Code
instructions for your target platform up to and including running hooks.

Building Cronet for development and debugging

To build Cronet for development and debugging purposes:

First, gn is used to create ninja files targeting the intended platform, then
ninja executes the ninja files to run the build.

Android / iOS builds

$ ./components/cronet/tools/cr_cronet.py gn --out_dir=out/Cronet

If the build host is Linux, Android binaries will be built. If the build host is
macOS, iOS binaries will be built.

Note: these commands clobber output of previously executed gn commands in
out/Cronet. If --out_dir is left out, the output directory defaults to
out/Debug for debug builds and out/Release for release builds (see below).

If --x86 option is specified, then a native library is built for Intel x86
architecture, and the output directory defaults to out/Debug-x86 if
unspecified. This can be useful for running on mobile emulators.

Desktop builds (targets the current OS)

TODO(caraitto): Specify how to target Chrome OS and Fuchsia.

gn gen out/Cronet

Running the ninja files

Now, use the generated ninja files to execute the build against the
cronet_package build target:

$ ninja -C out/Cronet cronet_package

Building Cronet mobile for releases

To build Cronet with optimizations and with debug information stripped out:

$ ./components/cronet/tools/cr_cronet.py gn --release
$ ninja -C out/Release cronet_package

Note: these commands clobber output of previously executed gn commands in
out/Release.

Building for other architectures

By default ARMv7 32-bit executables are generated. To generate executables
targeting other architectures modify cr_cronet.py‘s
gn_args variable to include:

  • For ARMv8 64-bit: target_cpu="arm64"
  • For x86 32-bit: target_cpu="x86"
  • For x86 64-bit: target_cpu="x64"

Alternatively you can run gn args {out_dir} and modify arguments in the editor
that comes up. This has advantage of not changing cr_cronet.py.