Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when running instrumented tests with Hilt and App Startup #3356

Closed
ampeixoto opened this issue Apr 22, 2022 · 19 comments
Closed

Exception when running instrumented tests with Hilt and App Startup #3356

ampeixoto opened this issue Apr 22, 2022 · 19 comments

Comments

@ampeixoto
Copy link

ampeixoto commented Apr 22, 2022

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.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)

My real codebase is much more complex, but I created a much simpler project where I am able to reproduce the issue HERE.

From what I understood, when running the instrumentation tests, the startup code is first executed, even before anything on the test class (Before and BeforeClass annotated methods).

What is the best way to handle this? Is this something that was already detected?

@bcorso
Copy link

bcorso commented Apr 22, 2022

Hi @ampeixoto, you'll want to use EarlyEntryPoint for the entry point that is referenced in your stacktrace.

@ampeixoto
Copy link
Author

Thanks for your reply @bcorso.

I just tried EarlyEntryPoint in my reproducible codebase (see commit) but now I get another exception that I am not sure how to fix.

2022-04-22 19:47:07.237 17646-17646/com.example.appstartupinstrumentationtest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.appstartupinstrumentationtest, PID: 17646
    java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: androidx.startup.StartupException: java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
        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: No instrumentation registered! Must run under a registering instrumentation.
        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: No instrumentation registered! Must run under a registering instrumentation.
        at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
        at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)
        at dagger.hilt.android.internal.testing.EarlySingletonComponentCreatorImpl.create(EarlySingletonComponentCreatorImpl.java:13)
        at dagger.hilt.android.internal.testing.EarlySingletonComponentCreator.createComponent(EarlySingletonComponentCreator.java:37)
        at dagger.hilt.android.internal.testing.TestApplicationComponentManager.earlySingletonComponent(TestApplicationComponentManager.java:86)
        at dagger.hilt.android.EarlyEntryPoints.get(EarlyEntryPoints.java:61)
        at com.example.appstartupinstrumentationtest.SomeComponentInitializer.create(SomeComponentInitializer.kt:14)
        at com.example.appstartupinstrumentationtest.SomeComponentInitializer.create(SomeComponentInitializer.kt:8)
        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) 

Any suggestion?

@bcorso
Copy link

bcorso commented Apr 22, 2022

Sounds similar to #2016 (comment)? Does the advice there help?

@ampeixoto
Copy link
Author

@bcorso it looks similar indeed, but there is a difference, they are trying to run the test using Robolectric. I am trying to run it on the device. On top of that, I already have the RunWith annotation:

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class ExampleInstrumentedTest {

    @get:Rule
    var hiltRule = HiltAndroidRule(this)

    @Before
    fun setUp() {
        hiltRule.inject()
    }

    @Test
    fun useAppContext() {
        Log.d("AAAAA", "ExampleInstrumentedTest-running test...")
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.example.appstartupinstrumentationtest", appContext.packageName)
    }
}

My situation is very similar with this one #2016 (comment), with the usage of app-startup.

@bcorso
Copy link

bcorso commented Apr 28, 2022

It looks like the issue is that Androidx ApplicationProvider#getApplicationContext() is not set by the time ContentProvider#attachInfo() is called, which seems like the place the androidx.startup library has decided to initialize everything.

I think we could likely fix this by passing in the application manually here:

https://github.com/google/dagger/blob/master/java/dagger/hilt/android/EarlyEntryPoints.java#L61

rather than trying to get the application instance via ApplicationProvider.

I'll test this out and report back.

@ampeixoto
Copy link
Author

Hi @bcorso , any news on this?

@bcorso
Copy link

bcorso commented May 3, 2022

We have a workaround that should fix this particular issue. I plan on doing a release this week, so I'll try to get the commit into that release.

We've also filed https://issuetracker.google.com/230844558 for the androidx.startup team. I think there's an underlying issue with how early the library runs the initializations. In particular, although we've worked around this particular issue in Hilt, you still may run into issues in your initialization code because initialization happens before the base application context is set, so calling seemingly innocuous methods like Application#getApplicationContext() could result in an NPE.

copybara-service bot pushed a commit that referenced this issue May 3, 2022
…ponent.

