Android Room无法创建用于测试迁移的JSON模式

Zoo*_*key 6 android android-room

我已经创建了从1到2版本的数据库迁移。

我在以下几个模块中安装了该应用程序:

  • 应用程式
  • 数据

我尝试将其添加到应用程序和数据模块的build.gradle中:

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

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

这是我的MigrationTest类:

@RunWith(AndroidJUnit4.class)
public class MigrationTest {

private static final String TEST_DB = "migration-test";

@Rule public MigrationTestHelper helper;

private Profile profile;

public MigrationTest() {
helper = new MigrationTestHelper(
    InstrumentationRegistry.getInstrumentation(),
    AppDatabase.class.getCanonicalName(),
    new FrameworkSQLiteOpenHelperFactory());
}

@Before
public void setUp(){
profile = createProfile();
}

@Test public void migrate1To2() throws IOException {
    SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
    insertProfile(db);
    db.close();
    AppDatabase database = (AppDatabase) helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);
    Single<ProfileData> profileDataSingle = database.profileDao().getById("userId");
    ProfileData profileData = profileDataSingle.blockingGet();
    Profile currentProfile = ProfileMapper.transform(profileData);
    assertEquals(currentProfile.getUserId(), profile.getUserId());
}
Run Code Online (Sandbox Code Playgroud)

测试失败:

java.io.FileNotFoundException:在资产文件夹中找不到架构文件。确保在测试断言输入中包括导出的json模式。有关 详细信息,请参见 https://developer.android.com/topic/libraries/architecture/room.html#db-migration-testing。缺少文件:org.app.app.data.sql.AppDatabase / 1.json

Evg*_*bei 7

对于 Kotliner:

android{
    defaultConfig {
        // ...
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas")
            }
        }
    }
    sourceSets {
        getByName("androidTest"){
            assets.srcDirs(File(projectDir, "schemas"))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


han*_*ach 5

对我来说,当我添加这个时,它会有所帮助(我只是忘记了它)。也许它可以帮助某人

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