So if I'm understanding correctly you just want two files that have the same database schema.
That shouldn't be too hard to achieve.
If you don't have any other dependencies you're not really getting the most bang for your buck with Hilt here.
basic use case
class DbFactory @Inject constructor(
@ApplicationContext context: Context
) {
fun create(dbName: String): MyDatabase = Room
.databaseBuilder(context, MyDatabase::class.java, name = dbName)
.build()
}
With the above just inject the factory and use the same db name each time you want to get it out to read/write the data.
It only really gets fun if you want to inject extra things when creating the db. In which case you could consider something like @AssistedInject
https://dagger.dev/dev-guide/assisted-injection.html
In which case you'd just make a factory with some kind of method to pass in the db name.
Assuming you want some kind of Repository style wrapper around the db to run insert/queries on you could do something along these lines...
@AssistedFactory
interface MyRepositoryLogicFactory {
create(name: String): MyRepositoryLogic
}
class MyRepositoryLogic @AssistedInject constructor(
@ApplicationContext context: Context,
dependencyA: DependencyA,
dependencyB: DependencyB,
@Assisted dbName: String
//add any other injected dependencies here
) {
private val db = Room
.databaseBuilder(context, MyDatabase::class.java, name = dbName)
.build()
//...do whatever with the db in here..
}
same rules apply as the first example. you'd inject the factory into whatever needs rather than the db, and use the same dbName each time you want to generate an instance to do things against.
dbNameto Hilt. Where does it come from? How do you guarantee that it is available before Hilt starts the injection?dbNameis not known until runtime, and will not be known until it is time to access the database.dbNamecomes from. Please edit the question to provide an example of wheredbNameis calculated and where the actual database instance is needed.