I've got a really annoying problem with Realm and Android Studio. About every third time I compile my project I get the following exception:
java.lang.RuntimeException: Unable to create application com.example.MyApp: io.realm.exceptions.RealmException: class com.example.MyApp.model.MyObject is not part of the schema for this Realm.
I understand that this is due to the annotation processor not correctly doing its thing. So currently each time I get the error, I run a full clean/rebuild and then it's working fine. But that is a bit of a pain. How can I fix it so that I do not have to clean/rebuild all the time?
This issue actually started appearing just recently and on both of the development machines that I'm using. A few weeks ago it never has happened.
In Android Studio I have "Enable annoation processing" enabled under "Default Preferences" > "Build, Execution, Deployment" > "Compiler" > "Annotation Processors".
Any idea what could be missing?
Realm version(s): 0.87.2
Android Studio version: 1.5.1
I really can't see the "Annotation Processor" option in Android Studio. As far as I remember that option has been removed more than a year ago, but you say you are using version 1.5.1 which is from December 2015.
Anyway, you don't need to inform your IDE about annotation processing. All of that is already handled by the build system. Does removing those settings help in any way?
Actually I have not had that setting enabled before. I've found and enabled it after this issue started appearing in the hope to solve it.
Now I've unchecked it again but the problem persists :(

That looks like the preferences panel of Intellij IDEA, not Android Studio. Which are you using?
It's Android Studio...

