In the library module's manifest, I need to use an Application class. Is it possible to do this in Android?
Manifest:
<application
android:name="com.compnay.app.App"
android:allowBackup="true"
android:label="@string/app_name"
>
</application>
Module library Manifest:
<application
android:name="com.example.mymodule.App"
android:allowBackup="true"
android:label="@string/app_name"
>
</application>
Unfortunately, it's not possible to have multiple
Applicationclasses, but there are workarounds, depending on what you're trying to do.An Android application can only have a single
Applicationsubclass that's used as the application context.This is because every
Contextin the app has a reference to it that can be retrieved bygetApplicationContext(). There's no provision for multiple application contexts, and no way to specify whether you want the "library application context" or the "main application context."Assuming that what you want to do is initialize your library on app startup, you can simply create an empty
ContentProviderthat'sandroid:exported="false"and do your initialization in itsonCreatemethod. This will be called before the app'sApplication.onCreate.This is, for example, what WorkManager does to initialize itself: it declares a provider in its manifest and performs its initialization within the
onCreatemethod of thatContentProvider.