From 7fc9011d783141b9bc5ab71bb42d5f8e1a3e57f8 Mon Sep 17 00:00:00 2001 From: Antoine Jaury Date: Tue, 7 Jul 2026 21:41:03 +0200 Subject: [PATCH] new: enable StrictMode in debug builds --- androidApp/README.md | 15 ++++++++ .../kotlin/bzh/ajaury/chombev/ChomBevApp.kt | 36 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/androidApp/README.md b/androidApp/README.md index eba9f33..fe30b45 100644 --- a/androidApp/README.md +++ b/androidApp/README.md @@ -5,6 +5,21 @@ GlitchTip is configured in the project. It uses the open source solution Senstry to log crashes generated by the app and send them to GlitchTip on a European server. +## StrictMode (debug builds) + +The app enables Android [StrictMode](https://developer.android.com/reference/android/os/StrictMode) +**only for debuggable builds** (gated on debug versions, so release builds are never affected). + +Both the thread policy (disk/network on the main thread, etc.) and the VM policy (leaked +`Closeable`s, activity leaks, etc.) are enabled and logged to Logcat. + +To see violations, filter Logcat for the `StrictMode` tag while running a debug build: + +```bash +./gradlew :androidApp:installDebug +adb logcat -s StrictMode +``` + ## Signing Release builds of the Android app are signed with a keystore whose credentials diff --git a/androidApp/src/main/kotlin/bzh/ajaury/chombev/ChomBevApp.kt b/androidApp/src/main/kotlin/bzh/ajaury/chombev/ChomBevApp.kt index a60ff65..8dca7e3 100644 --- a/androidApp/src/main/kotlin/bzh/ajaury/chombev/ChomBevApp.kt +++ b/androidApp/src/main/kotlin/bzh/ajaury/chombev/ChomBevApp.kt @@ -1,5 +1,39 @@ package bzh.ajaury.chombev import android.app.Application +import android.content.pm.ApplicationInfo +import android.os.StrictMode -class ChomBevApp : Application() +class ChomBevApp : Application() { + override fun onCreate() { + super.onCreate() + if (isDebuggable()) { + enableStrictMode() + } + } + + private fun isDebuggable(): Boolean = + applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 + + /** + * Enables StrictMode for debug builds only. All violations are logged + * ([StrictMode.ThreadPolicy.Builder.penaltyLog]) so problems surface in Logcat + * without ever crashing the app (no `penaltyDeath`). + */ + private fun enableStrictMode() { + StrictMode.setThreadPolicy( + StrictMode.ThreadPolicy + .Builder() + .detectAll() + .penaltyLog() + .build(), + ) + StrictMode.setVmPolicy( + StrictMode.VmPolicy + .Builder() + .detectAll() + .penaltyLog() + .build(), + ) + } +}