r/androiddev • u/AlexoTheSealFlexo • Nov 22 '21
Open Source Finally, I've found a valid use for "android:process" attribute for the activity - I've made a small library to trigger "safe mode" for application that experiences repeated crashes on startup
https://github.com/DrBreen/AppSalvager14
u/gold_rush_doom Nov 22 '21
There are other uses as well. Like if your app works mostly in the background and you don’t want your frontend to also crash your service when that happens.
9
u/Izacus Developer Nov 23 '21
Also anything you want running in background (media playback, location tracking) benefits from being split into a separate process because it'll have a massively smaller footprint for OOM killer.
1
u/AlexoTheSealFlexo Nov 23 '21
Oh yeah, true, smaller memory footprint is also a good usecase for that.
3
0
Nov 23 '21
[removed] — view removed comment
6
u/AlexoTheSealFlexo Nov 23 '21
Oh, there can be multiple reasons.
Personally, I've encountered these:
Data migration was not performed correctly, thus crashing the app when upgrading from old version.
Google Maps SDK downloaded some bad data, and app had to be reinstalled (or its' data cleared) to fix that
Third-party SDK started crashing randomly on start until the app data was cleared
-1
Nov 23 '21
[removed] — view removed comment
1
u/AlexoTheSealFlexo Nov 23 '21
Sure.
In your
build.gradle:dependencies { implementation `io.github.drbreen:appsalvager:0.1.0` }In your class that inherits from
android.app.Application:class TheApplication: Application() { override fun attachBaseContext(base: Context?) { super.attachBaseContext(base) AppSalvager.install( context = this, configuration = AppSalvager.Configuration( policy = SameExceptionPolicy(), // you can use other policy createSalvageView = <your view factory goes here> ) ) } override fun onCreate() { super.onCreate() if (AppSalvager.inSalvageMode) { return } ... } }
17
u/AlexoTheSealFlexo Nov 22 '21
The inner workings of the library are very simple:
Install default exception handler before
ContentProviders start initializing (inApplication#attachBaseContext).On uncaught exception the exception is quickly serialized and saved to the disk
Every time it happens, we pass the list of recent exceptions to
SalvageModePolicy, which returnstrueif these exceptions should trigger salvage mode.If it returns true, launch
Activityin another process, and abortApplicationinitialization process early.ContentProvidercrashes will not happen here, since they are only initialized once during main process start.With this Activity, you can instruct user to reinstall the app, clear app data or do whatever else that can fix the repeated crash.