【问题标题】:Testing room migration, Cannot find the schema file in the assets folder [room-migration]测试室迁移,在资产文件夹 [room-migration] 中找不到架构文件
【发布时间】:2019-11-01 18:15:01
【问题描述】:

这个问题是在我决定向房间数据库添加另一个实体之后出现的。架构正在导出到预期的目录中。所有 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

事实上,两个 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"
}

我还注意到 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)

【问题讨论】:

  • 我也遇到了同样的问题,你找到问题了吗?

标签: android android-room


【解决方案1】:

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

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

时髦

    sourceSets {
        debug.assets.srcDirs += files("$projectDir/schemas".toString())
    }

Kotlin DSL

    sourceSets {
        getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
    }

【讨论】:

  • 完美!感谢分享!
【解决方案2】:

还请确保在创建MigrationTestHelper 时插入实际的database.class。我感到困惑并设置了我的Entity.class,导致同样的错误。

【讨论】:

    【解决方案3】:

    你应该把它包装在 android 中:

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

    然后构建项目

    【讨论】:

      【解决方案4】:

      确保您在 schemas/ 文件夹下导出了两个架构。例如,如果您测试从版本 1 到版本 2 的迁移,则必须在 schemas/ 目录中同时导出 1.json 和 2.json 文件。

      【讨论】:

        【解决方案5】:

        我最近遇到这个错误,发现它是由我的数据库类的包更改引起的。

        为了解决这个问题,您必须将所有 json 文件从旧架构包位置复制到新位置:在架构目录中,迁移文件可能位于子目录名称 com.app.old .MyDB。将所有 json 文件复制到 schemas 文件夹示例 com.app.new.MyDB 的子目录中(您可能需要创建此目录)。

        com.app... 指的是您的 DB 类的包名。

        【讨论】:

          【解决方案6】:

          试试这个:

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

          根据the documentation,参数是

          一个 CharSequence,包括 String 或 GS​​tring。根据文件(对象)解释相对到项目目录

          (强调我的)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-07
            • 2012-03-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多