在父应用程序和子应用程序上使用@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
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...