This fixes cases where we need to create the component before `Application#attachBaseContext()` is called. For example. this seems to be the case for Androidx's startup library.

Fixes #3356

RELNOTES=N/A
PiperOrigin-RevId: 445182248
copybara-service bot pushed a commit that referenced this issue May 3, 2022
…ponent.

This fixes cases where we need to create the component before `Application#attachBaseContext()` is called. For example. this seems to be the case for Androidx's startup library.

Fixes #3356

RELNOTES=N/A
PiperOrigin-RevId: 445182248
@ampeixoto
Copy link
Author

We have a workaround that should fix this particular issue. I plan on doing a release this week, so I'll try to get the commit into that release.

We've also filed https://issuetracker.google.com/230844558 for the androidx.startup team. I think there's an underlying issue with how early the library runs the initializations. In particular, although we've worked around this particular issue in Hilt, you still may run into issues in your initialization code because initialization happens before the base application context is set, so calling seemingly innocuous methods like Application#getApplicationContext() could result in an NPE.

Hi @bcorso sorry for the late reply, but only now I got some time to go back to this.

Could you clarify which workaround you are talking about? Are you talking about passing the application manually here as you suggested? Did this ended up in a new version? It doesn't look like.

From what I read on the ticket open for the app startup library, it seems they wont do anything on their side...

I find it hard to believe that no one else faced this issue as it is a basic setup with hilt, app-startup and instrumentation tests...

@bcorso
Copy link

bcorso commented May 31, 2022

The workaround is in d14a3d6, which was released in 2.42.

Please update this post with a stacktrace or any new/additional information if you're still having issues after upgrading, thanks!

@ampeixoto
Copy link
Author

@bcorso thanks for the clarification!

When updating hilt to 2.42, I start getting the following compile time error:

> Task :app:packageDebug

> Task :app:hiltJavaCompileDebugAndroidTest
C:\Projects\AppStartupInstrumentationTestsExample\app\build\generated\hilt\component_sources\debugAndroidTest\dagger\hilt\android\internal\testing\EarlySingletonComponentCreatorImpl.java:9: error: EarlySingletonComponentCreatorImpl is not abstract and does not override abstract method create(Application) in EarlySingletonComponentCreator
class EarlySingletonComponentCreatorImpl extends EarlySingletonComponentCreator {
^
1 error

> Task :app:hiltJavaCompileDebugAndroidTest FAILED
> Task :app:createDebugApkListingFileRedirect UP-TO-DATE

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:hiltJavaCompileDebugAndroidTest'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 24s

Any idea?

@bcorso
Copy link

bcorso commented May 31, 2022

It sounds like it may be an issue with skewed versions. In particular, I would guess that maybe you've updated the API artifact (com.google.dagger:hilt-android) but not the compiler artifact (com.google.dagger:hilt-compiler).

@ampeixoto
Copy link
Author

ampeixoto commented May 31, 2022

I update both:

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

    implementation 'androidx.test:runner:1.4.0'

    implementation "com.google.dagger:hilt-android:2.42"
    kapt "com.google.dagger:hilt-android-compiler:2.42"

    implementation "androidx.startup:startup-runtime:1.1.1"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "com.google.dagger:hilt-android-testing:2.42"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.42"
}

Do you spot any issue?

(btw, what is the difference between the artifact com.google.dagger:hilt-compiler and the com.google.dagger:hilt-android-compiler?)

Update
Building and running the app with v2.42 works fine. The issue is only when trying to run the instrumentation test.

@bcorso
Copy link

bcorso commented May 31, 2022

Hmm, the method it's complaining about should be written here:

https://github.com/google/dagger/blob/master/java/dagger/hilt/processor/internal/root/EarlySingletonComponentCreatorGenerator.java#L44-L46

If possible, can you post the the generated java source for the generated EarlySingletonComponentCreatorImpl.java?

C:\Projects\AppStartupInstrumentationTestsExample\app\build\generated\hilt\component_sources\debugAndroidTest\dagger\hilt\android\internal\testing\EarlySingletonComponentCreatorImpl.java

It may also be worth double checking your dependencies to ensure you're getting 2.42

https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html#sec:listing_dependencies

