Debugging Android process death

Learn this now or spend šŸ¤·ā€ā™‚ļø in Crashlytics later

Eric Silverberg
Perry Street Software Engineering
4 min readMar 12, 2021

--

For a class we didnā€™t even write, Parcel sure seems to have some problemsā€¦Photo by Markus Winkler on Unsplash

In this series, we spoke at length about how Android process death can trigger crashing on apps that do not follow a Clean MVVM Application lifecycle pattern. In order to debug Android process death, you need to first trigger a process death, and then you need to restore your application to see how it behaves.

Step 1: Trigger process death

Method A: Use adb

Put your app into the background by pressing the home button

Kill the app using adb:

adb shell am kill <YOUR APPLICATION ID>

ā— DONā€™T use the ā€œStopā€ button of Android Studio to kill the process. This will not simulate a process death and it will falsely make you believe that your app is protected. This will indeed kill the app, but next time you launch it, it will go through the normal flow of the LAUNCHER Activity, instead of re-creating the Activity you were in before moving the app to the background.

Method B: Do it naturally

You can also use the following technique if you donā€™t have adb access for some reason:

From Developer Options, set the Background process limit to No background processes

Move the app to the background by pressing the home button

Launch a few other apps, like Phone or Messages or Chrome.

Tip: Use the Android Studioā€™s ā€œAttach Debugger to Android Processā€ window to know when your app has actually been killed.

Step 2: Restore your app and see how it behaves

When the process has been killed, use the square ā€œapp switcherā€ aka Recents screen to navigate back into your app. This will cause the OS to try to resume your app directly from the last screen it was on.

A square icon on newer versions of Android brings up the Recents screen, aka App Switcher
After you tap on your app from the Recents screen, your app launches

Additional tips

If there is a crash with a small amount of logs in Crashlytics or if you see logs for an Activity getting created without seeing the launch activity of your app being created, then if itā€™s not a deep link or a notification, itā€™s almost certainly a process death scenario that you can reproduce by following the steps above.

Debugging the back stack chain

Thereā€™s an additional tricky part to debugging android process death:

Only the Activity that you were in will be recreated once you foreground the app.

That means that every time you tap the Back arrow, the parent activity will be picked up from the back stack and get recreated as well. So essentially, all Activities in the back stack will experience restoration from a process death and the state of the Application could vary, depending on the operations that the recreated activities have performed (i.e. whether they have modified any application-scoped data).

When debugging process death for Activity A, try doing so both by backgrounding Activity A itself, and from child Activity B + pressing Back to return to Activity A

A note about fragments

When an Activity creates a Fragment, and you background the app while the Fragment is visible, then when the Activity is restored after a process death, it is not guaranteed that the onCreate() of the Activity will have completed before the onCreate() of the Fragment. So if you have shared data between the Activity and the Fragment, you might crash if the Fragment tries to access them before the Activity initializes them.

Further reading

One more thingā€¦

If youā€™re a mobile developer who enjoys app architecture, are interested in working for a values-first app company serving the queer community, talk to us! Visit https://www.scruff.com/careers for info about jobs at Perry Street Software

Other series you might like

Clean API Architecture (2021)
Classes, execution patterns, and abstractions when building a modern API endpoint.

Kotlin in Xcode? Swift in Android Studio? (2020)
A series on using Clean + MVVM for consistent architecture on iOS & Android

About the authors

Eric Silverberg and Stelios Frantzeskakis are developers for Perry Street Software, publishers of the LGBTQ+ dating apps SCRUFF and Jackā€™d, with more than 20M members worldwide.

--

--

Eric Silverberg
Perry Street Software Engineering

CEO, Perry Street Software. Developer. šŸ³ļøā€šŸŒˆ