안녕하세요. 안드로이드에서 DI를 적용하는데 잘 안되는 부분이 있어서 도움을 받고 싶어서 글을 올립니다.
component, module, provide 까지 생성했습니다.
class testModule {
@Provides @Singleton
fun provideTestServer(): TestService {
…..
}
}
@Component(modules = arrayOf(
TestModule::class
))
interface TestComponent {
fun inject(mainActivity: MainActivity)
}
그리고 MainActivity onCreate 에서
DaggerImageComponent.builder()
.build().inject(this)
를 호출 해줬습니다.
여기서 잘 안되는 부분이 나오는데요.
MainActivity 에서
@Inject
lateinit var testService: TestService
를 하면 정상적으로 인스턴스가 주입이 되는데,
다른 파일에서
object TestObject {
@Inject
@JvmSynthetic // 이 annotation 을 빼면 static에는 inject를 할 수 없다는 에러가 발생해서 추가 하였습니다.
lateinit var testService: TestService
fun test() = testService.testfun()
}
or
@Singleton
class TestClass {
@Inject
lateinit var testService: TestService
fun test() = testService.testfun()
}
를 하려고 하면 인스턴스가 주입이 안되는 것 같습니다.
실행하면 initialize 가 되지 않았다는 에러가 발생합니다.
계속 dagger2 구글링하고 샘플을 봐도 이유가 뭔지 모르겠습니다.