1. 程式人生 > >Android O Chromium 原始碼下載與編譯

Android O Chromium 原始碼下載與編譯

去年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,編譯出來放到板子上跑一切正常。