简体   繁体   中英

How to use Hilt correctly in two apps with a common library?

I'm developing two apps: one is an admin device for administrators to be able to do some things and the other one is for users with the main functionality of the app.

These two apps have some common activities, methods, fragments... So I created a third one that both use as a library and have some classes extend from those.

I'll call the normal app "App", the admin one "Admin" and the library "Common".

I'm trying to use DI with Hilt, but I need that to be able to work on all 3. I have a Repository in Common with the HiltAndroidApp tag on CommonApp . This one has different modules like a Network one or one for Room db.

On App, I have another repository that extends from the Common one, and AppApp has HiltAndroidApp . I have a new NetworkModule that extends the other one and all that jazz.

When I try to build, I get this error:

error: [Dagger/DuplicateBindings] android.app.Application is bound multiple times:
  public abstract static class SingletonC implements App_GeneratedInjector,
                         ^
      @Provides @Singleton android.app.Application com.adictic.common.util.hilt.AppModule.provideApplication(@dagger.hilt.android.qualifiers.ApplicationContext android.app.Application)
      @Provides android.app.Application dagger.hilt.android.internal.modules.ApplicationContextModule.provideApplication()
      android.app.Application is injected at
          dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.InternalFactoryFactory(application, �)
      dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.InternalFactoryFactory is requested at
          dagger.hilt.android.internal.lifecycle.DefaultViewModelFactories.ActivityEntryPoint.getHiltInternalFactoryFactory() [com.adictic.common.util.App_HiltComponents.SingletonC ? com.adictic.common.util.App_HiltComponents.ActivityRetainedC ? com.adictic.common.util.App_HiltComponents.ActivityC]
  It is also requested at:
      com.adictic.common.util.hilt.Repository(application, �)
  The following other entry points also depend on it:
<bunch of classes>

I imagine there's something wrong with my approach, or that there's someway I don't know to limit each repository to affect their own app, but I can't find anything online. What would be the best way to do all this?

According to that error message, you have code that looks like this:

@Module
class AppModule {
    @Provides
    public Application provideApplication(@ApplicationContext Application application) {
        return application;
    }
}

Hilt provides Application and @ApplicationContext Context by default, not @ApplicationContext Application . This @Provides method attempts to provide the same Application binding that Hilt provides by default, so you have a duplicate binding error. Remove that method to fix the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM