Reputation: 99
I am writing an app where a host application, lets call it the base App, calls and navigates to an activity in a different module. This module is an Android Library with its own custom application class because I used Dagger for DI; it is set to its own Android Manifest's application tag with the android:name attribute. I'm doing this because the end goal is to create a standalone Android Library for use in other apps.
Now, I am realizing that, with this approach, I've seemed to have restricted myself from allowing the base app from having and setting its own custom Application class to its own Android Manifest. Attempting to override it by setting tools:replace="android:name" causes the Android library to crash on when navigated to its host activity because it cannot cast the base app Application class to the library's Application class.
The solution could be simple: do not allow the base app implementers from making their own custom Application class and setting it to their Android Manifest xml, but this might be a deal breaker for some apps who say, need their own custom Application class for their own Dagger DI graphs.
Did I write myself into a dead end here with no solution to this? Any advice would be appreciated if I can somehow have the Library's application element have its own custom Application class to use independent of the base app's custom Application class set in its own Manifest xml?
Upvotes: 0
Views: 82
Reputation: 99
There is an alternative solution I think I can use. If the library's custom application class is extendable with an open modifier in Kotlin on its class definition, I can have the host app's application extend from it and set it to the manifest instead. This doesn't seem to break the app so far I've tried, but is this the right approach or is it too good to be true?
Library's application class:
open class LibraryApplication : Application() {
//Dagger Graph here along with other code
}
Base App's application class:
open class BaseAppApplication : LibraryApplication() {
//Base app's Dagger Graph here
}
Base App's Application node in AndroidManifest.xml
<application
android:name="package.BaseAppApplication"
</application>
Upvotes: 0