Android StrictMode Compat

Additional

Language
Java
Version
28.1.1 (Mar 9, 2019)
Created
Apr 8, 2017
Updated
Mar 9, 2019
Owner
Kirill Rozov (kirich1409)
Contributor
Kirill Rozov (kirich1409)
1
Activity
Badge
Generate
Download
Source code

Commercial

Android StrictMode Compat

Wrapper of StrictMode API that can be safely called on any version of Android. You must apply version of library for you project base on compileSdkVersion:

android {
    compileSdkVersion 28 // 28 is required minimum
}

dependencies {    
    // With AndroidX
    implementation "com.kirich1409:strict-mode-compat:28.1.0"
    
    // Without AndroidX
    implementation "com.kirich1409:strict-mode-compat:28.0.0"

    // Kotlin Extensions
    implementation "com.kirich1409:strict-mode-compat-kotlin:28.1.1"
}

Sample

build.gradle
android {
    defaultConfig {
        buildConfigField 'boolean', 'DEVELOPER_MODE', 'false'
    }

    buildTypes {
        debug {
            buildConfigField 'boolean', 'DEVELOPER_MODE', 'true'
        }
    }
}
SampleApplication.java
public class SampleApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        if (BuildConfig.DEVELOPER_MODE) {
            StrictMode.ThreadPolicy threadPolicy = new StrictModeCompat.ThreadPolicy.Builder()
                        .detectResourceMismatches()
                        .detectCustomSlowCalls()
                        .detectUnbufferedIo()  // Available only on Android 8.0+
                        .penaltyLog()
                        .build();

            StrictMode.VmPolicy vmPolicy = new StrictModeCompat.VmPolicy.Builder()
                    .detectFileUriExposure()
                    .detectLeakedRegistrationObjects()
                    .detectCleartextNetwork()
                    .detectUntaggedSockets() // Available only on Android 8.0+
                    .detectContentUriWithoutPermission()  // Available only on Android 8.0+
                    .penaltyLog()
                    .build();

            StrictModeCompat.setPolicies(threadPolicy, vmPolicy);
        }
    }
}

Or you can use Kotlin extension and configure StrictModeCompat via DSL

SampleApplication.kt
class SampleApplicationKt : Application() {

    override fun onCreate() {
        super.onCreate()
        initStrictMode(enable = BuildConfig.DEVELOPER_MODE, enableDefaults = false) {
            threadPolicy {
                resourceMismatches = true
                customSlowCalls = true
                unbufferedIo = true

                penalty {
                    log = true
                }
            }

            vmPolicy {
                fileUriExposure = true
                leakedRegistrationObjects = true
                cleartextNetwork = true
                cleartextNetwork = true
                untaggedSockets = true
                contentUriWithoutPermission = true

                penalty {
                    log = true
                }
            }
        }
    }
}

License

Copyright 2017-2019 Kirill Rozov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.