Skip to content

Commit e803562

Browse files
Enable the thread police 👮 for debug builds
* App will now crash if someone does funky stuff on the main thread. Change-Id: I9026c100705f2fec6963a1d888b40906186f9d28
1 parent 4a07234 commit e803562

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

app/src/main/kotlin/com/google/samples/apps/nowinandroid/NiaApplication.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.samples.apps.nowinandroid
1818

1919
import android.app.Application
20+
import android.content.pm.ApplicationInfo
21+
import android.os.StrictMode
2022
import coil.ImageLoader
2123
import coil.ImageLoaderFactory
2224
import com.google.samples.apps.nowinandroid.sync.initializers.Sync
@@ -37,10 +39,25 @@ class NiaApplication : Application(), ImageLoaderFactory {
3739

3840
override fun onCreate() {
3941
super.onCreate()
42+
43+
// Kill NiA if there are main thread policy violations and log the offending call.
44+
if (isDebuggable()) {
45+
StrictMode.setThreadPolicy(
46+
StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build(),
47+
)
48+
}
49+
4050
// Initialize Sync; the system responsible for keeping data in the app up to date.
4151
Sync.initialize(context = this)
4252
profileVerifierLogger()
4353
}
4454

4555
override fun newImageLoader(): ImageLoader = imageLoader.get()
56+
57+
/**
58+
* Check if the application is debuggable.
59+
*/
60+
private fun isDebuggable(): Boolean {
61+
return 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
62+
}
4663
}

0 commit comments

Comments
 (0)