Skip to content

Instantly share code, notes, and snippets.

@houssemzaier
Created February 9, 2020 22:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save houssemzaier/cf69f9a614f9550408872349e5798e0b to your computer and use it in GitHub Desktop.
Save houssemzaier/cf69f9a614f9550408872349e5798e0b to your computer and use it in GitHub Desktop.
This solution gives the possibility to just hide the "android.os.strictmode.UntaggedSocketViolation" or just log it in a verbose lever, which I prefer.
@RequiresApi(Build.VERSION_CODES.P)
private fun enabledStrictMode() {
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectAllExpect("android.os.StrictMode.onUntaggedSocket")
.build())
}
@RequiresApi(Build.VERSION_CODES.P)
fun StrictMode.VmPolicy.Builder.detectAllExpect(ignoredViolationPackageName: String, justVerbose: Boolean = true): StrictMode.VmPolicy.Builder {
return detectAll()
.penaltyListener(Executors.newSingleThreadExecutor(), StrictMode.OnVmViolationListener
{
it.filter(ignoredViolationPackageName, justVerbose)
})
}
@RequiresApi(Build.VERSION_CODES.P)
private fun Violation.filter(ignoredViolationPackageName: String, justVerbose: Boolean) {
val violationPackageName = stackTrace[0].className
if (violationPackageName != ignoredViolationPackageName && justVerbose) {
Timber.v(this)
}
}
@nhoxbypass
Copy link

This solution does not "hide" the violation. It's only attaches the listener then prints another console log.

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