Search code examples
androidperformancewebviewandroid-strictmode

Android WebView causes StrictMode violation


I'm noticing that StrictMode is complaining quite loudly about WebView creation. Is there any way to improve the startup performance of a WebView object? I'm instantiating from XML in my onCreate() method.

webView = (WebView) findViewById(R.id.webview);
webView.clearCache(true);
webView.getSettings().setDefaultFontSize(20);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());

WebSettings wb = webView.getSettings();
wb.setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setWebChromeClient(new MyWebChromeClient(this));

Here is the first of many StrictMode warnings:

D/StrictMode(22781): StrictMode policy violation; ~duration=1869 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
D/StrictMode(22781):    at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:745)
D/StrictMode(22781):    at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:106)
D/StrictMode(22781):    at android.database.sqlite.SQLiteDatabase.getVersion(SQLiteDatabase.java:928)
D/StrictMode(22781):    at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:196)
D/StrictMode(22781):    at android.webkit.WebView.<init>(WebView.java:1002)
D/StrictMode(22781):    at android.webkit.WebView.<init>(WebView.java:979)
D/StrictMode(22781):    at android.webkit.WebView.<init>(WebView.java:969)
D/StrictMode(22781):    at java.lang.reflect.Constructor.constructNative(Native Method)
D/StrictMode(22781):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
D/StrictMode(22781):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
D/StrictMode(22781):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
D/StrictMode(22781):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
D/StrictMode(22781):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
D/StrictMode(22781):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
D/StrictMode(22781):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
D/StrictMode(22781):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
D/StrictMode(22781):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
D/StrictMode(22781):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:214)
D/StrictMode(22781):    at android.app.Activity.setContentView(Activity.java:1660)
D/StrictMode(22781):    at com.myapplication.StartActivity.onCreate(StartActivity.java:886)

Solution

  • Is there any way to improve the startup performance of a WebView object?

    Not with respect to that specific StrictMode complaint, since it is coming from the WebView constructor.

    In general, there is nothing you can do about WebView triggering StrictMode -- since it is a widget, it may not be safe for you to do much with the WebView on a background thread, particularly once it is part of the view hierarchy.

    You might scan http://b.android.com to see if there are existing bug reports about this, and if not, create a sample project that generates the message and file it along with a bug report.