在父应用程序和子应用程序上使用@HiltAndroidApp

时间:2020-08-12 18:49:09

标签: android dagger-2 dagger-hilt

假设我有

// 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
}

当我尝试执行上述操作时,会出现一些与Dagger error: cannot find symbol相关的错误。但是,当我从@HiltAndroidApp中删除MyParentApplication时,一切都可以正常编译。显然,我无法执行此操作,因为Dagger注入不适用于发行版本。要获得注入的派生类/子类,什么是合适的Hilt设置?

1 个答案:

答案 0 :(得分:1)

只有两个孩子Applications。一个用于debug,另一个用于release


  • Application中的父src/main/your/package/(无@HiltAndroidApp批注)
open class ParentApplication : Application() {
// some injection here
}
  • 发行版:src/release/your/package/
@HiltAndroidApp
class ReleaseChildApplication : ParentApplication() {
// some injection can also be here, but does not have to...
}
  • 调试:src/debug/your/package/
@HiltAndroidApp
class DebugChildApplication : ParentApplication() {
// debug injections here
}