Android Chromium 内核编译

1,167 阅读4分钟

内核编译文档

www.chromium.org/Home/

GN构建语法

gn.googlesource.com/gn/+/master…

编译环境

Ubuntu 22.04.3 LTS

查看NDK版本,建议和默认的保持一致

gn args --list out/Debug    列出所有可选的编译参数。
android_ndk_root
    Current value (from the default) = "//third_party/android_toolchain/ndk"
      From //build/config/android/config.gni:196

android_ndk_version
    Current value (from the default) = "r25c"
      From //build/config/android/config.gni:197

编译产物下载:

www.chromium.org/getting-inv…

Chromium在线代码查看

方便在线查阅内核代码,下载到本地编译

source.chromium.org/chromium/ch…

Android Chrome编译:

chromium.googlesource.com/chromium/sr…

  • Install depot_tools
  • Get the code
  • Setting up the build

获取代码

fetch --nohooks --no-history android
gclient sync --nohooks --no-history
 source build/android/envsetup.sh

编译指令

sites.google.com/a/chromium.…

# Build Chromium
gn args out/Debug --list=is_component_build
gn args --list out/Debug    列出所有可选的编译参数。
autoninja -C  out/Debug chrome_public_apk

gn args out/Debug
gn clean out/Debug

gn gen --args='target_os="android" target_cpu="arm" is_debug=true  fatal_linker_warnings=false treat_warnings_as_errors=false' out/Debug

gn gen --args='target_os="android" target_cpu="arm" is_debug=true  fatal_linker_warnings=false treat_warnings_as_errors=false enable_gwp_asan=true' out/webview-Debug

autoninja -C out/Debug chrome_public_apk
autoninja -C out/Debug content_shell_apk

out/Debug/bin/chrome_public_apk install

gn ls out/Debug
autoninja -C out/Debug chrome/test:unit_tests

args.gn

target_os = "android"
target_cpu = "arm"  # (default)
is_debug = true  # (default)
android_ndk_root = "~/dev_tools/Android/android-ndk-r10e"
android_sdk_root = "~/dev_tools/Android/sdk"
android_sdk_build_tools_version = "23.0.2"

android_ndk_root
    Current value (from the default) = "//third_party/android_toolchain/ndk"
      From //build/config/android/config.gni:190
android_ndk_version
    Current value (from the default) = "r25c"
      From //build/config/android/config.gni:191
android_sdk_root
    Current value (from the default) = "//third_party/android_sdk/public"
      From //build/config/android/config.gni:209
android_sdk_version
    Current value (from the default) = "34"
      From //build/config/android/config.gni:210
android_sdk_build_tools_version
    Current value (from the default) = "34.0.0"
      From //build/config/android/config.gni:211
target_os = "android"
target_cpu = "arm"
is_component_build = false
is_java_debug = true
fatal_linker_warnings = false
treat_warnings_as_errors = false
android_static_analysis = "build_server"

android_ndk_major_version = 23
android_ndk_version = "r23c"
android_ndk_root = "~/Android/Sdk/ndk/23.2.8568313"

is_debug: 设置true时编译debug版本,false时编译release版本,默认为false。

is_component_build: 设置true时会把声明为components的目标作为共享库动态加载,一般在编译debug版本时,会设置为true,这样每次改动编译链接花费的时间就会减少很多。若为false,则组件将采用静态链接。

is_official_build:设置true时会使用Chromium官方的编译优化,false时不设置。

out/Release_119_0_6010/bin/content_shell_apk run --args='--disable-fre' 'data:text/html;utf-8,<html>Hello World!</html>'
out/Release_119_0_6010/bin/chrome_public_apk launch --args='--disable-fre' 'data:text/html;utf-8,<html>Hello World!</html>'

常用指令

cd /home/xxx/chromium/src

fetch --nohooks --no-history android
gclient sync --nohooks --no-history
#更新代码
git rebase-update
gclient sync
#编译配置
gn args out/Debug

WebView编译:

# For L+ (21+) devices (if on N-P, see "Important Notes for N-P")
autoninja -C out/Debug system_webview_apk

chromium.googlesource.com/chromium/sr…

发布日志

chromereleases.googleblog.com/

developer.chrome.com/blog/early-…

发布计划

4 周的发布周期

chromiumdash.appspot.com/schedule

AndroidStudio工程配置

不支持编译,仅用来调试代码

chromium.googlesource.com/chromium/sr…

内核调试

adb shell dumpsys activity | grep "mFocusedActivity"
adb shell ps | grep chrome

Debugging Java

# Install, launch, and wait:
out/Debug/bin/chrome_public_apk run --wait-for-java-debugger

# Launch, and have GPU process wait rather than Browser process:
out/Debug/bin/chrome_public_apk launch --wait-for-java-debugger --debug-process-name privileged_process0

# Have Renderers wait:
out/Debug/bin/chrome_public_apk launch --args="--renderer-wait-for-java-debugger"

# Have tests wait:
out/Debug/bin/run_chrome_public_test_apk --wait-for-java-debugger
out/Debug/bin/run_chrome_junit_tests --wait-for-java-debugger  # Specify custom port via --debug-socket=9999

Android Studio

  • Open Android Studio (instructions)

  • Click “Run”->“Attach debugger to Android process” (see here for more).

  • Click “Run”->“Attach to Local Process...” for Robolectric junit tests.

Debugging C/C++

Android NDK r23 LTS(2021 年 8 月)

  • 对 GDB 的支持已结束。GDB 将从下一个版本中移除。请改用 LLDB。请注意,ndk-gdb 默认使用 LLDB。

developer.android.google.cn/ndk/downloa…

out/Debug/bin/content_shell_apk gdb --ide

# Attaches to browser process.
out/Debug/bin/content_shell_apk gdb
out/Debug/bin/chrome_public_apk gdb

# Attaches to gpu process.
out/Debug/bin/chrome_public_apk gdb --debug-process-name privileged_process0

# Attach to other processes ("chrome_public_apk ps" to show pids).
out/Debug/bin/chrome_public_apk gdb --pid $PID

chromium.googlesource.com/chromium/sr…

其它注意事项

编译需要JDK,而且是特定版本的JDK(截至目前要求JDK 8)

sudo update-alternatives --config java  
sudo update-alternatives --config javac
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

sudo update-alternatives --remove python /usr/bin/python3.8

sudo update-alternatives --config python

设置文件夹所有者

root权限 代码会无法下载

sudo chown -R xxx:xxx $your_folder_name 

Webview内核版本对照表

Android 版本WebView 内核版本发布日期
6.0 (API level 23)Chromium 472015-10
7.0 (API level 24)Chromium 532016-08
7.1 (API level 25)Chromium 562016-10
8.0 (API level 26)Chromium 632017-08
8.1 (API level 27)Chromium 682017-12
9.0 (API level 28)Chromium 692018-08
10 (API level 29)Chromium 752019-09
11 (API level 30)Chromium 812020-09
12 (API level 31)Chromium 932021-10
12L (API level 32)Chromium 932022-03
13 (API level 33)Chromium 1012022-08

segmentfault.com/q/101000004…