Skip to content

Instantly share code, notes, and snippets.

@miensol
Last active May 6, 2019 06:27
Show Gist options
  • Save miensol/c4b5999ba1d21741d150a58ed4d257a0 to your computer and use it in GitHub Desktop.
Save miensol/c4b5999ba1d21741d150a58ed4d257a0 to your computer and use it in GitHub Desktop.
Override android strict mode policy when in android tests to allow calling network on main thread.Useful when you want to access mock web server address and you don't care about the threading.
import android.os.StrictMode
fun <T> withStrictModeThreadPolicy(threadPolicy: StrictMode.ThreadPolicy, block: () -> T): T {
val previousPolicy = StrictMode.getThreadPolicy()
StrictMode.setThreadPolicy(threadPolicy)
try {
return block()
} finally {
StrictMode.setThreadPolicy(previousPolicy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment