Android WebView instant crash with shouldInterceptRequest override
Asked Answered
G

2

6

I am having some 'fun' with an Android WebView.

I am using it to show a login screen and then intercept the auth code on response. Should be pretty straightforward...

My WebView loads and displays absolutely fine if I only override shouldOverrideUrlLoading but if I override (just as autocompleted by Android Studio):

override fun shouldInterceptRequest(
    view: WebView?,
    request: WebResourceRequest?
): WebResourceResponse {
    return super.shouldInterceptRequest(view, request)
}

with no other changes it crashes immediately at runtime with a native crash

A/chromium: [FATAL:jni_android.cc(259)]

followed by

A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 16220 (TaskSchedulerFo), pid 16175 (eports.internal)

Weirdly if I make the response nullable the WebView works once more. However adding anything else into the shouldInterceptRequest method makes it fall over with the same error.

So this works:

override fun shouldInterceptRequest(
    view: WebView?,
    request: WebResourceRequest?
): WebResourceResponse? {
    return super.shouldInterceptRequest(view, request)
}

But this crashes with the above crash:

override fun shouldInterceptRequest(
    view: WebView?,
    request: WebResourceRequest?
): WebResourceResponse? {
    val url = view?.url
    return super.shouldInterceptRequest(view, request)
}

This seems like a really odd issue, and makes no sense to me why adding a val assignment would make any difference at all.

I have been researching the error and suggestions were to add

webView.destroy()

in the activity/fragments onDestroy/onDestroyView, this does not help unfortunately.

Behaviour is the same on Device and Emulator, and on Android sdk 22 and 28.

Has anyone seen anything like this before? I feel like I am probably missing something obvious.

In case it is useful for anyone I also have the Breakpad Microdump generated also, it is too large to post in this question. But let me know if it, or a subset of it, may help diagnose!

Gliadin answered 16/7, 2018 at 9:6 Comment(2)
This is happening to me too.Burnisher
Wish I could say I know what's up. In my case, I was just logging the url. Both methods, the one with the string and the one with the web resource are causing this if I attempt to log the urlBurnisher
G
6

I have found the issue in my case, so thought I would post it here for anyone else in a similar position.

The Native crash is being caused by Javascript running in the WebView.

WebView runs the JavaScript in a background thread, so anything that touches the UI thread causes it to fall over at the native level. Giving the very unhelpful crash above.

The reason the val assignment was causing the crash wasn't the assigning of the val, but the call if view?.url, which is on the UIThread.

The solution?

Kotlin:

webView?.post{
   // Do your UI work here.
}

Java:

webView.post(new Runnable(){
    public void run() {
    // Do your UI work here.
    }
})

And don't touch UIThread otherwise within the overridden method!

Gliadin answered 17/7, 2018 at 12:52 Comment(1)
i just override the shouldInterceptRequest method and did nothing with and its throwing error without any error logAntislavery
A
1

If you're writing Kotlin code to implement WebViewClient, you should take a look of this answer and check your Kotlin overridden method signature.

I've encountered the same runtime exception and figured out that what caused this issue is Kotlin compiler miss-interoperate Java method's return type to Kotlin method return type.

The method signature of Java code:

    public WebResourceResponse shouldInterceptRequest(WebView view, String url)

which interoperate to Kotlin by override methods auto-completion as:

    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse

The Kotlin method return type is missing ?.
What you have to do is add ? to WebResourceResponse, e.g.,

    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse?
Are answered 8/8, 2019 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.