If you are interested in this article, maybe you will also be interested in my official account, please scan the qr code below or search the official wechat account: MXSZGG

  • The simulation
    • Call time
  • The advantages and disadvantages
  • Afterword.

The simulation

Recently xiaoming open source Android tripartite library, access process is very simple, developers only need to the Application onCreate() method to initialize it, and then can call the corresponding library API —

public class App extends Application { @Override public void onCreate() { super.onCreate(); XiaomingLibrary.init(this); }}Copy the code

This is a very common initialization process after three-party library access, but from the perspective of library developers, is it possible to reduce this step of library initialization process to make the access process of developers simpler? If anything, a library like LeakCanary, which only needs to be initialized in the Application and does not require any API calls, would give developers a non-invasive feel. Of course there is. As a library developer, you can create a ContentProvider in your library and initialize your library using the Context returned by getContext() in the ContentProvider onCreate() method, Instead of requiring developers to initialize your library in their Application’s onCreate() method, the actual type of Context returned by getBaseContext() is the Application of the Application, So you can use this Context to initialize the library.

Call time

So when is the onCreate() method of the ContentProvider called? It is called between Application attachBaseContext(Context) and onCreate(), The attachBaseContext(Context) method of the Application is called which means that the Context of the Application is initialized, The ContentProvider gets the Context of the Application, so you can initialize it in the ContentProvider’s onCreate() method. This should be proven from a code point of view — as you probably know, during application startup you end up in the ActivityThread’s handleBindApplication() method, In this method, you can see the app = makeApplication (), installContentProviders (app), mInstrumentation. CallApplicationOnCreate (app) three methods is called successively –

MakeApplication () creates the Application and assigns the Application instance to the local variable app, The ContentProvider is initialized with the app local variable in the installContentProviders(app). Finally, callApplicationOnCreate(app) must be calling the app’s onCreate() method. The specific process is as follows:

MakeApplication () call chain:

LoadedApk#makeApplication() -> Instrumentation#newApplication() -> Instrumentation.newApplication() -> Application#attach() -> Application#attachBaseContext()

InstallContentProviders ()

ActivityThread#installContentProviders() -> ActivityThread#installProvider() -> ContentProvider#attachInfo() -> ContentProvider.this.onCreate()

CallApplicationOnCreate () call chain:

Instrumentation#callApplicationOnCreate() -> Application#onCreate()

The advantages and disadvantages

The advantages are obvious — it saves developers from having to initialize the library and reduces access costs, especially for libraries like LeakCanary, BlockCanary, or others that only need to be initialized without requiring developers to call any APIS. Developers only need to add dependencies to use the library, a zero-intrusion access process.

Disadvantages: The downside is that it doesn’t necessarily apply to all scenarios, because the ContentProvider’s onCreate() executes before the Application’s onCreate() method, If your library needs to have other business dependencies (for example, your library needs to be initialized after the other three library dependencies have been initialized), this approach is not suitable for your library.

Afterword.

The content of this article is very simple, but the author thinks that the article should not only bring to the reader a sense of new skills + 1, after understanding the phenomenon of surface, it should be to remove surface exploration, so the next time before with friend to blow, can be able to show you more of SAO operation. Finally, in order for readers to better digest the content, I uploaded an example on Github, poke me straight.