Instantly share code, notes, and snippets.

Embed
What would you like to do?

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