StrictMode policy violation: android.os.StrictMode$StrictModeDiskReadViolation: policy=327711 violation=2 at Application super.onCreate

∥☆過路亽.° 提交于 2021-02-19 03:27:05

问题


When I turn on the strict mode detect All, my App crashes super.onCreate() of application (i.e. before even I have any of my code doing anything).

My application onCreate turning on the strict mode as below

override fun onCreate() {
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(
                StrictMode.ThreadPolicy.Builder()
                        .detectAll()
                        .penaltyLog()
                        .penaltyDeath().build())
        StrictMode.setVmPolicy(
                StrictMode.VmPolicy.Builder()
                        .detectAll()
                        .penaltyLog()
                        .penaltyDeath().build())
    }
    super.onCreate()

    // Some other code
}

The error I got (which is on the line of super.onCreate())

D/StrictMode: StrictMode policy violation; ~duration=98 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=327711 violation=2
                                                             at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1263)
                                                             at libcore.io.BlockGuardOs.open(BlockGuardOs.java:182)
                                                             at libcore.io.IoBridge.open(IoBridge.java:438)
                                                             at java.io.FileInputStream.<init>(FileInputStream.java:76)
                                                             at android.graphics.Typeface.getFullFlipFont(Typeface.java:584)
                                                             at android.graphics.Typeface.getFontPathFlipFont(Typeface.java:532)
                                                             at android.graphics.Typeface.SetFlipFonts(Typeface.java:719)
                                                             at android.graphics.Typeface.SetAppTypeFace(Typeface.java:846)
                                                             at android.app.Application.onCreate(Application.java:110)
                                                             at com.mypackage.MyApplication.onCreate(MyApplication.kt:40)

Is this an expected error we should ignore, or is this something that we should fix?


回答1:


Apparently the error are device specific. It happens on Samsung S7, but not Nexus 6P. Hence it's not something I would be fixing. Hence the best way to suppress it.

The below is the example how I got it suppressed. You could wrap those functions into an Util Class.

override fun onCreate() {
    turnOnStrictMode()
    permitDiskReads{
        super.onCreate()
    }

    // Some other code
}


fun turnOnStrictMode() {
    if (BuildConfig.DEBUG) {
        StrictMode.setThreadPolicy(
                StrictMode.ThreadPolicy.Builder()
                        .detectAll()
                        .penaltyLog()
                        .penaltyDeath().build())
        StrictMode.setVmPolicy(
                StrictMode.VmPolicy.Builder()
                        .detectAll()
                        .penaltyLog()
                        .penaltyDeath().build())
    }
}

fun permitDiskReads(func: () -> Any) : Any {
    if (BuildConfig.DEBUG) {
        val oldThreadPolicy = StrictMode.getThreadPolicy()
        StrictMode.setThreadPolicy(
                StrictMode.ThreadPolicy.Builder(oldThreadPolicy)
                .permitDiskReads().build())
        val anyValue = func()
        StrictMode.setThreadPolicy(oldThreadPolicy)

        return anyValue
    } else {
        return func()
    }
}

Refers to this for more detail.




回答2:


Move the statement 'super.onCreate()' before the StrictMode methods in fun onCreate(). Check this article for details. Walk through hell with Android StrictMode




回答3:


Here is my Java version

public class StrictModeManager {

private static boolean enabled = false;

private StrictModeManager() {}

public static void enableStrictMode() {
    enabled = true;
    StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork()
            .penaltyLog()
            .penaltyDropBox();

    StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects()
            .detectLeakedRegistrationObjects()
            .penaltyLog()
            .penaltyDropBox()
            .penaltyDeath();

    StrictMode.setThreadPolicy(threadPolicyBuilder.build());
    StrictMode.setVmPolicy(vmPolicyBuilder.build());

}

public static void allowDiskReads(Runnable runnable) {
    StrictMode.ThreadPolicy oldThreadPolicy = null;
    if (enabled) {
        oldThreadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(oldThreadPolicy).permitDiskReads().build());
    }
    runnable.run();
    if (oldThreadPolicy != null) StrictMode.setThreadPolicy(oldThreadPolicy);
   }
}

And to use it:

@Override
public void onCreate() {
    if (BuildConfig.DEBUG) StrictModeManager.enableStrictMode();
    StrictModeManager.allowDiskReads(App.super::onCreate);

    // Code here
}


来源:https://stackoverflow.com/questions/38427478/strictmode-policy-violation-android-os-strictmodestrictmodediskreadviolation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!