(btw, what is the difference between the artifact com.google.dagger:hilt-compiler and the com.google.dagger:hilt-android-compiler?)

They are exactly the same. At some point we may remove hilt-android-compiler and just have everyone migrate to hilt-compiler. The reasoning we decided to drop the "android" is that at some point we may decide to have a non-android version of Hilt and we anticipate that the same compiler artifact will be used for both hilt and hilt-android artifacts.

@ampeixoto
Copy link
Author

@bcorso thanks for the info!

Here is the content of the EarlySingletonComponentCreatorImpl.java generated class:

package dagger.hilt.android.internal.testing;

import androidx.test.core.app.ApplicationProvider;
import dagger.hilt.android.internal.Contexts;
import dagger.hilt.android.internal.modules.ApplicationContextModule;
import dagger.hilt.android.internal.testing.root.DaggerDefault_HiltComponents_SingletonC;
import java.lang.Object;

class EarlySingletonComponentCreatorImpl extends EarlySingletonComponentCreator {
  Object create() {
    return DaggerDefault_HiltComponents_SingletonC.builder()
        .applicationContextModule(
            new ApplicationContextModule(Contexts.getApplication(ApplicationProvider.getApplicationContext())))
        .build();
  }
}

Regarding the dependencies, it looks like it is using v2.42:

C:\Projects\AppStartupInstrumentationTestsExample> .\gradlew :app:dependencies --configuration androidTestImplementation

> Task :app:dependencies

------------------------------------------------------------
Project ':app'
------------------------------------------------------------

androidTestImplementation - Implementation only dependencies for 'androidTest' sources. (n)
+--- androidx.test.ext:junit:1.1.3 (n)
+--- androidx.test.espresso:espresso-core:3.4.0 (n)
\--- com.google.dagger:hilt-android-testing:2.42 (n)

(n) - Not resolved (configuration is not meant to be resolved)

A web-based, searchable dependency report is available by adding the --scan option.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s
C:\Projects\AppStartupInstrumentationTestsExample> .\gradlew :app:dependencies --configuration kaptAndroidTest

> Task :app:dependencies

------------------------------------------------------------
Project ':app'
------------------------------------------------------------

kaptAndroidTest
\--- com.google.dagger:hilt-android-compiler:2.42
     +--- com.google.dagger:dagger:2.42
     |    \--- javax.inject:javax.inject:1
     +--- com.google.dagger:dagger-compiler:2.42
     |    +--- com.google.dagger:dagger:2.42 (*)
     |    +--- com.google.dagger:dagger-producers:2.42
     |    |    +--- com.google.dagger:dagger:2.42 (*)
     |    |    +--- com.google.guava:failureaccess:1.0.1
     |    |    +--- com.google.guava:guava:31.0.1-jre
     |    |    |    +--- com.google.guava:failureaccess:1.0.1
     |    |    |    +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
     |    |    |    +--- com.google.code.findbugs:jsr305:3.0.2
     |    |    |    +--- org.checkerframework:checker-qual:3.12.0
     |    |    |    +--- com.google.errorprone:error_prone_annotations:2.7.1
     |    |    |    \--- com.google.j2objc:j2objc-annotations:1.3
     |    |    +--- javax.inject:javax.inject:1
     |    |    \--- org.checkerframework:checker-compat-qual:2.5.5
     |    +--- com.google.dagger:dagger-spi:2.42
     |    |    +--- com.google.dagger:dagger:2.42 (*)
     |    |    +--- com.google.dagger:dagger-producers:2.42 (*)
     |    |    +--- com.google.code.findbugs:jsr305:3.0.2
     |    |    +--- com.google.devtools.ksp:symbol-processing-api:1.5.30-1.0.0
     |    |    |    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30 -> 1.5.32
     |    |    |         +--- org.jetbrains.kotlin:kotlin-stdlib:1.5.32 -> 1.6.10
     |    |    |         |    +--- org.jetbrains:annotations:13.0
     |    |    |         |    \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10
     |    |    |         \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.32
     |    |    |              \--- org.jetbrains.kotlin:kotlin-stdlib:1.5.32 -> 1.6.10 (*)
     |    |    +--- com.google.guava:failureaccess:1.0.1
     |    |    +--- com.google.guava:guava:31.0.1-jre (*)
     |    |    +--- com.squareup:javapoet:1.13.0
     |    |    +--- javax.inject:javax.inject:1
     |    |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10 (*)
     |    |    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.32 (*)
     |    +--- com.google.code.findbugs:jsr305:3.0.2
     |    +--- com.google.googlejavaformat:google-java-format:1.5
     |    |    +--- com.google.guava:guava:22.0 -> 31.0.1-jre (*)
     |    |    \--- com.google.errorprone:javac-shaded:9-dev-r4023-3
     |    +--- com.google.guava:failureaccess:1.0.1
     |    +--- com.google.guava:guava:31.0.1-jre (*)
     |    +--- com.squareup:javapoet:1.13.0
     |    +--- javax.inject:javax.inject:1
     |    +--- net.ltgt.gradle.incap:incap:0.2
     |    +--- org.checkerframework:checker-compat-qual:2.5.5
     |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10 (*)
     |    +--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.32 (*)
     |    \--- org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2
     |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10 (*)
     +--- com.google.dagger:dagger-spi:2.42 (*)
     +--- com.google.code.findbugs:jsr305:3.0.2
     +--- com.google.guava:failureaccess:1.0.1
     +--- com.google.guava:guava:31.0.1-jre (*)
     +--- com.squareup:javapoet:1.13.0
     +--- javax.annotation:jsr250-api:1.0
     +--- javax.inject:javax.inject:1
     +--- net.ltgt.gradle.incap:incap:0.2
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10 (*)
     \--- org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2 (*)

