Realm-java: kotlin.incremental=true cause problems with annotation processor

Created on 3 Jul 2016  路  60Comments  路  Source: realm/realm-java

Hello , I get this error when i build my project unreachable statament in DefaultRealmModuleMediator.java and it points to
throw getMissingProxyClassException(clazz);
if (iterator.hasNext()) {
throw getMissingProxyClassException(clazz);
}

Can you help me please ?

O-Community T-Bug

Most helpful comment

it seems kotlin 1.0.4 + kapt2 solved this issur for me

All 60 comments

Do you have at least one class that extends RealmObject?

Can you please share the whole file of DefaultRealmModuleMediator.java? It is a generated file which should be located in your build directory.

@beeender
DefaultRealmModuleMediator.txt

@Zhuinden no, I don't have any class that extends RealmObject

You should make at least one class that extends RealmObject.

Any class will do, also specify deleteIfMigrationNeeded() for your RealmConfiguration builder.

I recommend adding this, you might need it anyways

public class RealmString extends RealmObject {
     private String value;

     public String getValue() { return value; }
     public void setValue(String value) { this.value = value; }
}

But if you use RealmModel, then you have to add @RealmClass annotation too.

To @DinuIoan I think follow @Zhuinden 's comment will solve your problem.

Internal notes: in this case we should throw earlier in the annotation processor.

    @Override
    public void insert(Realm realm, Collection<? extends RealmModel> objects) {
        Iterator<? extends RealmModel> iterator = objects.iterator();
        RealmModel object = null;
        Map<RealmModel, Long> cache = new IdentityHashMap<RealmModel, Long>(objects.size());
        if (iterator.hasNext()) {
            //  access the first element to figure out the clazz for the routing below
            object = iterator.next();
            // This cast is correct because obj is either
            // generated by RealmProxy or the original type extending directly from RealmObject
            @SuppressWarnings("unchecked") Class<RealmModel> clazz = (Class<RealmModel>) ((object instanceof RealmObjectProxy) ? object.getClass().getSuperclass() : object.getClass());

            throw getMissingProxyClassException(clazz);
            if (iterator.hasNext()) {
                throw getMissingProxyClassException(clazz);
            }
        }
    }

I have this problem with kotlin since I updated android gradle plugin to 2.2.0-alpha5 version.
Even with realm 1.1.1-SNAPSHOT, still the same error.
I do have RealmClass/RealmObject (like 7 or 8) in my project.
The project is compiling 1 time out of 4.
I usually don't ask this (I know it's very annoying) but do you have any ETA on this (I can live with a working snapshot !) ? I will move back to Android Studio 2.1 for the moment.

@NitroG42 I am really sorry for the delay. I am looking into this issue right now.

Hey I'm sorry I just saw there was a 1.2.0-snapshot, I'm trying this right now (because it seems fixed on the latest source code version).

I think it is not fixed ... Just to confirm, you have some RealmObject or RealmModle defined, right?

Yes I have, but I as said, I only got this issue since updating to Android Studio 2.2 Preview 5 (was on Preview 4 before and it was working) with gradle plugin alpha 5. I'm not sure how it interact with your code generation (it shouldn't but hey... It's android !)

@NitroG42 do you by chance use your own @RealmModule to define your schema, or do you rely on the default schema (which contains all RealmObjects in the project)?

I'm using the default schema, no custom module

I just tested with 1.2.0-snapshot, same issue.

@NitroG42 It might be this issue https://github.com/realm/realm-java/issues/2822
Can you please try to add @RealmClass annotation to the classes to see if it works?

It is already done, I need this because i'm using RealmModel interface instead of RealmObject classes for my model.

@NitroG42 Is it a java/kotlin mixed project? I just tried a kotlin project with AS 2.2 preview 5, it works ...

There is one or two java class yes, I will try to remove them in a few moments, right now with 1.2.0-snapshot (I used the clean button, I should have done this before reporting), it seems to work (the generated DefaultRealmModuleMediator was entirely different after using Clean)

@NitroG42 and maybe this helps https://github.com/realm/realm-java/pull/2568

@beeender so essentially

As a temporary workaround until this request is adopted, I wrote apply plugin 'realm-android' before apply plugin 'kotlin-android'.

?

I guess so, but I haven't tested it before. @zaki50 Do you remember the problem of mixing java and kotlin apt?

