AndroidManifest.xml 中的
android:allowBackup="true" 可防止在卸载应用后清除数据。
将此添加到您的清单中:
android:allowBackup="false"
并重新安装应用程序。
注意:如果您想要自动备份,请确保稍后将其更改回 true。
另一种解决方案:
检查旧 json 文件的 identityHash 和 apps\schema 文件夹中的新 json 文件。
如果identityHash不同,就会报错。如果您不想更改任何内容,请通过比较两个 json 文件找出您更改的内容。
确保您有 exportSchema = true。
@Database(entities = {MyEntity.class, ...}, version = 2, exportSchema = true)
json 架构文件:
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "53cc5ef34d2ebd33c8518d79d27ed012",
"entities": [
{
代码:
private void checkIdentity(SupportSQLiteDatabase db) {
String identityHash = null;
if (hasRoomMasterTable(db)) {
Cursor cursor = db.query(new SimpleSQLiteQuery(RoomMasterTable.READ_QUERY));
//noinspection TryFinallyCanBeTryWithResources
try {
if (cursor.moveToFirst()) {
identityHash = cursor.getString(0);
}
} finally {
cursor.close();
}
}
if (!mIdentityHash.equals(identityHash) && !mLegacyHash.equals(identityHash)) {
throw new IllegalStateException("Room cannot verify the data integrity. Looks like"
+ " you've changed schema but forgot to update the version number. You can"
+ " simply fix this by increasing the version number.");
}
}