Skip to content
inazaruk edited this page Aug 10, 2012 · 31 revisions

robomorphine-strictmode project

This project is created to improve experiences around Android's StrictMode implementation.

What's StrictMode?

StrictMode is Android feature designed to help developers find bugs at runtime. Usually this includes configuring what types of issues developer wants to be reported and how he or she wants them reported (f.e. print to logcat). Usually StrictMode is only enabled in debug configuration of application.

More information is available in documentation and blog post.

What this project is about?

With each new version of Android it becomes harder and harder to configure StrictMode using minimal amount of code. Some extra code is required if developer needs a custom StrictMode configuration which uses new features on newer platforms and gracefully handles missing features on older platforms.

Here is an example of how StrictMode may be configured. This is quite a lot of code. Which needs to go to each project that wants to use it.

Note: example also takes care of the StrictMode bug in Jelly Bean.

Configuring StrictMode

One of the goals of this project is to remove boiler plate code and provide easier way to configure StrictMode.

Here is an example of how StrictMode can be configured using this project's StrictModeHelper class:

if(BuildConfig.DEBUG) {
    StrictModeHelper.setStrictMode(Policy.All.Reset, 
                                   Policy.Thread.DetectAll, 
                                   Policy.Thread.PenaltyDeathOnNetwork,
                                   Policy.Thread.PenaltyFalshScreen,
                                   Policy.Vm.DetectAll,
                                   Policy.Vm.PenaltyLog);
}

This code enables only those features that are supported on current platform and ignores those that are not supported. And it also takes care of StrictMode bug in Jelly Bean mentioned above.

Or here is how default configuration can be used:

if(BuildConfig.DEBUG) {
    StrictModeHelper.enableStrictMode();
}

You can start using StrictModeHelper right away. The jar library can be downloaded from Downloads sections.

Viewing and Analyzing StrictMode Violations

Currently it's only possible to either print violations to LogCat (which usually results in a lot of noise) or print them to DropBox (has nothing to do with DropBox company). It would've been nice to have a place which would collect all violations and allow developer to later analyze those violation. This is what second part of this project is all about.

As part of improving analysis of StrictMode violations this project introduces two application called StrictMode Viewer and StrictMode Violator. The library described in Configuring StrictMode section should be used by 3rd party applications in order to report violations to StrictMode Viewer (otherwise they will not be visible from this application).

Applications can be downloaded from Google Play: StrictMode Viewer, StrictMode Violator.

  • StrictMode Viewer - allows to view violations and gives some stats on them (if applicable).
  • StrictMode Violator - allows to generate violations that can be then viewed by StrictMode Viewer. This application is created to quickly demonstrate what StrictMode Viewer is capable of. It can be used by developers to try features without actually going and re-configuring their applications.

Note that you need to configure your application in a special way in order to use StrictMode Viewer. Here is a detailed tutorial on how to enable StrictMode Viewer for your application.

Here are few screenshots of StrictMode Viewer:

strictmode-viewier-main-screen strictmode-viewier-main-screen-package strictmode-viewier-histogram