Hello everyone! We need to regenerate the whole de...
# dagger
m
Hello everyone! We need to regenerate the whole dependency graph on logout. We know that a better solution would be managing the reset of the Singletons (it's not doable considering the current size of the codebase) or use a custom component (this would require writing boilerplate code anyway). The limitation of Hilt, that makes impossible to regenerate the graph, led us to this possible hack:
Copy code
@HiltAndroidApp
class LogApplication : Application() {

    @Inject
    internal lateinit var dependency: LoggerInMemoryDataSource

    override fun onCreate() {
        super.onCreate()
        logout()
    }

    @Suppress("CAST_NEVER_SUCCEEDS")
    private fun regenerateComponent() {
        Log.d("", "Before: $dependency")

        val cm = (this as GeneratedComponentManagerHolder).componentManager()
        val f = ApplicationComponentManager::class.java.declaredFields.find { it.name == "component" }
        f?.let {
            it.isAccessible = true
            f.set(cm, null)
        }
        ((this as GeneratedComponentManager<ApplicationComponentManager>)
            .generatedComponent() as LogApplication_GeneratedInjector)
            .injectLogApplication(UnsafeCasts.unsafeCast(this))

        Log.d("", "After: $dependency")
    }

    fun logout() {
        regenerateComponent()
    }
}
(LoggerInMemoryDataSource is a singleton and the reference is changing) Do you foresee any drawback apart of the need of testing on every Hilt upgrade? Should we propose to support this use case in Hilt? Thanks!
h
m
Thanks for the article! Btw we cannot use a custom component due the additional boilerplate it requires (entrypoint for getting dependencies) and the impossibility to migrate the existing codebase.