简体   繁体   中英

Dependency cycle in android hilt

I'm pretty new to Hilt and I wanted to use it in an Android app. The problem is that while building I'm getting a dependency cycle and I'm not really sure how to solve this,.

The basic idea is that I have a local database, and I want to inject it into activities. I have an activity that defines a toolbar and the local DB is needed there for an action triggered by the toolbar, then I have an activity extending that one in which I inject other things.

Here is some code as an example:

@Singleton
public class LocalDatabase{
    ...
}

@Module
@InstallIn(SingletonComponent.class)
public abstract class LocalDatabaseModule {

    @Singleton
    @Binds
    public abstract LocalDatabase bindLocalDatabaseService(LocalDatabase localDatabase);
}

@AndroidEntryPoint
public abstract class ToolbarActivity extends AppCompatActivity {
    @Inject
    LocalDatabase localdb;

    ...
}

@AndroidEntryPoint
public class LoginActivity extends ToolbarActivity {

    @Inject
    LoginService login;

    ...
}

Sadly the activities hierarchy hasn't been decided by me so I cannot modify it (other activities have to extend ToolbarActivity).

The error I get is this one:

public abstract static class SingletonC implements HiltApplication_GeneratedInjector,
                         ^
      app.database.local.LocalDatabase is injected at
          app.hilt.LocalDatabaseModule.bindLocalDatabaseService(localDatabase)
      app.database.local.LocalDatabase is injected at
          app.ToolbarActivity.localdb
      app.LoginActivity is injected at
app.ScrollingActivity_GeneratedInjector.injectScrollingActivity(app.ScrollingActivity)[app.hilt.HiltApplication_HiltComponents.SingletonC ? app.hilt.HiltApplication_HiltComponents.ActivityRetainedC ? app.hilt.HiltApplication_HiltComponents.ActivityC]

I'm really struggling to understand where the cycle is, and as consequence, I am not sure how to avoid this problem. Before introducing the Local DB code, the other injections were working fine.

Hilt needs localDatabase to make bindLocalDatabaseService. i hope below code will work.

        @Module
        @InstallIn(SingletonComponent.class)

        @Singleton
        @Provides 
        public LocalDatabase provideLocalDatabase(){
            return LocalDataBase;
        }
        
        
            @Singleton
            @Binds
            public abstract LocalDatabase bindLocalDatabaseService(LocalDatabase localDatabase){
        }

Solved the issues by creating an interface for the Local DB and using that class for dependency injection. I don't know why I didn't do this since it's what I was doing with the other injected classes.

public interface LocalDatabaseService {
    ...
}

@Singleton
public class LocalDatabase implements LocalDatabaseService {
    @Inject
    public LocalDatabase(){
        ...
    }

    ...
}

@Module
@InstallIn(SingletonComponent.class)
public abstract class LocalDatabaseModule {

    @Singleton
    @Binds
    public abstract LocalDatabaseService bindLocalDatabaseService(LocalDatabase localDatabase);
}

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