Android StrictMode for all platform versions

With the release of the Android platform 2.3 aka Gingerbread a while ago us developers got a great tools into our hands: StrictMode. Unfortunately if you application still needs to be run on on older versions of Android you are a bit out of luck. Until now at least because I will show you how to use StrictMode in development even if your app e.g. supports Android 1.6 and up.

StrictMode is a great tool for finding potential performance problems in your Android application, nudging you to get that multi-threaded programming happening and getting of the UI thread with any longer running operations. As a first step to play with StrictMode I just made a git branch of my project and upgraded to api level 9 and enjoyed the great feedback in logcat while running my app. Problem was I was hooked and wanted to have this feedback all the time. So I needed to figure out a way to get it happening in the main branch and I do not have the option to upgrade it to Android 2.3. So what next? Well after asking a question on stackoverflow with no solution I decided to take matters into my own hands and with a bit of reading up on the net I came up with this:

First of all I implemented a wrapper class for StrictMode like so

public class StrictModeWrapper {
   static {
       try {
           Class.forName("android.os.StrictMode", true, Thread.currentThread().getContextClassLoader());
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       }
   }

   public static void checkAvailable() {}

   public static void enableDefaults() {
       StrictMode.enableDefaults();
    }

This class will check for the availability of the StrictMode class at runtime and class loading will fail and throw an exception if no StrictMode is available. If it however succeeds it provides wrappers to the StrictMode methods we want to call. In this simple example here we just want to call the static method enableDefaults().

To actually use it and activate StrictMode for the whole application I added this to my application class.

public class MorseFlashApplication extends Application {
    private static boolean strictModeAvailable;

    // use the StrictModeWrapper to see if we are running on Android 2.3 or higher and StrictMode is available
    static {
        try {
            StrictModeWrapper.checkAvailable();
            strictModeAvailable = true;
        } catch (Throwable throwable) {
            strictModeAvailable = false;
        }
    }

    @Override
    public void onCreate() {
        if (strictModeAvailable) {
            // check if android:debuggable is set to true
            int applicationFlags = getApplicationInfo().flags;
            if ((applicationFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
                StrictModeWrapper.enableDefaults();
            }
        }
        super.onCreate();
    }

Looking at the code you can see that on creation of the application we check for availability of StrictMode with the help of our StrictModeWrapper and then we activate StrictMode by enabling defaults via the wrapper class. We only do this if the android:debuggable is set to true in the AndroidManifest.xml file, which is automatically done by the build depending on environment.

That was already it. Together with some Maven build related setup that ensures that android:debuggable is set to true in development, but to false in production, we are ready to have no more long running operations on the UI thread. We will notice it all in development by looking at logcat output and without having to change anything it will work fine in production as well as on older platforms where no StrictMode is available yet. If you like this all feel free to have a look at the implementation in the MorseFlash sample in my github clone of the Maven Android Plugin Samples project.

I hope you find this useful and join me for tips like this and other cool things at my classes 306 and 406 at AnDevCon in March in San Francisco.

See you there.

Manfred

3 comments » Write a comment

  1. Pingback: Tweets that mention Android StrictMode for all platform versions | simpligility -- Topsy.com

  2. Pingback: Version-tolerant StrictMode wrapper for Android | Rolling with the changes…

Leave a Reply

Required fields are marked *.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>