Question

I do have an old app that refuses to work on Android 4.1 devices. It's the NetworkOnMainThreadException that jumps in here.

So I tried to permit this with the following steps - but these don't work. I tested that with the 4.1 emulator. What is really needed to come around that error - app rewrite is no option. Currently I exclude 4.1 devices from my apps.

A class file ...

public class StrictModeWrapper {

    static {
        try {
            Class.forName("android.os.StrictMode");
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }

    public static void checkAvailable() {
    }

    @SuppressLint("NewApi")
    public static void setThreadPolicy() {
        StrictMode.ThreadPolicy strictModeThreadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(strictModeThreadPolicy);
    }
}

... called in an extended Application class:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            StrictModeWrapper.checkAvailable();
            StrictModeWrapper.setThreadPolicy();
            } catch (Throwable throwable) {
        }
    }
}

The extended Application class is registered in the Manifest and working.

Was it helpful?

Solution 2

StrictMode behaves different on Android version >= 16 than prior releases. The docs suggest to issue StrictMode calls in onCreate() of an extended Application, Activity, etc.. At least onCreate() in an extended Application works different now and proofes the docs wrong (as of today).

Here's the StrictMode doc that describes how to add StrictMode calls to an extended application for example (that's wrong as of today):

StrictMode

Here's a Google Code issue that describes the problem and gives a workaround:

Google Code Issue 35298

OTHER TIPS

Nothing seems to have changed in StrictMode since api 11. It must be the changes in some other android classes you used, that caused a StrictMode policy violation.

The Android documentation itself says

"But don't feel compelled to fix everything that StrictMode finds. "

But since its a NetworkOnMainThreadException you must do a thorough check. See all network communications in your app, and ensure that they are not blocking your main thread.

And make sure you remove/disable the StrictMode code in your release build, as it is only a developer tool to identify accidental mistakes.

Update:

Your app crashed because :

  • You had not blocked the execution of StrictMode policy setting code in your release build. It should be executed only while testing.
  • Something changed in the StrictMode class that caused the strict mode policy to reset after onCreate.

I have 2 questions :

  1. Doesnt the crash indicate that the StrictMode policy was working? There was a policy violation and hence it crashed.
  2. Doesnt it indicate that there is some network code in your app that blocks the main thread?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top