Android StrictMode for debugging

Google Android provides a very useful tool for developers to see what happened to their Apps. Sometimes, you just forgot to keep heavy loading job off main UI thread, e.g network access, database query, or something you need a lot of time to handle, in these cases, you usually got ANR dialogs to notify you: Your app is going to spend too much time on something in UI thread.

But sometimes, we just ignore these signals and release our App, then you received a lot of complaints from customer emails, comments on Google Play, and weird crash from Crashlyitcs.

So, how do we solve these problems? Any tools can help us to monitor these cases? Yes, we can use StrictMode to check current status in our Apps. StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

StrictMode is most commonly used to catch accidental disk or network access on the application’s main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application’s main thread responsive, you also prevent ANR dialogs from being shown to users.

Here is StrictMode Introduction. Below are exmaple codes for StrictMode.

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());
     }
     super.onCreate();
}

Remember, you should enable that from early in your Application, Activity, or other application component’s onCreate() method, or it would not work.

There are two different types monitoring here, 1) for threads,  2) for VMs. That means you can monitor some heavy loading jobs on UI thread, or check leaks on VMs. Both are really useful. I will suggest you should open two detection policies for both thread and VM. But I need to emphasize that StrictMode is not a security mechanism, it’s just a helper tool. So, I will recommend that you, developers, should combine other tools to check/analyze your Apps.

There are a lot of policies you can use, please refer descriptions here. The most interesting thing is you can send penalty logs to your dropbox account, so, all you need to do is connect to your dropbox account with your phone, and start testing.

Enjoy coding, and have fun!

References
[Google Best Practices]
StrictMode: https://www.youtube.com/watch?v=BxTfwT7mkB4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s