11

If the Application has a Custom Application object. It is needed to annotate this with @HiltAndroidApp ex: @HiltAndroidApp class AppCore: Application

Appcore has some initialization logic which is needed for the app to run

Now in the Instrumentation tests We also need to Extend the custom Application object. @CustomTestApplication(AppCore::class) interface HiltTestApplication

This gives an error @CustomTestApplication value cannot be annotated with @HiltAndroidApp

Is there any other way of using HILT in instrumentation tests with custom Application objects

public abstract interface HiltTestApplication {
                ^
  @CustomTestApplication value cannot be annotated with @HiltAndroidApp. Found: AppCore

2 Answers2

5

As suggested in the issue tracker. Can you abstract your initialization logic into a base class, say BaseAppCore : Application then in your prod application extend it @HiltAndroidApp AppCore : BaseAppCore and then for tests make Hilt generate a test app based on your abstract one, @CustomTestApplication(BaseAppCore::class) interface AppCoreTestApplication. It might be best to file this issue in https://github.com/google/dagger/issues

0

You will need to create a new class with the @HiltAndroidApp annotation, which would be different from the one you will use in your tests.

open class AppCore: Application
{
 // Existing code.
}
@HiltAndroidApp
class ProdAppCore : AppCore
{}

@CustomTestApplication(AppCore::class)
interface HiltTestApplication

If you are using Robolectric, you can set:

application = $packageName.HiltTestApplication_Application

And in your AndroidManifest.xml, set:

<application
        android:name="$packageName.ProdAppCore"

where $packageName is the package where ProdAppCore and HiltTestApplication class have been defined.

This is also discussed here: https://github.com/google/dagger/issues/2033

shubhamgarg1
  • 1,619
  • 1
  • 15
  • 20