I want to use StrictMode with .detectAll() and .penaltyDeath() to discover problems in my app. But it's very common, that my app is terminated (because of penaltyDeath) with this message:
D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close
To find the cause, I have removed almost all the code from my app - but without success.
I was surprised to find that I can recreate this problem in the automatically generated project "Basic Activity" from Android Studio.
And now I notice that this problem only occurs in the Android 11 emulator. It does not occur in the Android 8.1 image.
How to reproduce:
In Android Studio: File > New > New Project > Basic Activity > Kotlin + API 21
Add
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build())
}
to the onCreate-method between super.onCreate(savedInstanceState)
and binding = ActivityMainBinding.inflate(layoutInflater)
Add missing imports
Run app
Wait until the app is started/launch in the emulator
Run again
Wait 5 seconds
App crashes with
D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released
...(Sometimes penaltyDeath() kills too fast and the exception is not written to Logcat)
I already:
- recreated and cold booted the emulator
- x86 and x86_64 of Android R are effected
I'm using Linux, Manjaro, Ryzen 7 5800X, 32GB RAM. Does anyone have an idea what I'm doing wrong?
Log from Android Studio (from Run tab) https://pastebin.com/fkq0u1vd