question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@CustomTestApplication value cannot be annotated with @HiltAndroidApp

See original GitHub issue

I have an application that needs injection:

@HiltAndroidApp
class MyApplication: Application() {
    @Inject lateinit var printerFactory: PrinterFactory
    ...
}

Other code in the app uses application.printerFactory.

The printerFactory is provided by this:

@Module
@InstallIn(ApplicationComponent::class)
object ProdModule {
    @Provides
    fun providePrinterFactory(): PrinterFactory {
        return FactoryThatReturnsARealPrinter()
    }
}

Now, for instrumented tests, I need to override this with a test version:

@HiltAndroidTest
@UninstallModules(ProdModule::class)
@RunWith(AndroidJUnit4::class)
@LargeTest
class MainTest {
    @get:Rule(order = 0)
    var hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    var intentsTestRule = IntentsTestRule(MainActivity::class.java, false, false)

    // Test replacements for production stuff.
    @Module
    @InstallIn(ApplicationComponent::class)
    class TestModule {
        @Provides
        fun providePrinterFactory(): PrinterFactory {
            return FactoryThatReturnsATestPrinter()
        }
    }
    ...
}

And so I get this error:

java.lang.IllegalStateException: Hilt test, MainTest, cannot use a @HiltAndroidApp application but found MyApplication. To fix, configure the test to use HiltTestApplication or a custom Hilt test application generated with @CustomTestApplication.

Well, I can’t use HiltTestApplication because I need to use MyApplication, which has dependencies injected.

So I added this:

    @CustomTestApplication(MyApplication::class)
    interface HiltTestApplication

And so I get this error:

error: [Hilt]
    public static abstract interface HiltTestApplication {
                           ^
  @CustomTestApplication value cannot be annotated with @HiltAndroidApp. Found: MyApplication

If MyApplication cannot be annotated with @HiltAndroidApp, then how is it expected to inject things?

Instructions unclear, ended up in inconsistent state.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:25 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
rhonyabdullahcommented, Oct 1, 2020

Bad Solution Regarding the this documentation I create interface and put my test application:

@CustomTestApplication(TestApplication::class)
interface AndroidTestApplication

But it gives me exception:

Caused by: java.lang.InstantiationException: java.lang.Class<com.myapp.AndroidTestApplication> cannot be instantiated
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1165)
        at com.myapp..AndroidTestRunner.newApplication(AndroidTestRunner.kt:16)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1218)

So i’m directly passing the generated hilt application class for my test app and skip using AndroidTestApplication interface and it works: Instrumentation.newApplication(AndroidTestApplication_Application::class.java, context)

5reactions
tianjyancommented, Sep 30, 2020

@danysantiago I got the following issue when I use a base app. Do I missing something else ?

java.lang.RuntimeException: Unable to instantiate application com.sample.MainApplication: java.lang.InstantiationException: java.lang.Class<com.sample.di.TestApplication> cannot be instantiated

@CustomTestApplication(BaseApplication::class)
interface TestApplication
open class BaseApplication : Application()
@HiltAndroidApp
open class MainApplication : BaseApplication()
Read more comments on GitHub >

github_iconTop Results From Across the Web

CustomTestApplication value cannot be annotated with ...
You will need to create a new class with the @HiltAndroidApp annotation, which would be different from the one you will use in...
Read more >
Hilt testing guide | Android Developers
Custom application for tests ... If you cannot use HiltTestApplication because your test application needs to extend another application, annotate a new class...
Read more >
Testing with Hilt android - Nyame Bismark - Medium
Custom application for tests. If you cannot use HiltTestApplication because your test application needs to extend another application, annotate a new class or ......
Read more >
Hilt Can't Find Application Class Even After Been Annotated ...
As the error indicates adding the annotation @HiltAndroidApp to the AppController @CustomTestApplication value cannot be annotated with @HiltAndroidApp.
Read more >
Testing - Hilt - Dagger
In addition, Hilt cannot be used in vanilla JVM tests, but it does not ... with @CustomTestApplication and specify the base class in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found