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

问题描述

假设我有

// 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设置?

解决方法

只有两个孩子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
}