Source:
public void setEncryptionKey_canReOpenRealmWithoutKey() { String username = UUID.randomUUID().toString(); String password = "password"; SyncUser user = SyncUser.login(SyncCredentials.usernamePassword(username, password, true), Constants.AUTH_URL); final byte[] randomKey = TestHelper.getRandomKey(); SyncConfiguration configWithEncryption = configurationFactory.createSyncConfigurationBuilder(user, Constants.USER_REALM) .modules(new StringOnlyModule()) .waitForInitialRemoteData() .errorHandler(new SyncSession.ErrorHandler() { @Override public void onError(@Nonnull SyncSession session, @Nonnull ObjectServerError error) { fail(error.getErrorMessage()); } }) .encryptionKey(randomKey) .build(); Realm realm = Realm.getInstance(configWithEncryption); assertTrue(realm.isEmpty()); realm.beginTransaction(); realm.createObject(StringOnly.class).setChars("Hi Alice"); realm.commitTransaction(); SystemClock.sleep(TimeUnit.SECONDS.toMillis(2)); realm.close(); user.logout(); Realm.deleteRealm(configWithEncryption); user = SyncUser.login(SyncCredentials.usernamePassword(username, password, false), Constants.AUTH_URL); SyncConfiguration configWithoutEncryption = configurationFactory.createSyncConfigurationBuilder(user, Constants.USER_REALM) .modules(new StringOnlyModule()) .waitForInitialRemoteData() .errorHandler(new SyncSession.ErrorHandler() { @Override public void onError(@Nonnull SyncSession session, @Nonnull ObjectServerError error) { fail(error.getErrorMessage()); } }) .build(); realm = Realm.getInstance(configWithoutEncryption); RealmResults<StringOnly> all = realm.where(StringOnly.class).findAll(); assertEquals(1, all.size()); assertEquals("Hi Alice", all.get(0).getChars()); realm.close(); user.logout(); }

Target:
public void setEncryptionKey_canReOpenRealmWithoutKey() { String username = UUID.randomUUID().toString(); String password = "password"; SyncUser user = SyncUser.login(SyncCredentials.usernamePassword(username, password, true), Constants.AUTH_URL); final byte[] randomKey = TestHelper.getRandomKey(); SyncConfiguration configWithEncryption = configurationFactory.createSyncConfigurationBuilder(user, Constants.USER_REALM) .modules(new StringOnlyModule()) .waitForInitialRemoteData() .errorHandler(new SyncSession.ErrorHandler() { @Override public void onError(SyncSession session, ObjectServerError error) { fail(error.getErrorMessage()); } }) .encryptionKey(randomKey) .build(); Realm realm = Realm.getInstance(configWithEncryption); assertTrue(realm.isEmpty()); realm.beginTransaction(); realm.createObject(StringOnly.class).setChars("Hi Alice"); realm.commitTransaction(); SystemClock.sleep(TimeUnit.SECONDS.toMillis(2)); realm.close(); user.logout(); Realm.deleteRealm(configWithEncryption); user = SyncUser.login(SyncCredentials.usernamePassword(username, password, false), Constants.AUTH_URL); SyncConfiguration configWithoutEncryption = configurationFactory.createSyncConfigurationBuilder(user, Constants.USER_REALM) .modules(new StringOnlyModule()) .waitForInitialRemoteData() .errorHandler(new SyncSession.ErrorHandler() { @Override public void onError(SyncSession session, ObjectServerError error) { fail(error.getErrorMessage()); } }) .build(); realm = Realm.getInstance(configWithoutEncryption); RealmResults<StringOnly> all = realm.where(StringOnly.class).findAll(); assertEquals(1, all.size()); assertEquals("Hi Alice", all.get(0).getChars()); realm.close(); user.logout(); }