Search code examples
androiddagger-2dagger-hilt

Using @HiltAndroidApp on Parent Application and Child Application


Let's say I have

// this class lives in the release (variant) directory
@HiltAndroidApp
open class MyParentApplication : Application() {
// some injection here
}

// this class lives in the debug (variant) directory 
@HiltAndroidApp
class MyChildApplication : MyParentApplication {
// some debug only injection here. Debug injections won't be available as part of any app releases
// use debug only injections to do debug only actions
}

When I tried to do the above, I will get some Dagger error: cannot find symbol related errors. However, when I removed @HiltAndroidApp from MyParentApplication, then everything compiles fine. Obviously, I can't do this because Dagger injection wouldn't work on the release build. What would be the appropriate Hilt setup to get a derived/child class injected?


Solution

  • Just have two child Applications. One for debug and one for the release.


    • parent Application in src/main/your/package/ (no @HiltAndroidApp annotation)
    open class ParentApplication : Application() {
    // some injection here
    }
    
    • release: in src/release/your/package/
    @HiltAndroidApp
    class ReleaseChildApplication : ParentApplication() {
    // some injection can also be here, but does not have to...
    }
    
    • debug: in src/debug/your/package/
    @HiltAndroidApp
    class DebugChildApplication : ParentApplication() {
    // debug injections here
    }