问题描述
假设我有
// 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
}