room database multi instance in aar

27 Views Asked by At

There is an SDK A in which the Room is located

@RestrictTo(RestrictTo.Scope.LIBRARY)
@Module
class DatabaseModule {
    @Provides
    @Singleton
    @CustomDatabase
    fun provideAnalyticsDatabase(config: 
AnalyticsConfig, context: Context) =
        Room.databaseBuilder(
            context,
            AnalyticsDatabase::class.java,
            getDatabaseFileName(config.flowId)
        ).fallbackToDestructiveMigration()
            .build()


    companion object {

        fun getDatabaseFileName(flowId: String): String? {
            val databaseName = "${flowId}.analytics.db"
            Log.d("Database","DatabaseFilename $databaseName")
            return databaseName
        }
    }
}

The developer who will use our SDK will specify the flowId by which we will create the database

There is also SDK B, which uses our SDK A, as well as its own internal database

And both sdks are connected in the application

The problem is that only 1 database is being created (although they should be different, because the flowId in SDK B and Application are different). The database is created from the SDK that is higher in initialization (if SDK B is higher, then the database with its flowId, if SDK A, then with the flowId specified during initialization in application)

And both SDKs access the same database.

Can you advise how to solve this problem?

Some have written that there may be a problem with Dagger, but I'm not sure about it

version Room: 2.5.2

Libraries are distributed as .aar

0

There are 0 best solutions below