This issue came after I decided to add another entity to the room database. The schema is being exported in the expected directory. All build.gradle setting is done and seems to be working but is not. Since I got:
java.io.FileNotFoundException: Cannot find the schema file in the assets folder. Make sure to include the exported json schemas in your test assert inputs.
See https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schema for details. Missing file: com.company.companyapp.db.AppStore/1.json
In fact both json schemas are being generated but the test runner is not able to find such files. Here is the gradle setup:
testOptions.unitTests.includeAndroidResources = true
defaultConfig {
...
multiDexEnabled true
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/store".toString()]
}
}
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
android.compileOptions.sourceCompatibility 1.8
android.compileOptions.targetCompatibility 1.8
}
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/store".toString())
}
dependencies {
def roomVersion = '2.2.1'
// Other deps ...
implementation "androidx.room:room-runtime:$roomVersion"
implementation "androidx.room:room-rxjava2:$roomVersion"
annotationProcessor "androidx.room:room-compiler:$roomVersion"
testImplementation "androidx.room:room-testing:$roomVersion"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "androidx.test:core:$testCoreVersion"
androidTestImplementation "androidx.test.ext:junit:$extJUnitVersion"
testImplementation "androidx.test:core:$testCoreVersion"
testImplementation "androidx.test.ext:junit:$extJUnitVersion"
testImplementation "org.robolectric:robolectric:4.2.1"
}
I also note the Studio IDE marks the json schema directory holder
By looking at the google room migration examples I can see there is a difference, and not talking about the name.
So it's very clear the gradle plugin is doing something different than I spect or the documentation says it should work, but it doesn't.
at androidx.room.testing.MigrationTestHelper.loadSchema(MigrationTestHelper.java:320)
at androidx.room.testing.MigrationTestHelper.createDatabase(MigrationTestHelper.java:152)
at com.company.companyapp.MigrationTest.migrate1To2(MigrationTest.java:32)
Export schemasThe exported JSON files represent your database's schema history. You should store these files in your version control system, as it allows Room to create older versions of the database for testing purposes.
fallbackToDestructiveMigration() Allows Room to destructively recreate database tables if Migration s that would migrate old database schemas to the latest schema version are not found.
Using this workaround found by PaulWoitaschek works for me with Robolectric 4.3:
add schemas dir on debug souceSets on your app's build.gradle
Groovy
sourceSets {
debug.assets.srcDirs += files("$projectDir/schemas".toString())
}
Kotlin DSL
sourceSets {
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
}
Please also make sure while creating the MigrationTestHelper
to insert the actual database.class
. I got confused and set my Entity.class
instead, leading to the same error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With