I am migrating the application from Dagger2 to Hilt, and I started it from creating a single activity that uses Hilt
I wrote the inject annotation for one of the dependencies inside the activity
I have the provider method(it has a return type) in my HiltApplicationModule
configService is one of the dependencies provideRemoteConfig needs, so for that I also have a provider named method
Moreover, one of the dependencies that RemoteConfig also uses - it uses that same configService that we need to inject
When I run the build, I receive the error
#javax.inject.Named("config_service_merchant") com.example.configapp.ConfigService cannot be provided without an #Provides-annotated method and
public interface ApplicationComponent extends AndroidInjector {
^
#javax.inject.Named("config_service_merchant") com.example.configapp.ConfigService is injected at
At this point, I am confused why it can't find it if I have the method
My assumption that it tries to find it in Dagger2 modules and application components that I still have in the project, but I am not sure.
Why does configService's provider cannot be found to be injected in one of the classes(particularly AnalyticsConfig)?
Related
I am trying to follow the Hilt migration guide here:
https://dagger.dev/hilt/migration-guide.html
And have annotated all my modules with:
#InstallIn(SingletonComponent::class)
However I am running into issues with my "Contributor" Modules for services, fragments and activities.
I have one module for each,
#Module
#InstallIn(SingletonComponent::class)
abstract class FragmentContributorModule {
#ContributesAndroidInjector
internal abstract fun contributeMyFragment(): MyFragment
}
#Module
#InstallIn(SingletonComponent::class)
abstract class ActivityContributorModule {
#ContributesAndroidInjector
internal abstract fun contributeMyActivity(): MyActivity
}
#Module
#InstallIn(SingletonComponent::class)
abstract class ServiceContributorModule {
#ContributesAndroidInjector
internal abstract fun contributeMyService(): MyService
}
During compile I am getting errors for each one of the "contribute" functions:
com.test.ActivityContributorModule_ContributeMyActivity$defaultsDebug is missing an #InstallIn annotation. If this was intentional, see https://dagger.dev/hilt/compiler-options#disable-install-in-check for how to disable this check.
I have also tried to use ServiceComponent::class, FragmentComponent::class and ActivityComponent::class for each Module with no luck. I am trying to migrate the project in pieces so I don't think I can remove these until everything is upgraded to Hilt.
Any ideas?
The answer is here:
Warning: Modules that are not annotated with #InstallIn are not used
by Hilt. Hilt by default raises an error when unannotated modules are
found, but this error can be disabled.
Was not 100% clear to me at first, but the contributor modules I have for services/fragments/activities are only used with Dagger, not Hilt. So if you are trying to migrate you project in pieces you can leave those module as is until you start to provide Hilt entry points for your services/fragments/activities. However if you do that, you will need to tell Hilt to ignore the error it throws for missing #InstallIn.
More info on how to disable that here:
https://dagger.dev/hilt/compiler-options.html#disable-install-in-check
By default, Hilt checks #Module classes for the #InstallIn annotation
and raises an error if it is missing. This is because if someone
accidentally forgets to put #InstallIn on a module, it could be very
hard to debug that Hilt isn’t picking it up.
This check can sometimes be overly broad though, especially if in the
middle of a migration. To turn off this check, this flag can be used:
-Adagger.hilt.disableModulesHaveInstallInCheck=true.
Alternatively, the check can be disabled at the individual module
level by annotating the module with #DisableInstallInCheck.
My Android production is code full of Hilt modules that install various production implementations:
#Module
#InstallIn(ApplicationComponent.class)
public abstract class TimeModule {...}
#Module
#InstallIn(ApplicationComponent.class)
public abstract class DatabaseModule {...}
In all my instrumented tests, I would like to replace those bindings with fakes. My test codebase includes modules that bind fake implementations, but having two modules provide the same class obviously causes compile-time errors.
The Hilt documentation recommends using #UninstallModule(), but that means I'd have to add UninstallModule for every single production module in every single test. That seems like the wrong approach.
How would one normally replace production modules with fake modules? And is there a way to install modules from another module like Guice does, so I could remove #InstallIn from all my production modules and instead simply have one ProductionModule that installs all the individual modules? That would make it easier to just uninstall one module in tests.
How would one normally replace production modules with fake modules?
Probably how it's normally done, is like the documentation said with the UninstallModule annotation. But here is an alternative, which I like to use, using build flavors:
I like to organize my project, so there are mock and live flavors. And there are 3 folders inside my app module: src/main/kotlin with Activities, Fragments etc..., src/mock/kotlin where my fake bindings live, and finally src/live/kotlin where my real production bindings live.
Here's the relevant config from my app level build.gradle.kts:
android {
productFlavors {
flavorDimensions("environment")
register("mock") {
dimension = "environment"
}
register("dev") {
dimension = "environment"
}
register("prod") {
dimension = "environment"
}
sourceSets {
getByName("mock").java.srcDir("src/mock/kotlin")
getByName("dev").java.srcDir("src/live/kotlin")
getByName("prod").java.srcDir("src/live/kotlin")
}
}
}
Project structure overview
Inside the live InteractorModule:
#Module
#InstallIn(ApplicationComponent::class)
abstract class InteractorModule {
#Binds
abstract fun bindTodoInteractor(interactor: TodoInteractorImpl): TodoInteractor
}
Inside the FakeInteractorsModule:
#Module
#InstallIn(ApplicationComponent::class)
abstract class InteractorModule {
#Binds
abstract fun bindTodoInteractor(interactor: TodoInteractorFake): TodoInteractor
}
So now you can use the build variant tab to change between the real and the mock implementations of your interfaces. So if you want to use your fakes inside your instrumentation tests use the mock flavor while running the tests.
One upside of this method, is that by changing the build variant, you can swich between your instrumentation tests using your fakes to using the live implementations. Conversly, you can use your fake implementations inside the actuall application, which can be nice, if you just want to try out the app with mock data.
I hope this helped a bit to at least promote some ideas, on how you can solve this "two implementations for one interface" dilemma!
I have an issue with Dagger and my own generated code.
Assumptions:
I need to generate my own dagger component for UI tests purpose
I have my own Gradle's module for annotation processing which provides dagger component with dependencies. Call this GeneratedTestCoreComponent. This class is generated correctly
GeneratedTestCoreComponent is built at \build\generated\source\kapt\debug\...
GeneratedTestCoreComponent is used in dagger component, smth like this
#Component(modules = [UiTestModule::class],
dependencies = [GeneratedTestCoreComponent::class])
interface TestUiComponent {}
My annotation processor module is correctly added to gradle
implementation project(path: ':processor')
kapt(name: 'processor')
The issue is. During compilation I get below error
TestUiComponent.java:6: error: cannot find symbol
#com.dagger.Component(modules = {com.xxx.xxx.UiTestModule.class}, dependencies = {GeneratedTestCoreComponent.class})
symbol: class GeneratedTestCoreComponent
TestUiComponent.java:8: error: [ComponentProcessor:MiscError] com.dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract interface TestUiComponent
Additional info.
When I copy GeneratedTestCoreComponent class from build directory to src (keeping the same package) and disable my processor, then everything works fine
Try changing kapt(name: 'processor') into kapt project(':processor')
Simple scope annotation:
#Scope
#Retention(RUNTIME)
public #interface SimpleScope {
}
Simple component:
#SimpleScope
#Component
public interface SimpleComponent {
}
compiler error:
error: Scoping annotations are only allowed on concrete types and
#Provides methods
The compiler error is actually coming from Dagger 1 annotation processor. Specifically here: Dagger 1 ValidationProcessor. If the annotation processor sees that any interface is annotated with a javax.inject.Scope annotation, it reports an error. Even if the interface or scope are not used by Dagger 1 in any way.
We are currently migrating from Dagger 1 to Dagger 2, and hence both annotation processors are running on Gradle modules that use both Dagger 1 and Dagger 2.
One solution is to fully migrate each Gradle module, so that both annotation processors don't have to run on the same code. However, this is not always easy.
Our solution was to fork Dagger 1 and modify the ValidationProcessor to not fail if the interface is a Dagger 2 component.
I get following error message from Dagger when compiling the module:
test.MyClass is exclusively members injected and therefore cannot be scoped required by test.MyModule for test.MyModule
Google is no help in this case.
I have a quite simple architecture: 1 module, all injected classes are listed in injects clause. I don't see anything what may cause a problem.
There error seems to be caused by not having an #Inject constructor.
In my case, I was using lombok constructor generation on the class. IE.
#RequiredArgsConstructor(onConstructor = #__({#Inject}))
public class MyClass {
private final AnotherClass anotherClass;
}
And changing to an explicit constructor resolved the problem.
I expect the failure is related to dagger running before the generated code is generated. (Similar to this question and the related issue.)