测试房间迁移,在assets文件夹中找不到schema文件[room-migration]

AXS*_*XSM 14 android android-room

这个问题是在我决定向房间数据库添加另一个实体之后出现的。架构正在预期的目录中导出。所有 build.gradle 设置都已完成并且似乎正在工作但没有。自从我得到:

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
Run Code Online (Sandbox Code Playgroud)

实际上,正在生成两个 json 模式,但测试运行程序无法找到此类文件。这是gradle设置:

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"
}
Run Code Online (Sandbox Code Playgroud)

我还注意到 Studio IDE 标记了 json 模式目录持有者 在此处输入图片说明

通过查看 google 房间迁移示例,我可以看到存在差异,而不是谈论名称。
在此处输入图片说明

因此,很明显 gradle 插件正在做与我认为不同的事情,或者文档说它应该可以工作,但事实并非如此。

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)
Run Code Online (Sandbox Code Playgroud)

小智 11

使用PaulWoitaschek发现的这个解决方法对我来说适用于 Robolectric 4.3:

在您的应用程序的调试源集上添加架构目录 build.gradle

常规

    sourceSets {
        debug.assets.srcDirs += files("$projectDir/schemas".toString())
    }
Run Code Online (Sandbox Code Playgroud)

科特林 DSL

    sourceSets {
        getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
    }
Run Code Online (Sandbox Code Playgroud)


Evg*_*bei 0

你应该将它包装在android中:

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

然后构建项目