Skip to content

Instantly share code, notes, and snippets.

@fredgrott
Last active October 1, 2016 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredgrott/d075ca97be9c2c3c579919a5a24cf0d2 to your computer and use it in GitHub Desktop.
Save fredgrott/d075ca97be9c2c3c579919a5a24cf0d2 to your computer and use it in GitHub Desktop.

Apache 2.0 License Copyright(C) Fred Grott(GrottWorkSHop)

This is my strict mode stetup...to be effective you combine this with obviously having some gradle product flavors for each api levlel you are targeting

@SuppressLint("NewApi")
  public void strictModeSetUp(){
    if(BuildConfig.DEBUG){
      if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .detectCustomSlowCalls()
                .penaltyLog()
                .build());
      }else{
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .detectCustomSlowCalls()
                .detectResourceMismatches()
                .penaltyLog()
                .build());
      }
      if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .detectLeakedSqlLiteObjects()
                .detectLeakedRegistrationObjects()
                .detectActivityLeaks()
                .penaltyLog()
                .penaltyDeath()
                .build());
      }if(Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2){
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .detectLeakedSqlLiteObjects()
                .detectLeakedRegistrationObjects()
                .detectActivityLeaks()
                .detectFileUriExposure()
                .penaltyLog()
                .penaltyDeath()
                .build());
      }if(Build.VERSION.SDK_INT> Build.VERSION_CODES.JELLY_BEAN_MR2 || Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .detectLeakedSqlLiteObjects()
                .detectLeakedRegistrationObjects()
                .detectActivityLeaks()
                .detectFileUriExposure()
                .penaltyLog()
                .penaltyDeath()
                .build());
      }if(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1 || Build.VERSION.SDK_INT<Build.VERSION_CODES.N){
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .detectLeakedSqlLiteObjects()
                .detectLeakedRegistrationObjects()
                .detectActivityLeaks()
                .detectFileUriExposure()
                .detectCleartextNetwork()
                .penaltyLog()
                .penaltyDeath()
                .build());
      }if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .detectLeakedSqlLiteObjects()
                .detectLeakedRegistrationObjects()
                .detectActivityLeaks()
                .detectFileUriExposure()
                .detectCleartextNetwork()
                .penaltyLog()
                .penaltyDeath()
                .build());
      }
    }




  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment