본문 바로가기

Android/TDD(Test Driven Development, 테스트 주도 개발)

Dagger Hilt와 App Startup 조합 시 AndroidTest 불가능

반응형

TL; DR

2022-05-11, dagger 2.42, startup 1.20.0-alpha01 기준 불가능합니다.

문제

App Startup 적용 이후 AndroidTest(Instrumented Test)가 실행되지 않습니다. (debugger 무한 대기)
Logcat을 보니 다음과 비슷한 오류가 찍힙니다.

2022-04-11 10:41:09.352 24409-24409/com.example.appstartupinstrumentationtest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appstartupinstrumentationtest, PID: 24409
    java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: androidx.startup.StartupException: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
        at android.app.ActivityThread.installProvider(ActivityThread.java:8195)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:7726)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7546)
        at android.app.ActivityThread.access$1500(ActivityThread.java:301)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: androidx.startup.StartupException: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
        at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:162)
        at androidx.startup.AppInitializer.discoverAndInitialize(AppInitializer.java:198)
        at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:38)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2429)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2399)
        at android.app.ActivityThread.installProvider(ActivityThread.java:8190)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:7726) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7546) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:301) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8633) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
     Caused by: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
        at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
        at dagger.hilt.android.internal.testing.TestApplicationComponentManager.generatedComponent(TestApplicationComponentManager.java:96)
        at dagger.hilt.android.testing.HiltTestApplication.generatedComponent(HiltTestApplication.java:49)
        at dagger.hilt.EntryPoints.get(EntryPoints.java:59)
        at dagger.hilt.android.EntryPointAccessors.fromApplication(EntryPointAccessors.kt:35)
        at com.example.appstartupinstrumentationtest.InitializerEntryPoint$Companion.resolve(InitializerEntryPoint.kt:20)
        at com.example.appstartupinstrumentationtest.SomeComponentInitializer.create(SomeComponentInitializer.kt:10)
        at com.example.appstartupinstrumentationtest.SomeComponentInitializer.create(SomeComponentInitializer.kt:7)
        at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:155)
        at androidx.startup.AppInitializer.discoverAndInitialize(AppInitializer.java:198) 
        at androidx.startup.InitializationProvider.onCreate(InitializationProvider.java:38) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2429) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:2399) 
        at android.app.ActivityThread.installProvider(ActivityThread.java:8190) 
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:7726) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7546) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:301) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8633) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

출처: https://github.com/google/dagger/issues/3356

 

Exception when running instrumented tests with Hilt and App Startup · Issue #3356 · google/dagger

Hello, I am starting to setup some instrumented tests on my project, but when I run them, I get the following exception on logcat: 2022-04-11 10:41:09.352 24409-24409/com.example.appstartupinstrume...

github.com

따끈따끈한 이슈...
근데 사실 1년 전에도 제기된 적 있습니다.
https://github.com/google/dagger/issues/2016#issuecomment-788770446

 

Hilt: handle Services with injection automatically started in instrumentation tests · Issue #2016 · google/dagger

Let's assume I have a service that is automatically started at the app startup and it is required by some third party library. This service needs some fields to be injected in the onCreate meth...

github.com

이슈가 생성됐으니 해결을 해야죠! 해당 에러를 해결한 dagger 2.42가 어제 출시되었습니다. 빠르네요!
https://github.com/google/dagger/releases/tag/dagger-2.42

 

Release Dagger 2.42 · google/dagger

What’s new in Dagger Potentially breaking changes As of this release, Dagger’s generated class will no longer extend the user-defined @Component annotated class (885b16d). This change was done to i...

github.com

근데 간단한 샘플 앱에서는 문제가 해결되는 반면, 실제 앱에 적용하니 여전히 문제가 지속됩니다. 근본적인 문제는 startup의 초기화 시점과 관련이 있기 때문에... 일단은 dagger 팀에서 androidx.startup 팀에 이슈트래커를 남겨놓았습니다.
https://issuetracker.google.com/issues/230844558

 

Google Issue Tracker

 

issuetracker.google.com

따라서 androidx.startup이 새 버전으로 릴리즈 되고, 안정화 되기 전까지는 보류해야 하는 문제입니다...

교훈: 다들 AndroidTest를 사용하지 않는다!

반응형