Search code examples
androidwebviewwebkitandroid-webviewandroid-for-work

Android O failing to inflate webview after AFW enrollment


I'm using the latest Android O beta version in a Pixel XL device. This is my webview in xml.

<WebView
    android:id="@+id/launch_anim"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_centerHorizontal="true" />

This is how its used in my Activity class

WebView launch_anim = (WebView)findViewById(R.id.launch_anim);
launch_anim.loadUrl("file:///android_asset/launch_anim_centered.html");

This is my launch_anim_centered.html file in assets dir

<HTML>
<head>
    <style>
body {
    background-color: #FFFFFF;
}
</style>
</head>
<body>
<CENTER>
    <IMG SRC="launch_anim_1x.gif">
</CENTER>
</body>
</HTML>

The launch_anim_1x.gif file is a gif file containing an image which is rotating. When I start the application, there is no problem in running of this activity. Then I do Android-For-Work enrollment. Post enrollment, as soon as app is launched, this activity fails to render and it throws an exception like this

07-28 15:39:55.352 16538-16538/? I/WebViewFactory: Loading com.google.android.webview version 58.0.3029.125 (code 303012500)
07-28 15:39:55.353 16538-16538/? D/AndroidRuntime: Shutting down VM
07-28 15:39:55.354 16538-16538/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.sample.app, PID: 16538
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.app/com.sample.app.ui.StartupActivity}: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class android.webkit.WebView
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                    Caused by: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class android.webkit.WebView
                                                    Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class android.webkit.WebView
                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                       at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                       at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                                                       at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                       at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                       at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:418)
                                                       at android.app.Activity.setContentView(Activity.java:2654)
                                                       at com.sample.app.ui.StartupActivity.onCreate(Unknown Source:37)
                                                       at android.app.Activity.performCreate(Activity.java:6975)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                    Caused by: java.lang.NullPointerException
                                                       at android.content.res.AssetManager.addAssetPathNative(Native Method)
                                                       at android.content.res.AssetManager.addAssetPathInternal(AssetManager.java:689)
                                                       at android.content.res.AssetManager.addAssetPathAsSharedLibrary(AssetManager.java:684)
                                                       at android.webkit.WebViewFactory.getProviderClass(WebViewFactory.java:407)
                                                       at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:211)
                                                       at android.webkit.WebView.getFactory(WebView.java:2467)
                                                       at android.webkit.WebView.ensureProviderCreated(WebView.java:2462)
                                                       at android.webkit.WebView.setOverScrollMode(WebView.java:2527)
                                                       at android.view.View.<init>(View.java:4536)
                                                       at android.view.View.<init>(View.java:4668)
                                                       at android.view.ViewGroup.<init>(ViewGroup.java:597)
                                                       at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:55)
07-28 15:39:55.354 16538-16538/? E/AndroidRuntime:     at android.webkit.WebView.<init>(WebView.java:636)
                                                       at android.webkit.WebView.<init>(WebView.java:581)
                                                       at android.webkit.WebView.<init>(WebView.java:564)
                                                       at android.webkit.WebView.<init>(WebView.java:551)
                                                        ... 27 more

This issue is only seen in Pixel and PixelXL devices. Nexus device works fine. Any help in resolving this issue is appreciated. Thanks EDIT: New info. So I disabled the webview to see what happens but then the app crashes when trying to access CookieManager.getInstance(). This is the stacktrace:

07-31 09:21:51.767 11267-11557/com.airwatch.vmworkspace I/WebViewFactory: Loading com.google.android.webview version 58.0.3029.125 (code 303012500)
07-31 09:21:51.775 13243-13276/? I/ConfigUpdater: Update started

                                                  --------- beginning of crash
07-31 09:21:51.775 11267-11557/com.airwatch.vmworkspace E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                                                                          Process: com.airwatch.vmworkspace, PID: 11267
                                                                          java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                              at android.os.AsyncTask$3.done(AsyncTask.java:353)
                                                                              at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
                                                                              at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:271)
                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                              at java.lang.Thread.run(Thread.java:764)
                                                                           Caused by: java.lang.NullPointerException
                                                                              at android.content.res.AssetManager.addAssetPathNative(Native Method)
                                                                              at android.content.res.AssetManager.addAssetPathInternal(AssetManager.java:689)
                                                                              at android.content.res.AssetManager.addAssetPathAsSharedLibrary(AssetManager.java:684)
                                                                              at android.webkit.WebViewFactory.getProviderClass(WebViewFactory.java:407)
                                                                              at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:211)
                                                                              at android.webkit.CookieManager.getInstance(CookieManager.java:39)
                                                                              at com.sample.app.network.communication.MyCookieManager.<init>(MyCookieManager.java:48)

I think the real problem is NullPointerException on android.content.res.AssetManager.addAssetPathNative(Native Method). Any idea how to fix this?


Solution

  • This is a bug in Android O: if Chrome is disabled in one of the profiles then WebView is broken for both profiles until it's updated by Play.

    To fix this issue during provisioning and avoid having to update WebView, you can enable Chrome in the work profile using DevicePolicyManager.enableSystemApp() with the package name com.android.chrome, and then prevent the user from disabling it using DevicePolicyManager.setUninstallBlocked().

    If you then want to prevent the user from using the Chrome browser, you should use DevicePolicyManager.setPackagesSuspended() instead of DevicePolicyManager.setApplicationHidden().