Room database + unit tests + AS = NoSuchFieldError: useNoBackupDirectory

Viewed 89

In our app, we use Room database for some unit tests like below.

   fun provideMyDatabase(...): MyDatabase {
        return Room.inMemoryDatabaseBuilder(application, MyDatabase::class.java)
            .allowMainThreadQueries()
            .build()
    }

When I try to run unit tests in Android Studio on my Mac I get the error:

java.lang.NoSuchFieldError: useNoBackupDirectory

These unit tests work when I run them from the command line.

I know that this is not recommended how to test database, I assume that SQLite implementation is the source of the problem, but also I am looking for some docs where I can find the exact root of the problem.

Adding the below dependency will fix the problem. Only when I use implementation, not testImplementation.

implementation("androidx.sqlite:sqlite:2.1.0")

Source about testing room: https://developer.android.com/training/data-storage/room/testing-db

My setup: Android Studio 4.1.2, OSX 11.1, Java 8, Kotlin 1.4.21

0 Answers
Related