We currently have 3 modules: App, Api and Core.
App depends on Api and Core.
Api depends on Core.
在我们的核心模块中,我们有一个VersionProvider:
class VersionProvider @Inject constructor(@ApplicationContext context: Context) {
val versionName = context.packageManager.getPackageInfo(context.packageName, 0).versionName
val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
context.packageManager.getPackageInfo(context.packageName, 0).longVersionCode
} else {
@Suppress("DEPRECATION")
context.packageManager.getPackageInfo(context.packageName, 0).versionCode
}
}
它被注入到类中,包括Api模块和App模块.
核心模块刀柄模块的定义如下:
@Module
@InstallIn(SingletonComponent::class)
abstract class CoreModule {
@Singleton
@Binds
abstract fun bindVersionProvider(
provider: VersionProvider
): VersionProvider
}
在编译时,我们收到以下错误:
C:\Code\xxx\app\build\generated\hilt\component_sources\development\com\xxx\App_HiltComponents.java:200: 错误:[Dagger/DependencyCycle]找到一个依赖循环: 公共 抽象静态类SingletonC实现App_GeneratedInjector, ^ com.xxx.core.VersionProvider在 com.xxx.core.hilt.CoreModule.bindVersionProvider(provider) com.xxx.core.VersionProvider在 com.xxx.core.hilt.CoreModule.bindVersionProvider(provider) ... 通过以下方式请求循环: com.xxx.core.VersionProvider在 com.xxx.api.core.hilt.ApiClientModule.bindNetworkManager(�,versionProvider) com.xxx.api.services.helpers.INetworkManager被注入到 com.xxx.api.core.hilt.ApiClientModule.bindInventoryApi(networkManager) 在以下位置注入com.xxx.api.services.inventory.InventoryApi: com.xxx.api.services.inventory.InventoryService(api,�) 在以下位置注入com.xxx.api.services.inventory.InventoryService com.xxx.api.core.hilt.ServiceModule.bindInventoryService(服务) com.xxx.api.services.inventory.IInventoryService在 com.xxx.managers.inventory.InventoryManager(inventoryService,�) 在以下位置注入com.xxx.managers.inventory.InventoryManager com.xxx.core.hilt.ManagerModule.bindInventoryManager(管理器) com.xxx.managers.inventory.IInventoryManager在 com.xxx.App.inventoryManager com.xxx.应用程序被注入 com.xxx.App_GeneratedInjector.injectApp(com.xxx.App) 以下其他入口点也依赖于它: dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.xxx.App_HiltComponents.SingletonC? com.xxx.App_HiltComponents.ActivityRetainedC? com.xxx.App_HiltComponents.ViewModelC] 1错误
在App和API模块中,以相同的方式定义Hilt模块,例如:
@Module
@InstallIn(SingletonComponent::class)
object ApiClientModule {
@Provides
@Singleton
fun bindNetworkManager(
settings: ISettings,
versionProvider: VersionProvider
): INetworkManager {
return NetworkManager(settings, versionProvider)
}
...
}
I think the problem has to do with the fact that VersionProvider is somehow registered mulitple times in the Hilt container as it is referenced both from the App and Api modules.
But I don't know what Hilt configuration I am missing to make it work.
Any clues?