androidandroid-application-class

App Startup library and long-running tasks in Application::onCreate


We recently integrated App Startup library into a project with a somewhat complex and time-consuming initialization logic that may take (in the worst case) up to several seconds. So we've got around 15 Initializers, some of them even trigger network / API calls.

It is not covered anywhere in the docs of the library, so I wonder: are there any limitations or time restrictions when it comes to App Startup initializers? Is it okay to do networking stuff with them? And, what's even more important: can long-running initializers actually trigger ANR dialog to appear?

Essentially, my question is not specific to App Startup, it's more like a general question about application initialization that happens before or during Application::onCreate call:

As per the docs, it seems that ANR can be shown mostly when the UI gets irresponsive for 5+ seconds, but does this apply to the initialization step? Given that the users are not expected to interact with anything before the first activity is created, i.e. there's no UI at this stage.

My gut feeling is that it should be fine to do from a technical point of view, putting something like Thead.sleep(10_000L) into onCreate also does not seem to trigger any ANRs, but wondering if somebody has made wider research here and maybe can share some feedback from real production projects, although I think that hitting 5 second init time is not very common :)


Solution

  • When the UI thread of an Android app is blocked for too long, an "Application Not Responding" (ANR) error is triggered.

    Background ANR dialogs are not always displayed to the user, but the app could still be experiencing performance issues.

    source

    ANR isn't just related to UI becoming unresponsive; it arises when the UI thread is blocked for over 5 seconds. The Application::onCreate function runs on the UI thread, so any prolonged operation without thread switching might lead to an ANR. Also, remember that not all ANRs are presented to the user. You can enable the "Show all ANRs" in your device's Developer options to see all of them.

    I would like to recommend an article written by the Bumble engineer "How We Achieved a 6x Reduction of ANRs." In this article, they directly explain that the Application::onCreate function was the cause of numerous ANR reports.

    we found out that approximately 60% of ANR reports were occurring while executing Application.onCreate method.