javascriptandroidwebviewgarbage-collectionhybrid-mobile-app

Android WebView JavaScript-Bridge becomes unusable after some time


We developed a hybrid android app. For communication between Webview and native App-Code we set up a Javascript-Bridge via:

webView.addJavascriptInterface(
    WebViewJavaScriptApi(),
    "androidJsBridge"
)

This is working perfectly and we can call native app-code within the WebViewJavaScriptApi. But after some time, some function seems to become uncallable.

I tested this via the chrome remote debugging tools: After loading the webview and calling androidJsBridge['myFunction'] in the console, I'll get "native Code" as return value. Doing this some seconds oder minutes later, I'll get "undefined".

This is happening randomly to any of my methods. Withoud any visible priority or logic.

After hours of debugging we discovered this: Android - Javascript Interface with pointer to WebView - how to achive it without memleak

Garbage Collection could be indeed the problem. Also the problem seems to exist only since some weeks or months. So probably a chrome webview update is doing wrong things?

Does anybody have similar issues? Is it possible to prevent an JS object from being garbage collected?

The methods are not visible in plaintext within my JS-Code. They are called via:

androidJsBridge[functionName](promiseId, params);

where functionName is a string var containing the final method.


Solution

  • Google solved this problem in the meantime. See: https://issuetracker.google.com/issues/185145178?pli=1