Android WebView causes StrictMode violation
Asked Answered
A

3

5

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)
Athanasius answered 26/10, 2011 at 3:45 Comment(1)
Similar problem when creating a WebView in code: new WebView(getActivity().getApplicationContext());. It triggers Read/Write disk violations.Kale
P
6

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.

Promotive answered 26/10, 2011 at 6:39 Comment(4)
The bug has been reported: code.google.com/p/android-developer-preview/issues/…Meri
@MilošČernilovský: Yes. It was reported by me.Promotive
@Promotive the link's dead, why did you report in the preview and not the main android repo?Aleksandr
@TWiStErRob: Probably because non-preview framework issues get ignored, in general. For the M Developer Preview (and perhaps L), they used a separate issue tracker. which is why that link broke. This year's N Developer Preview used labels in the main issue tracker, which will help, until such time as they discontinue that issue tracker (since it's part of the defunct Google Code service).Promotive
V
3

A solution that worked for me (Android 5.0) was to create a dummy WebView before setting StrictMode:

…
new WebView(this);

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build());
…
Vagal answered 19/10, 2016 at 16:23 Comment(1)
In kotlin: StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build()) StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build())Tuchman
A
1

If you're bothered by the StrictMode violation or it crashes your app you can use StrictMode.allowThreadDisk*():

ThreadPolicy realPolicy = StrictMode.allowThreadDiskReads();
try {
    web = new WebView(container.getContext());
} finally {
    StrictMode.setThreadPolicy(realPolicy);
}

But, sadly allowing reads is not enough:

D/StrictMode: StrictMode policy violation; ~duration=546 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=245 violation=1
    at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1755)
    at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:243)
    at android.webkit.WebView.<init>(WebView.java:981)
    at android.webkit.WebView.<init>(WebView.java:958)
    at android.webkit.WebView.<init>(WebView.java:948)
    at android.webkit.WebView.<init>(WebView.java:939)
    at MyFragment.onCreateView(CategoryHelpFragment.java:41)

So the final workaround is:

ThreadPolicy realPolicy = StrictMode.allowThreadDiskWrites();
try { // in older versions of Android WebViewDatabase.getInstance creates/opens a DB and StrictMode complains
    web = new WebView(container.getContext());
} finally {
    StrictMode.setThreadPolicy(realPolicy);
}

I used manual WebView creation, but you can just put this around setContentView.

Aleksandr answered 2/7, 2016 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.