Testing - Android SDK

See also:

See the Unit Testing Example App for information on how Realm can be combined with JUnit3, JUnit4, Robolectric, Mockito and PowerMock.


The Android Studio debugger can provide misleading values for Realm object fields. For correct values, you can watch accessor values instead, or use the Realm object toString() method to see the latest field values.

This section details information you should keep in mind when debugging Realm applications with Android Studio to avoid incorrect value reporting. For example, adding a watch in Android Studio on a Realm object displays values of the fields. These values are wrong because the field values are not used. Realm creates a proxy object behind the scenes and overrides the getters and setters in order to access the persisted data in the realm. Add a watch on any of the accessors to see the correct values. See the image below:

When using the Android Studio debugger, accessors display the correct field value, while fields show outdated information.

In the image above, the debugger has stopped on line 113. There are three watch values:

  • The person variable
  • The person.getName() accessor
  • The person.getAge() accessor

The code from lines 107 to 111 alters the person instance by changing the name and age in a write transaction. On line 113, the person watch instance reports incorrect values for the field watch values. The watch values that use the accessors report values that are correct.

The Realm Android SDK library contains native code. Debugging NDK crashes can be cumbersome as the default stack trace provides minimal information.

We recommend you use a crash reporting tool such as Crashlytics to track native errors and gather other valuable information so we are in a better position to help you if something goes wrong.

To enable NDK crash reporting in Crashlytics for your project, add the following to the root of your application build.gradle file:

crashlytics {
enableNdk true

The values androidNdkOut and androidNdkLibsOut are not needed.

Give Feedback