WebView cannot find objects opened via addJavascriptinterface in Lollipop

I've struggled with this for too long. Hope someone can help me. Here is my situation:

In our Android app, we are sending XMLHttpRequests from our WebView via Java / Android so that we can add an OAuth2 token and track requests. This worked great until Android 5 was released.

Since Android 5, whenever a WebView tries to interact with an open object in JavaScript, the following exception is thrown:

java.lang.ClassNotFoundException: Didn't find class "com/company/android/AndroidXMLHttpRequest$AjaxRequest" on path: DexPathList[[zip file "/system/app/webview/webview.apk"],nativeLibraryDirectories=[/system/app/webview/lib/x86_64, /vendor/lib64, /system/lib64]]
  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
  at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:53)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:135)
  at android.os.HandlerThread.run(HandlerThread.java:61)
  Suppressed: java.lang.ClassNotFoundException: Invalid name: com/company/android/AndroidXMLHttpRequest$AjaxRequest
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
    ... 6 more

      

Results for different configurations:

  • This code works fine on physical and virtual devices running Android <5.
  • This code breaks down on physical devices running Android 5 and 5.1
  • This code works fine on a virtual device running Android 5.0
  • This code breaks down to virtual devices running Android 5.1
  • There is no difference between running debug or production build

Some information about the code: * WebViewFragment runs in MainActivity.onCreate

. * WebViewFragment.onCreateView

creates an object that will be displayed in the WebView * JavaScript interface added to WebViewClient.onPageFinished

 * The class that is exposed is Public * All methods of the public class are public and annotated@JavascriptInterface

Launching Lollipop, WebView has been disconnected from Android, so it can be updated via the Play Store. This also indicates an error:

DexPathList[[zip file "/system/app/webview/webview.apk"],nativeLibraryDirectories=[/system/app/webview/lib/x86_64, /vendor/lib64, /system/lib64]]

      

I have looked through the release notes to see if we need to do something about the WebView and addJavaScriptInterface, but I cannot find anything.

Anyone have any ideas what might be causing it to fail?

+3


source to share


1 answer


Sorry for that, there was an error in the WebView: http://crbug.com/491800

The bug appeared in Android 5.1, so the original 5.0 works fine (emulator). But on physical Lollipop devices with the Play Store, the WebView is updated to a more recent version (43) which was still broken. The fix worked with WebView version 44, which I think is rolling out now.



However, there is a workaround. In the injected method, which takes an instance AjaxRequest

, you need to replace the argument type with Object

and then convert to AjaxRequest

later in a function (see this comment for an example). This way, the WebView will stop trying to find a class that it cannot access at this point.

+2


source







All Articles