简体   繁体   English

Android + Kotlin + 刀柄:如何提供。 SQLite数据库

[英]Android + Kotlin + Hilt: How to provide. SQLiteDatabase

I'm implementing Hilt in my multi-module Android app and not being able to provide/inject SQLiteDatabase (I was trying to look for help before posting, but none found regarding SQLiteDatabase).我在我的多模块 Android 应用程序中实现 Hilt 并且无法提供/注入 SQLiteDatabase(我在发布之前试图寻求帮助,但没有找到关于 SQLiteDatabase 的帮助)。

My first attempt was the next:我的第一次尝试是下一个:

@Module
@InstallIn(SingletonComponent::class)
class Database {

    @Provides
    @Singleton
    fun provideDatabase(database: SQLiteDatabase): SQLiteDatabase {
        return database
    }
}

But the builder complains:但是建设者抱怨:

error: [Dagger/MissingBinding] android.database.sqlite.SQLiteDatabase cannot be provided without an @Inject constructor or an @Provides-annotated method.

I thought that might be:我认为这可能是:

@Provides
@Singleton
fun provideDatabase(): SQLiteDatabase {
    return SQLiteDatabase()
}

But now it complains init is private in SQLiteDatabase.但现在它抱怨 init 在 SQLiteDatabase 中是私有的。

I'm stuck, and I don't know how to provide database to repositories, what I'm trying to do in constructor this way:我被卡住了,我不知道如何为存储库提供数据库,我试图在构造函数中这样做:

class TrackRepository
@Inject constructor(
    private var database: SQLiteDatabase) {
    ...
}

Edit 1: Moving the database "Provides" from Repository module to app module the error is completely different.编辑1:将数据库“提供”从存储库模块移动到应用程序模块,错误完全不同。 Now it complains about a dependency cycle, but for me it's difficult to understand the error message clearly:现在它抱怨依赖循环,但对我来说很难清楚地理解错误消息:

/app/build/generated/hilt/component_sources/debug/com/myapp/myapp/app/AWApplication_HiltComponents.java:135: error: [Dagger/DependencyCycle] Found a dependency cycle:
  public abstract static class SingletonC implements AWApplication_GeneratedInjector,
                         ^
      android.database.sqlite.SQLiteDatabase is injected at
          com.myapp.myapp.app.Dependencies.provideDatabase(database)
      android.database.sqlite.SQLiteDatabase is injected at
          com.myapp.myapp.app.Dependencies.provideDatabase(database)
      ...
  
The cycle is requested via:
  android.database.sqlite.SQLiteDatabase is injected at
      com.artandwords.repository.local.ThoughtRepository(database, …)
  com.artandwords.repository.local.ThoughtRepository is injected at
      services.ThoughtService(repository)
  services.ThoughtService is injected at
      com.myapp.myapp.activities.thought.DisplayThoughtViewModel(…, thoughtService, …)
  com.myapp.myapp.activities.thought.DisplayThoughtViewModel is injected at
      com.myapp.myapp.activities.thought.DisplayThoughtViewModel_HiltModules.BindsModule.binds(arg0)
  @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
      dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.myapp.myapp.app.AWApplication_HiltComponents.SingletonC → com.myapp.myapp.app.AWApplication_HiltComponents.ActivityRetainedC → com.myapp.myapp.app.AWApplication_HiltComponents.ViewModelC]

Ok, I found the solution myself.好的,我自己找到了解决方案。 Instead of trying to provide SQLiteDatabase directly I tried providing my SQLiteHelper, which extends SQLiteOpenHelper, and so I have access to readableDatabase and writableDatabase.我没有尝试直接提供 SQLiteDatabase,而是尝试提供我的 SQLiteHelper,它扩展了 SQLiteOpenHelper,因此我可以访问 readableDatabase 和 writableDatabase。

Not yet sure if it'll work, because the "provideDatabase" method is not marked in yellow in Android Studio (in the sense of being referenced by any class), but at least now the app builds.尚不确定它是否会起作用,因为 Android Studio 中的“provideDatabase”方法未标记为黄色(在被任何类引用的意义上),但至少现在该应用程序已构建。

@Module
@InstallIn(SingletonComponent::class)
class Database {

    @Provides
    @Singleton
    fun provideDatabase(database: SQLiteHelper): SQLiteHelper {
        return database
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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