AXSM
AXSM

Reputation: 1172

Testing room migration, Cannot find the schema file in the assets folder [room-migration]

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 enter image description here

By looking at the google room migration examples I can see there is a difference, and not talking about the name.
enter image description here

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)

Upvotes: 21

Views: 5053

Answers (6)

Griffin OG
Griffin OG

Reputation: 1

I experienced this error recently and found that it was caused by a change in package of my database class.

In order to resolve this you'll have to copy all json files from the old schema package location to the new one: Inside the schemas directory the migration files may be located in a subdirectory name com.app.old.MyDB. Copy all the json files into the subdirectory in the schemas folder example com.app.new.MyDB (You may need to create this directory).

The com.app... refers to the package name of your DB class.

Upvotes: 0

aguiarroney
aguiarroney

Reputation: 121

Make sure that you have both schemas exported under the schemas/ folder. For instance, if your testing the migration from version 1 to version 2, you must have both 1.json and 2.json files exported on the schemas/ directory.

Upvotes: 0

LM_IT
LM_IT

Reputation: 197

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.

Upvotes: 1

jyodoid
jyodoid

Reputation: 321

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
    }

Upvotes: 29

Giorgos Kylafas
Giorgos Kylafas

Reputation: 2381

Try using this:

sourceSets {
   androidTest.assets.srcDirs += files("store".toString())
}

According to the documentation, the argument is

A CharSequence, including String or GString. Interpreted relative to the project directory, as per file(Object)

(emphasis mine)

Upvotes: -1

Evgenii Vorobei
Evgenii Vorobei

Reputation: 1569

You should wrap it in android:

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation":
                             "$projectDir/schemas".toString()]
            }
        }
    }
}

Then build the project

Upvotes: -1

Related Questions