Ok some updates :
I still doesn't work with 1.2.0-snapshot. What happens is :

  • Clean build, run => The DefaultRealmModuleMediator is generated with all my classes in it, no error.
  • Second run, the mediator it is modified and there is no class in it => error
  • It might be because I have one Java class in my project (but it's a behavior, a custom view for the CoordinatorLayout), or because of kotlin incremental build that is enabled on my project, or something else.

Ok I can confirm that transforming my Java Class to Kotlin made the error disappear. So it is definitelly because of the kotlin/java mix (even if java is one class, without annotation)

@NitroG42 Does below temp fix work for you? (from https://github.com/realm/realm-java/pull/2568)

As a temporary workaround until this request is adopted, I wrote apply plugin 'realm-android' before apply plugin 'kotlin-android'.

No I got lots of errors like :
Caused by: java.lang.IllegalArgumentException: Child is not part of the schema for this Realm

Caused by: java.lang.ClassCastException: mypackage.model..Child cannot be cast to io.realm.ChildRealmProxyInterface

@NitroG42 It seems it will take us some time to identify/fix the problem with preview AS (or maybe it is a android plugin issue :( ). Before it is fixed I think you need to switch back to AS 2.1 or using kotlin only. Really sorry for the convenience.

Hey don't worry I just needed to identify the problem, now that I know it works with kotlin only I removed my java class !
Thank you very much for your reactivity btw !

I think https://github.com/realm/realm-java/pull/2568 should fix this. And apply plugin: 'realm-android' should be put AFTER apply plugin: 'kotlin-android'

Hey, I needed to add a lots of Java class to my project, so I decided to try the 1.2.0-SNAPSHOT one more time, and good news everyone !
It seems to work now.

@NitroG42 Uh... I don't think we made any changes related to this ...

So still a java & kotlin mixed project?

@NitroG42 now that I think about it, a possibility is that incremental dex should have been disabled:

android {
    dexOptions {
        incremental false
    }

@beeender Yeah I just got the same error, but it worked multiple times before regenerating an empty Mediator, maybe it worked because of Instant run, I don't know.
Now, it is definitelly a java/kotlin mixed (Maybe i'll convert it to full kotlin later but right now there are too many java class)
@Zhuinden I'll try this !

I don't have this issue anymore since using :
compileOptions {
incremental false
}

@nitrog42 glad to hear that! :)

@NitroG42 Since we merged #2830 , I think it should work with incremental true. Would you please help to verify this with our snapshot? Just try with io.realm:realm-gradle-plugin:1.2.0-SNAPSHOT

I switched to 1.2.0 this morning and compiled many times, no error yet.

Thank you very much!!!!!!
I am closing this issue. Let us know if the problem happens again.

I still have it with io.realm:realm-gradle-plugin:1.2.0 in every second run.

@Adambl4 Try disable instant run?

Instant run disabled.
The issue passes away if I set kotlin.incremental=false

I just changed the title of this issue and reopen this.

For now, please just use kotlin.incremental=false as a workaround.
We will try to figure out the reason later. Thanks!

I'm having the same problem, not using instant run. Kotlin is 1.0.3, Realm 1.2.0.
For now I'm using kotlin.incremental=false in gradle.properties as a workaround.

I'm using latest versions of Kotlin and Realm and Instant Run works fine (Java only) in latest stable version of Android Studio with incremental kotlin builds turned off.

I have similar issue with kotlin, but the kotlin.incremental=false tricky dose not work for me

@kuno disable instant run

Also, while I said dexOptions, the other guy said

compileOptions {
    incremental false
}

@Zhuinden

I already disabled instant run.

Are you sure you're not getting some error message regarding some kind of other mistake somewhere?

Or is it still that the proper RealmMediator logic is not generated

I can confirm I still got this issue. Before, using incremental on kotlin and java worked like 9/10 times, but yesterday I used the new experimental annotation processor (kapt2, more info here : https://discuss.kotlinlang.org/t/kotlin-1-0-4-eap/1901/3) and on my Mac, every incremental compilation seems to fail.
Yesterday night though, I tried on my Windows and "seemed" to work.
I don't know what you can do with those info but if you need anything I'll try what you want.

Do you plan to support incremental compilation or will it never work for some reason ?

Here's a sample that reproduces exactly the issue on my Mac (even at first compile) :
MyApplication.zip

If it comes from Kotlin (even a tiny chance), tell me so I can create an issue on their tracker

@NitroG42 Incremental compilation in kapt is not supported in 1.0.4-eap-100, but will be supported very soon. Stay tuned!

Hey, just wanted to ley you know that using the new kapt2 made the issue go away completly.
The build never fails now, it awesome !

@NitroG42 Really!!?? haven't tried that before! So which plugin should be applied first? realm or kapt2?

You need to use kotlin 1.0.4-eap-118, and as follow :
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

and remove kapt generateStubs as seens here : https://discuss.kotlinlang.org/t/kotlin-1-0-4-eap/1901/3

Thanks a lot for the information! We will try to update Realm plugin with kapt2!

@yanex will have better info but I think the 1.0.4 version will probably be released soon (it seems stable, at least for me)

I have set compatibility settings to java 1.7, which solved the issue for me. Hope this will help somebody.

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

I use kotlin 1.4.0-eap-118 and just had to create a class extending RealmObject. Then the build succeeded. Problem with DefaultRealmMediator class generation code.

Please check the https://bintray.com/kotlin/kotlin-dev/kotlin/1.0.5-dev-3143 build as well, it contains some kapt2 fixes.

it seems kotlin 1.0.4 + kapt2 solved this issur for me

This issue appears to be fixed. So closing.

Was this page helpful?
0 / 5 - 0 ratings