I'm developing a multi-module app -too have a clear separation of concerns- with Kotlin in which I'm trying to implement Hilt -to also decouple- and I'm having problems accessing application class from modules.
First, the structure of my app:
project
|- App
|- Common
|- Core
|- DTO
|- Repository
The relation between modules (modules visibility):
Project
|
|- App
| |- Common
| |- DTO
| |- Core
|
|- Common
| |- DTO
|
|- Core
| |- Common
|
|- DTO
|
|- Repository
| |- Common
| |- DTO
Well, all modules have the required Hilt dependencies and plugins in their corresponding build.gradle.
I have an AppSettings class with constants representing all app configuration (like colours), and a context and a singleton SQLite database instance.
Up to now, I was accessing all that constants and context in a static way, and all was working fine, and I was using WeakReference for the context to avoid memory leaks, but someone suggested that using a context that way is not good for testing, to decouple and use DI, that's why I started with Hilt.
I don't know if I'm doing implementation correctly, because I'm new to Hilt, and it's giving me many headaches, but let's go:
I thought that my AppSettings could be the Application class Hilt requires and, as it's available everywhere could also fit perfectly to serve the context wherever it is needed.
AppSettings:
@HiltAndroidApp
class AppSettings : MultiDexApplication() {
val actionBarTitleColor = "#0D0D0D"
...
}
I'm not yet sure if I should put this Application class in the app module, or if it should be put in the "Common" module, which is accesible from all other modules...
This confusion arises because if I put it in "Common" it would be available everywhere, but if it is located into app, then Common does not have access to app and Common needs access to config constants.
On the other hand, I'm not also sure if my AppSettings should be the Application class or it's better if it is just a Hilt module...
Anyway, I'm trying to serve that config globals in many ways, but none of them are actually making my app to build.
One of my attempts was to create a Hilt module in Common to serve AppSettings like so:
@Module
@InstallIn(SingletonComponent::class)
object Dependencies {
@Provides
@Singleton
fun bindsAppSettings(): AppSettings {
return AppSettings()
}
}
Then in any helper class I could do:
class TestClass @Inject constructor() {
@Inject
lateinit var appSettings: AppSettings // = Dependencies.bindsAppSettings()
fun whatever() {
val color = appSettings.actionBarEndColor
}
}
But not sure how to initialise appSettings, if the commented code is OK or not (it does work with the commented code though)
Of if instead of creating a module to serve AppSettings I could inject it directly anywhere, but how.
In the end what I need is my configuration class to be available anywhere along my multi-module app, but this also applies to any other class I will need then to inject.
Any help on how to deal with Application class (or AppSettings in a module, or however) being available in all modules.