Accessing application context before app has been launched #8382
-
For test arrangement (e.g. setting up a room database or shared preferences with specific data), it would be useful to be able to access the application context before the app has been launched (since many functions require the context). I am wondering, is this somehow possible currently? Or am I on the wrong track and that goal (test setup before app launch) should be solved completely differently? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
If you just want to use Application context, you can try https://developer.android.com/reference/androidx/test/core/app/ApplicationProvider. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick answer! Calling ApplicationProvider.getApplicationContext() doesn't achieve the desired result though, I believe. Because it would already launch the application. But the goal is to retrieve the application context without app-launch (so some preparations could be done using the context before launch) getApplicationContext() calls getTargetContext():
which in turn calls RuntimeEnvironment.getApplication()
|
Beta Was this translation helpful? Give feedback.
-
Code-wise, my (potentially naive) approach would have been to split
into two parts: first is doing everything the original function did, including the application context creation, but exluding instantiating and launching the app, which would go into second part. Then run the 1st part immediately in
even in lazy loading case. Meaning that the application context (which could be stored in RuntimeEnvironment, for instance) would be available from the very beginning (after setUpApplicationState() has been completed). The second part is executed as a replacement of installAndCreateApplication() (i.e. in the application supplier returned in createApplicationSupplier() ) Would that make sense to you? I could create a PR if this helps for discussion. |
Beta Was this translation helpful? Give feedback.
-
What are you trying to optimize by accessing an Application before it has been launched? |
Beta Was this translation helpful? Give feedback.
-
My goal would be to setup certain things (preferences, room database etc.) before app is launched, because app might initialize itself by reading these data very early (i.e. leading to race conditions between app reading the data and test case setting up exactly the same). E.g. for setting up the shared preferences, I would need to use the application context:
same for room database:
and this holds for plenty other things a test case might want to set up. |
Beta Was this translation helpful? Give feedback.
-
Would making use of a https://developer.android.com/reference/androidx/test/runner/lifecycle/ApplicationLifecycleCallback work for this case? You can get a callback before Application.onCreate happens, and get passed an Application object. This approach would work for android instrumentation tests as well. |
Beta Was this translation helpful? Give feedback.
-
Great! The life-cycle monitoring approach works fine for our use cases (where no relevant initialization before application::onCreate() is being run). So we are doing now in the test:
before lazily launching the app. Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Would making use of a https://developer.android.com/reference/androidx/test/runner/lifecycle/ApplicationLifecycleCallback work for this case? You can get a callback before Application.onCreate happens, and get passed an Application object.
This approach would work for android instrumentation tests as well.