ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Your android libraries should not ask for an application context

--

What is the first thing we need to do when we import a library ? Override an Application class to initialize it with a context šŸ˜ž But why ? and why some libs like Firebase šŸ”„donā€™t need to be initialized ? lets take a look šŸ§

We need to initialize our library

Letā€™s take an example, we want to use Stetho in our app, following the documentation, it needs to be initialized with the app context, so we need to create an application if we donā€™t have one :

class MainApplication : Application() {
override fun onCreate(){
super.onCreate()
Stetho.initializeWithDefaults(this)
}
}

This application needs to be declared in our AndroidManifest.xml to be executed :

<application
android:name=".
MainApplication"
...

A lot of libraries

Now imagine we use Stetho, JodaTimeAndroid, Crashlytics and Realm, our application will look like

class MainApplication : Application() {
override fun onCreate(){
super.onCreate()
Fabric.with(this, new Crashlytics())
Stetho.initializeWithDefaults(this)
JodaTimeAndroid.init(this)
Realm.init(this)
}
}

In my project, I need to override an application only forā€¦ initializing my libraries

Uselessā€¦

Libraries that do not need to be initialized

When you include Firebase šŸ”„, it does not ask for any initialization, you just have to use :

This database access do not needs a context, and can stored locally, for offline access. We can assume itā€™s initialized automatically, and has a mechanism to retrieve the application context.

ContentProvider & Manifest-Merger

https://developer.android.com/studio/build/manifest-merge

Your APK file can contain just one AndroidManifest.xml file, but your Android Studio project may contain severalā€”provided by the main source set, build variants, and imported libraries. So when building your app, the Gradle build merges all manifest files into a single manifest file that's packaged into your APK.

If we take a look at our merged AndroidManifest.xml :

app/build/intermediates/merged_manifests/MY_APP/processMY_APPDebugManifest/merged/AndroidManifest.xml

We can find at the end of this file a provider that have been imported from the Firebase library :

Looking inside the FirebaseInitProvider, we understand that this provider has access to the applicationā€™s context using this.getContext().

ContentsProviders are initialized and called directly after the applicationā€™s object is created, itā€™s a good idea to use them to init a library !

Automatically initialize our library

We can imagine facebook adding a StethoInitProvider for its initialization :

class StethoInitProvider : ContentProvider() {

override fun onCreate(): Boolean {
Stetho.initializeWithDefaults(context)
return true
}
...
}

And exposing this provider in its AAR/AndroidManifest file

<provider
android:name=".
StethoInitProvider"
android:authorities="com.facebook.stetho.StethoInitProvider"
/>

Using Gradle to import this version of Stetho, we will not need extra code to initialize this library in our project šŸ˜Ž

As a result, if we have to configure Stetho, it should have some statics methods like

Stetho.getInstance().configure(ā€¦)

Our project without application

If our library doesnā€™t use InitProviders, we can create our own :

class JodaTimeInitProvider : ContentProvider() {

override fun onCreate(): Boolean {
JodaTimeAndroid.init(this)
return true
}
...
}
class RealmInitProvider : ContentProvider() {

override fun onCreate(): Boolean {
Realm.init(this)
return true
}
...
}

And add them to our AndroidManifest :

<provider
android:name=".
JodaTimeInitProvider"
android:authorities="
${applicationId}.JodaTimeInitProvider" />
<provider
android:name=".
RealmInitProvider"
android:authorities="
${applicationId}.RealmInitProvider" />

We can now remove our useless MainApplication.class

Happy Dance

BONUS STAGE

I developed a library to retrieve the Application from anywhere and the current Activity, donā€™t hesitate to try it !

https://github.com/florent37/ApplicationProvider

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (8)

Write a response