Hmmm... I guess they removed that in the canary channel a long time ago. Newer version of AS don't have that options. Anyway, the IDE is really not important. Gradle is the only one in charge of the annotation processing.
Do you have a complete stacktrace?
It's not under "Android Studio" > "Preferences..." but under "File" > "Other Settings" > "Default Settings...".
Here's the full log output:
04-28 10:20:21.531 17680-17680/? I/art: Late-enabling -Xcheck:jni
04-28 10:20:21.570 17680-17680/com.example.MyApp.DevLocal I/GMPM: App measurement is starting up, version: 8487
04-28 10:20:21.570 17680-17680/com.example.MyApp.DevLocal I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
04-28 10:20:21.688 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$4>
04-28 10:20:21.688 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$4>
04-28 10:20:21.689 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$3>
04-28 10:20:21.689 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$3>
04-28 10:20:21.689 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$2>
04-28 10:20:21.689 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$2>
04-28 10:20:21.690 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$1>
04-28 10:20:21.690 17680-17680/com.example.MyApp.DevLocal I/art: Rejecting re-init on previously-failed class java.lang.Class<io.realm.rx.RealmObservableFactory$1>
04-28 10:20:21.743 17680-17680/com.example.MyApp.DevLocal D/AndroidRuntime: Shutting down VM
04-28 10:20:21.743 17680-17680/com.example.MyApp.DevLocal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.MyApp.DevLocal, PID: 17680
java.lang.RuntimeException: Unable to create application com.example.MyApp.Application.MyApp: io.realm.exceptions.RealmException: class com.example.MyApp.Model.TMSettings is not part of the schema for this Realm.
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4556)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: io.realm.exceptions.RealmException: class com.example.MyApp.Model.TMSettings is not part of the schema for this Realm.
at io.realm.internal.RealmProxyMediator.getMissingProxyClassException(RealmProxyMediator.java:162)
at io.realm.DefaultRealmModuleMediator.getTableName(DefaultRealmModuleMediator.java:81)
at io.realm.RealmSchema.getSchemaForClass(RealmSchema.java:216)
at io.realm.RealmQuery.<init>(RealmQuery.java:132)
at io.realm.RealmQuery.createQuery(RealmQuery.java:81)
at io.realm.Realm.where(Realm.java:879)
at com.example.MyApp.Application.TrustMessenger.onCreate(TrustMessenger.java:85)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)聽
at android.os.Handler.dispatchMessage(Handler.java:102)聽
at android.os.Looper.loop(Looper.java:135)聽
at android.app.ActivityThread.main(ActivityThread.java:5254)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at java.lang.reflect.Method.invoke(Method.java:372)聽
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)聽
When that happens, can you take a look at the generated DefaultRealmModuleMediator.java and post its contents?
Sure, here it is...
Ok, so it only contains references to TMMessage and TMChat. Do you have any other model classes apart from TMSettings?
Yep, about 15 of them...
Can you find the generated proxy classes for those models or are those missing as well?
I've got 14 generated TM*RealmProxy.java classes but only two of them (TMMessage and TMChat as said above) are in DefaultRealmModuleMediator.java. Should they be all in there?
Yes they should. In Realm 0.88 we introduces the Realm plugin which uses a different mechanism for annotation processing. We no longer include the annotation processor in the AAR file, but we have a separate module for it and let the apt plugin manage it. I wonder if it would make a difference in your case.
So you want me to upgrade Realm to 0.88 (or the latest version)?
It would be interesting to know if it makes a difference at least. Latest version is 0.89.1 and note that the installation has changed slightly: https://realm.io/docs/java/latest/#installation
I've upgraded to 0.89.1 and it just did happen again :(
In the Gradle output you should be able to see something like
Note: Processing class Cat
Note: Processing class Person
Note: Processing class Dog
Note: Creating DefaultRealmModule
Could you please share that output?
Note: Processing class TMChat
Note: Processing class TMMessage
Note: Creating DefaultRealmModule
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
So there's no mention of the missing model classes. This is weird. Is there any chance you could share your project (or a sample project that reproduces the issue) with us, even in a private way? you could email it to [email protected]
Unfortunately I cannot share it and the project is rather large, I will not be able to produce a sample project with the same issue I guess. Can I do anything more to help you debug it?
Ok, let's see. It looks like your model classes are not being caught by roundEnv.getElementsAnnotatedWith(RealmClass.class) (https://github.com/realm/realm-java/blob/master/realm/realm-annotations-processor/src/main/java/io/realm/processor/RealmProcessor.java#L125). What version of the JVM are you using?
jdk1.7.0_71 on OS X 10.11.4.
Ok... No JDK6 madness either. Do your other classes (apart from TMChat and TMMessage) look any different from the two working ones? Maybe you could send all the model classes to email address I mentioned above?
Ok, I'll send you one working and one not working model class.
Ok, I saw the classes and I saw nothing wrong with them (but I think you will love the new features we introduces in 0.88 to simplify your code).
My next idea is trying to add the @RealmClass annotation to your model classes and see if that makes a difference.
I've added @RealmClass but it just did happen again :(
Just to rule out any issues with the editor, can you install the latest version of Android Studio (2.1) and try on that? It should be possible to have both 1.5.1 and 2.1 installed at the same time?
Also, does it happen both if you launch the build from Android Studio or the command line?
Eehm, could you tell me _how_ to launch the build from the command line?
from the root of your project: ./gradlew assemble should to the trick
By doing that I've just seen this:
...
:app:processDevLocalDebugManifest UP-TO-DATE
:app:processDevLocalDebugResources UP-TO-DATE
:app:generateDevLocalDebugSources UP-TO-DATE
:app:compileDevLocalDebugJavaWithJavac
Note: Processing class TMChat
Note: Processing class TMMessage
Note: Creating DefaultRealmModule
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Incremental compilation of 28 classes completed in 4.058 secs.
:app:compileDevLocalDebugNdk UP-TO-DATE
:app:compileDevLocalDebugSources
:app:prePackageMarkerForDevLocalDebug
:app:transformClassesWithRealmTransformerForDevLocalDebug
:app:transformClassesWithDexForDevLocalDebug
:app:mergeDevLocalDebugJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDevLocalDebug UP-TO-DATE
:app:processDevLocalDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDevLocalDebug UP-TO-DATE
:app:validateDebugSigning
:app:packageDevLocalDebug
:app:zipalignDevLocalDebug
:app:fullDevLocalDebugBuildInfoGenerator
:app:assembleDevLocalDebug
:app:checkDevRemoteDebugManifest
:app:prepareDevRemoteDebugDependencies
:app:compileDevRemoteDebugAidl
:app:compileDevRemoteDebugRenderscript
:app:generateDevRemoteDebugBuildConfig
:app:generateDevRemoteDebugAssets UP-TO-DATE
:app:mergeDevRemoteDebugAssets
:app:generateDevRemoteDebugResValues
:app:processDevRemoteDebugGoogleServices
:app:generateDevRemoteDebugResources
:app:mergeDevRemoteDebugResources
:app:processDevRemoteDebugManifest
:app:processDevRemoteDebugResources
:app:generateDevRemoteDebugSources
:app:compileDevRemoteDebugJavaWithJavac
:app:compileDevRemoteDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Note: Processing class TMSettings
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Processing class TM...
Note: Creating DefaultRealmModule
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
...
The two working model classes are processed first, then way later all the others... might this be of interest?
Oh yes! It looks like you are using build flavours right? And each build flavour defines its own sourceset as far as I can tell. I wonder if this is something that gets the Gradle daemon confused. Which version of Gradle are you using?
Yes, I'm having four different flavours. They each only define just one class with a few public static final String variables.
I'm using Gradle 2.10. It was 2.08 before but then I was promoted to upgrade to 2.10 by some component. It _could_ be that this problem started appearing after the Gradle 2.10 upgrade actually.
It's still kind of weird that the different flavours trigger the processing of different model classes. Do they reside in the same folder?
All model classes are in the same folder, yes.
This line makes me suspicious: Incremental compilation of 28 classes completed in 4.058 secs.
Did you turn incremental compilation on? I can see that causing issues with the annotation processor.
I don't have dexOptions { incremental true } if that's what you mean. Other than that, I'm not aware of any incremental compilation setting. Where should I check?
No, it's something that was introduced back in Gradle 2.1 (https://docs.gradle.org/2.1/release-notes#incremental-java-compilation). Maybe Android Studio turns it on by default. Do you get consistently working builds with the command line or does it behave exactly the same as Android Studio?
When I ./gradlew install it seems to always process all the model classes.
When I ./gradlew installJustTheFlavourThatIWant I get the same problem with not all model classes getting processed.
I'm starting to this this is a Gradle/Android plugin bug/configuration issue and not so related to Realm.
I would love to see your build.gradle file. Also, what version of the Android Gradle plugin are you using?
Do you need the project level or the app level build.gradle? I can mail them to you. My Gradle plugin is classpath 'com.android.tools.build:gradle:1.5.0'I guess.
Both would be nice actually. Email is perfect
Nothing wrong in those files. It looks like an issue in the Android Gradle plugin. Maybe you could try updating that and see if it helps.
I've updated the Gradle plugin from 1.5.0 to 2.1.0 a while ago and for now it seems to have solved the issue... but let's wait a day or two to really be sure. It looks primising though :)
I have been compiling all afternoon long now and it never happened once. I can confirm that the issue is solved by upgrading the Grade plugin. Thanks so much for all your help -- and thanks for Realm, I love it!
Perfect! Glad I could help! :)
I've got same issue with kotlin v 1.0.2 (kotlin.incremental = true) and instant run.
@ragaisis What is the android gradle plugin version you are using? and does it work if you disable the instant run?
Most helpful comment
I've updated the Gradle plugin from 1.5.0 to 2.1.0 a while ago and for now it seems to have solved the issue... but let's wait a day or two to really be sure. It looks primising though :)