我在Android Studio模拟器上进行了一些migration
realm
测试并没有遇到任何问题,但在production
部分设备正在迁移trouble
。
在版本1中:
public class Configuration extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.name("myapp.realm").schemaVersion(1).build();
Realm.setDefaultConfiguration(configuration);
}
}
在版本2中:
public class Configuration extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.name("myapp.realm").schemaVersion(2).migration(new MyMigration()).build();
Realm.setDefaultConfiguration(configuration);
}
private class MyMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 1) {
schema.get(ClassRealm.class.getSimpleName())
.addField(ClassRealm.STRING_FIELD_NAME, String.class);
schema.create(AnotherClassRealm.class.getSimpleName())
.addField(AnotherClassRealm.INTEGER_FIELD_NAME, Integer.class, FieldAttribute.PRIMARY_KEY)
.addField(AnotherClassRealm.STRING_FIELD1_NAME, String.class, FieldAttribute.REQUIRED)
.addField(AnotherClassRealm.STRING_FIELD2_NAME, String.class, FieldAttribute.REQUIRED)
.addField(AnotherClassRealm.DATE_FIELD_NAME, Date.class, FieldAttribute.REQUIRED);
oldVersion++;
}
}
}
}
升级到新版本后出现错误:
设备:Galaxy J5(j5lte), Android:6.0
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmObjectSchema io.realm.RealmObjectSchema.a(java.lang.String, java.lang.Class, io.realm.e[])' on a null object reference
at myapp.configuration.Configuration$MyMigration.migrate(Configuration.java)
at io.realm.BaseRealm$4.onResult(BaseRealm.java)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java)
at <OR>.release(RealmCache.java)
at <OR>.validateConfiguration(RealmCache.java)
at <OR>.invokeWithGlobalRefCount(RealmCache.java)
at <OR>.updateSchemaCache(RealmCache.java)
at <OR>.findColumnIndices(RealmCache.java)
at <OR>.storeColumnIndices(RealmCache.java)
at io.realm.BaseRealm.isInTransaction(BaseRealm.java)
at <OR>.setVersion(BaseRealm.java)
at <OR>.get(BaseRealm.java)
at <OR>.get(BaseRealm.java)
at <OR>.get(BaseRealm.java)
at <OR>.deleteRealm(BaseRealm.java)
at <OR>.migrateRealm(BaseRealm.java)
at io.realm.Realm.init(Realm.java)
at <OR>.createInstance(Realm.java)
at <OR>.initializeRealm(Realm.java)
at <OR>.createObject(Realm.java)
at <OR>.createObjectInternal(Realm.java)
at <OR>.copyToRealm(Realm.java)
at <OR>.copyFromRealm(Realm.java)
at <OR>.where(Realm.java)
at <OR>.executeTransaction(Realm.java)
at <OR>.copyOrUpdate(Realm.java)
at <OR>.createDetachedCopy(Realm.java)
at <OR>.checkMaxDepth(Realm.java)
at <OR>.migrateRealm(Realm.java)
at <OR>.updateSchemaCache(Realm.java)
at <OR>.isInTransaction(Realm.java)
at io.realm.Realm.init(Realm.java)
at <OR>.createInstance(Realm.java)
at <OR>.initializeRealm(Realm.java)
at <OR>.createObject(Realm.java)
at <OR>.createObjectInternal(Realm.java)
at <OR>.copyToRealm(Realm.java)
at <OR>.copyFromRealm(Realm.java)
at <OR>.where(Realm.java)
at <OR>.executeTransaction(Realm.java)
at <OR>.copyOrUpdate(Realm.java)
at <OR>.createDetachedCopy(Realm.java)
at <OR>.checkMaxDepth(Realm.java)
at <OR>.migrateRealm(Realm.java)
at <OR>.updateSchemaCache(Realm.java)
at <OR>.isInTransaction(Realm.java)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java)
at <OR>.release(RealmCache.java)
at <OR>.validateConfiguration(RealmCache.java)
at <OR>.invokeWithGlobalRefCount(RealmCache.java)
at <OR>.updateSchemaCache(RealmCache.java)
at <OR>.findColumnIndices(RealmCache.java)
at <OR>.storeColumnIndices(RealmCache.java)
at io.realm.Realm.getDefaultInstance(Realm.java)
at myapp.MyView.onCreate(MyView.java)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
另: 设备:Galaxy S3 Neo(s3ve3gds),Android:4.4
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.NullPointerException
at myapp.configuration.Configuration$MyMigration.migrate(Configuration.java)
at io.realm.BaseRealm$4.onResult(BaseRealm.java)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java)
at <OR>.release(RealmCache.java)
at <OR>.validateConfiguration(RealmCache.java)
at <OR>.invokeWithGlobalRefCount(RealmCache.java)
at <OR>.updateSchemaCache(RealmCache.java)
at <OR>.findColumnIndices(RealmCache.java)
at <OR>.storeColumnIndices(RealmCache.java)
at io.realm.BaseRealm.isInTransaction(BaseRealm.java)
at <OR>.setVersion(BaseRealm.java)
at <OR>.get(BaseRealm.java)
at <OR>.get(BaseRealm.java)
at <OR>.get(BaseRealm.java)
at <OR>.deleteRealm(BaseRealm.java)
at <OR>.migrateRealm(BaseRealm.java)
at io.realm.Realm.init(Realm.java)
at <OR>.createInstance(Realm.java)
at <OR>.initializeRealm(Realm.java)
at <OR>.createObject(Realm.java)
at <OR>.createObjectInternal(Realm.java)
at <OR>.copyToRealm(Realm.java)
at <OR>.copyFromRealm(Realm.java)
at <OR>.where(Realm.java)
at <OR>.executeTransaction(Realm.java)
at <OR>.copyOrUpdate(Realm.java)
at <OR>.createDetachedCopy(Realm.java)
at <OR>.checkMaxDepth(Realm.java)
at <OR>.migrateRealm(Realm.java)
at <OR>.updateSchemaCache(Realm.java)
at <OR>.isInTransaction(Realm.java)
at io.realm.Realm.init(Realm.java)
at <OR>.createInstance(Realm.java)
at <OR>.initializeRealm(Realm.java)
at <OR>.createObject(Realm.java)
at <OR>.createObjectInternal(Realm.java)
at <OR>.copyToRealm(Realm.java)
at <OR>.copyFromRealm(Realm.java)
at <OR>.where(Realm.java)
at <OR>.executeTransaction(Realm.java)
at <OR>.copyOrUpdate(Realm.java)
at <OR>.createDetachedCopy(Realm.java)
at <OR>.checkMaxDepth(Realm.java)
at <OR>.migrateRealm(Realm.java)
at <OR>.updateSchemaCache(Realm.java)
at <OR>.isInTransaction(Realm.java)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java)
at <OR>.release(RealmCache.java)
at <OR>.validateConfiguration(RealmCache.java)
at <OR>.invokeWithGlobalRefCount(RealmCache.java)
at <OR>.updateSchemaCache(RealmCache.java)
at <OR>.findColumnIndices(RealmCache.java)
at <OR>.storeColumnIndices(RealmCache.java)
at io.realm.Realm.getDefaultInstance(Realm.java)
at myapp.MyView.onCreate(MyView.java)
at android.app.Activity.performCreate(Activity.java:5582)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(NativeStart.java)
所有设备:
答案 0 :(得分:1)
使用ProGuard时,会对模型类进行模糊处理。
这意味着ClassRealm.class.getSimpleName()
不会返回"ClassRealm"
。
这是预期的行为。
当然,您可以通过添加自己的proguard配置来保留模型类的名称。
答案 1 :(得分:0)
如果您不太关心公开逻辑,因为Proguard不会混淆该类,则可以在整个MyMigration类上使用@keep批注。
import android.support.annotation.Keep
@Keep
private class MyMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 1) {
schema.get(ClassRealm.class.getSimpleName())
.addField(ClassRealm.STRING_FIELD_NAME, String.class);
schema.create(AnotherClassRealm.class.getSimpleName())
.addField(AnotherClassRealm.INTEGER_FIELD_NAME, Integer.class, FieldAttribute.PRIMARY_KEY)
.addField(AnotherClassRealm.STRING_FIELD1_NAME, String.class, FieldAttribute.REQUIRED)
.addField(AnotherClassRealm.STRING_FIELD2_NAME, String.class, FieldAttribute.REQUIRED)
.addField(AnotherClassRealm.DATE_FIELD_NAME, Date.class, FieldAttribute.REQUIRED);
oldVersion++;
}
}
}