Android O Webview 源码下载与编译

去年Android O 发布之后,按照惯例,我需要做一些移植工作,把之前版本加的针对我们公司平台的Pacth合过来,从Android M开始,Chromium source code就需要单独下载和编译,不再包含在Android source code里面。一般下载完Android source code之后,在路径oreo-mstar-master/external/chromium-webview下会有一个README文件,内容如下:大致说明了一些编译参数,以及使用的tag版本,这里说明了是tag 58.0.3029.125。

Building the Chromium-based WebView in AOSP is no longer supported. WebView can
now be built entirely from the Chromium source code.
General instructions for building WebView from Chromium:
https://www.chromium.org/developers/how-tos/build-instructions-android-webview
------
The prebuilt libwebviewchromium.so included in these APKs is built from Chromium

release tag 58.0.3029.125, using the GN build tool. To match our build settings, set:

target_os="android"
is_debug=false
is_official_build=true
is_chrome_branded=false
use_official_google_api_keys=false
exclude_unwind_tables=true
enable_resource_whitelist_generation=true
ffmpeg_branding="Chrome"
proprietary_codecs=true
enable_remoting=true
in your GN argument file before building.
------
Due to WebView API changes in the O release, the Java code in the Chromium 
3029_108 branch is not compatible with O. We'll be working on upstreaming 
the O-specific Java changes to Chromium once the final O SDK is released.
------
For questions about building WebView, please see

https://groups.google.com/a/chromium.org/forum/#!forum/android-webview-dev

原生的webview.apk则位于oreo-mstar-master/external/chromium-webview/prebuilt/下面,这个目录下有几种CPU类型:arm、arm64、mips、x86、x86_64,我用的平台是arm64位的,当我们利用自己下载的Chromium 编译出webview.apk之后,覆盖到对应的目录就可以了,也可以直接安装到平台上验证。

在开发板上烧完Android O的image之后,启动内置的浏览器,从log也可以看到使用的Chromium版本,类似下面这样的log:

cr_LibraryLoader: Expected native library version number "58.0.3029.125", actual native library version number "58.0.3029.125"

所以第一步要做的就是下载Chromium source code(官方教程),这里我主要列出完整的步骤,并简单讲解一下,想要看官方的教程可以点击前面的链接。

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

2、将depot_tools添加到环境变量:export PATH=$PATH:/path/to/depot_tools

3、下载Chromium source code:

~$mkdir oreo_chromium
~$cd oreo_chromium
~/oreo_chromium$ fetch --nohooks android (Chromium checkout for Android platform)
~/oreo_chromium$ gclient sync --with_branch_heads -r 58.0.3029.125 (切到Android O 的tag)
~/oreo_chromium$ cd src
~/oreo_chromium/src# ./build/install-build-deps-android.sh (root权限,安装Android相关的编译依赖)

~/oreo_chromium/src$ gclient runhooks(下载编译需要的一些文件)

下载过程会比较久,整包code大概有30G左右,编译完大概是40G。Chromium的编译工具有主要有两种:GPY和GN,现在都是推荐使用GN编译,这里也只介绍GN的编译流程:

1、创建编译目录:gn gen out/Release_arm64

2、设置编译参数:gn args out/Release_arm64

执行这步会直接进入vi,将前面README的参数复制进去,然后还要加一个target_cpu="arm64"指定CPU类型,如下所示:

target_os="android"
target_cpu="arm64"
is_debug=false
is_official_build=true
is_chrome_branded=false
use_official_google_api_keys=false
exclude_unwind_tables=true
enable_resource_whitelist_generation=true
ffmpeg_branding="Chrome"
proprietary_codecs=true
enable_remoting=true

3、编译webview.apk:ninja -C out/Release_arm64 system_webview_apk

编译过程也比较久,看服务器性能吧,一般三四个小时还是要的。

编译完后,在oreo_chromium/src/out/Release_arm64/apks目录下会生成一个SystemWebView.apk,可以直接安装到板子上测试,安装之前需要先卸载系统自带的webview,用pm uninstall com.android.webview 是卸载不了的,需要到板子上的目录/system/app/webview下,把原本的webview.apk删掉或重命名,然后重启板子,这样就自动卸载了,然后再安装:pm install  -r /mnt/usb/190C-3332/o/SystemWebView.apk,安装成功之后,发现启动浏览器直接闪退,打印了下面的log:

WebViewFactory: Chromium WebView package does not exist
WebViewFactory: android.webkit.WebViewFactory$MissingWebViewPackageException: Failed to load WebView provider: No WebView installed
WebViewFactory:        at android.webkit.WebViewFactory.getWebViewContextAndSetProvider(WebViewFactory.java:334)
WebViewFactory:        at android.webkit.WebViewFactory.getProviderClass(WebViewFactory.java:398)

前面安装是确定有安装成功的,但是还是提示No WebView installed,从这个异常看是没有找到webview,查看相关的code,WebViewFactory.java,发现有一下定义:

private static final String CHROMIUM_WEBVIEW_FACTORY =

            "com.android.webview.chromium.WebViewChromiumFactoryProviderForO";

查看下载下来的Chromium的code,却发现没有这个类,目录oreo_chromium/src/third_party/android_tools/sdk下可以看到依赖的frameworks和sdk版本都是Android N的,这说明下载下来的这包Chromium是有问题的,分析到这里第一个想法是对比Chromium Source的git log,将Android O相关的code 合过来,这里再推荐一个工具,可以在线查看chromium的source code,但是在合code的过程中发现这些commit相互之间的依赖太多了,合完一笔build fail,为了解决build fail发现又要合新的commit,有些commit甚至是当天才上的,而且反复revert和reland,似乎google还没有完全整合好。

查看之前的sync code记录,发现log中也有出现一个错误,但又不影响编译,带着这些疑问到Chromium论坛发帖求助,一般隔天就有回复。

[0:03:45] fatal: unable to access 'https://chromium.googlesource.comnative_client/pnacl-subzero/': Could not resolve host: chromium.googlesource.comnative_client

Chromium的答复如下,想要兼容Android O,需要tag 62.0.3200.0 ,并且还不是稳定版本。。。这个说法其实很奇怪,Android O发布出来的版本,自带的webview tag就是58.0.3029.125,并且Google SRI项目使用的也是这个版本,有点搞不懂他们。。


在等了一个多月之后,终于等到了兼容Android O的稳定版本,这里也提供一个链接,可以查看版本发布情况,最终我使用的版本是62.0.3202.73,编译出来放到板子上跑一切正常。






猜你喜欢

转载自blog.csdn.net/a957666743/article/details/80061159