(*) - dependencies omitted (listed previously)

A web-based, searchable dependency report is available by adding the --scan option.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1s

@bcorso
Copy link

bcorso commented May 31, 2022

Hmm, that's weird, it's definitely generating the old implementation for some reason.

If you have a sample project I can take a look.

@ampeixoto
Copy link
Author

@bcorso I just invalidate caches, with same result.

You can look at the example repo I linked on the first post here.

I just commited the change to update hilt dependencies.

Looking forward to here from you.

@bcorso
Copy link

bcorso commented May 31, 2022

The version here needs to be upgraded to 2.42.

@ampeixoto
Copy link
Author

The version here needs to be upgraded to 2.42.

@bcorso You are totally right! Completely forgot about it. I usually have the versions centralized, but for this sample project, I didn't... Thanks for the solution! Now the original issue is solved :)

Now I'm facing another (unrelated issue) in my main project related to the ViewModels instantiation using Hilt:

Caused by: java.lang.InstantiationException: java.lang.Class<my.package.MyViewModel> has no zero argument constructor

The strange thing is that, when running the app, the ViewModel is properly instantiated. This only happens in instrumentation tests that run on the device.

I know this is not related with the original issue, but if you have any suggestions for me to look into... It seems to me that I am missing some androidTestImplementation dependency. You know if I have to add some dependency for the instrumentation tests to not face this error?

@ampeixoto
Copy link
Author

Ok, found the issue in my instrumentation test. I will let here my solution as I believe it might help others.

Some context: I am using compose for the UI.

Basically, the main problem was that I was using createComposeRule() instead of createAndroidComposeRule<MyActivity>(), so the HiltViewModelFactory was not used.

After I started using createAndroidComposeRule<MyActivity>() as follow, my test started running:

@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class MyScreenTest  {

    val hiltRule = HiltAndroidRule(this)

    val composeTestRule = createAndroidComposeRule<MyActivity>()

    @get:Rule
    val rule: RuleChain = RuleChain.outerRule(hiltRule)
        .around(composeTestRule)

    // Tests....
}

(MyActivity must be annotated with @AndroidEntryPoint)

ZoricaStojchevska pushed a commit to ProtonMail/proton-mail-android that referenced this issue Nov 4, 2022
Caused by some ops in core's newly added initializers to happen before
the DI componenet was setup.
Based on google/dagger#3356 this issue is
fixed in dagger / hilt 2.42. Such upgrade doesn't apply cleanly here,
which lead to this workaround of removing the offending core
initializers in the manifest to instead invoke them manually, so that
they won't be initialized when runnign tests (as they use
`HiltTestApplication` instead of `ProtonApplication`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants