Skip to content

Instantly share code, notes, and snippets.

@tw-Frey
Last active November 12, 2019 04:03
Show Gist options
  • Select an option

  • Save tw-Frey/2eb7b7d983027aae9b9b809052e4e2b8 to your computer and use it in GitHub Desktop.

Select an option

Save tw-Frey/2eb7b7d983027aae9b9b809052e4e2b8 to your computer and use it in GitHub Desktop.
部分 Android L 使用 WebView 會 crash 的問題

在最近的項目引用 WebViewSupportFragmentX
結果在 Android L 模擬器上 會 crash
因為該項目使用 Android Studio 3.6-beta 加 Kotlin 1.3.50
以為該不會無法支援 Android L
又以為 WebViewSupportFragmentX 會不會哪裡有錯
後來發現
在直接 new WebView 就會 crash (在 Android L 模擬器上)
錯誤如下

android.content.res.Resources$NotFoundException: String resource ID #0x2040002
        at android.content.res.Resources.getText(Resources.java:274)
        at android.content.res.Resources.getString(Resources.java:360)
        at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:702)
        at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
        at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:619)
        at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:758)
        at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:608)
        at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:546)
        at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:312)
        at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:97)
        at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:264)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:124)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.run(WebViewChromium.java:111)
        at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
        at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:108)
        at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:261)
        at android.webkit.WebView.<init>(WebView.java:548)
        at android.webkit.WebView.<init>(WebView.java:483)
        at android.webkit.WebView.<init>(WebView.java:466)
        at android.webkit.WebView.<init>(WebView.java:453)
        at android.webkit.WebView.<init>(WebView.java:443)
        at asia.fredd.tk3c.androidx.test.MainActivity.onCreate(MainActivity.kt:18)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
@tw-Frey
Copy link
Copy Markdown
Author

tw-Frey commented Oct 30, 2019

以這個項目引發 crash 的原因如下

if you use "androidx.appcompat:appcompat:1.1.0" , try "androidx.appcompat:appcompat:1.0.2" instead. it seems that 1.1.0 dont handle bug with webview in android 5.1.1.
by 戴文錦 (stackoverflow.com)

不過奇怪的是
stackoverflow 的 Question 是 2016 發問的
當時 AndroidX.AppCompat 還沒問世
(androidx.appcompat:appcompat:1.1.0 是 Sep 06, 2019 發布, 參考)

PS: 在 Sep 2019 有人發問 WebView Crash on Android 5-5.1
就是真的跟 androidx.appcompat:appcompat:1.1.0 有關

@tw-Frey
Copy link
Copy Markdown
Author

tw-Frey commented Oct 31, 2019

This is the revision that caused this crash - android.googlesource.com/platform/frameworks/support/+/… . So, according to the code, the crash will occur in API 21..25 in the devices in which Google Play Store is not installed and Android Webview Version is <50.
A R Mythili Saran Sep 29 at 16:56

@tw-Frey
Copy link
Copy Markdown
Author

tw-Frey commented Oct 31, 2019

解決方案 (擇一)

  1. downgrade AndroidX.AppCompat
  2. upgrade Android System WebView (com.android.webview)
  3. replace default system WebView to Google System WebView (com.google.android.webview)

但不想 downgrade AndroidX.AppCompat

@tw-Frey
Copy link
Copy Markdown
Author

tw-Frey commented Oct 31, 2019

2. upgrade Android System WebView (com.android.webview)

搜尋不到 AOSP 版的 Android System WebView (com.android.webview)
都是要自行 build, 但又沒有 key

要更新 Android Emulator 的 Android System WebView
自然得找 AOSP 的 System WebView
後來有找到: Prebuilt AOSP WebView

也搜尋到其他 vender 打包的 Android System WebView (com.android.webview)
推薦 Bromite System WebView

安裝方式有好幾種 (有分 root 跟 非 root)
Bromite 也提幾個方法: Installing SystemWebView


使用過的升級方法 (使用在 Android L Emulator)


發現 Android L Emulator 上的 webview.apk 會與 Prebuilt AOSP WebView 版號 40.0.22xx.xx 的 webview.apk 一致
所以更新至版號 40.0.22xx.xx 可以直接 install 不必 remount/root/-writable-system

但是其他版號的 webview.apk 簽名不一定會跟 Android L Emulator 一致
此時可以用上面的第二個方法
強制刪除後重新安裝新的 AOSP WebView

@tw-Frey
Copy link
Copy Markdown
Author

tw-Frey commented Oct 31, 2019

3. replace default system WebView to Google System WebView (com.google.android.webview)

詳情參考:替换 Webview 預設 Package Name e.g. com.android.webviewcom.google.android.webview


為了測試各版本
Google System WebView 不一定要安裝最新
可以從一些 mirror/archive 網站下載舊版 apk
APKMirror 版本收集比較齊全

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment