Kotlin uses Java 8 api in Android without warning or error


#1

I used Collection.removeIf in Kotlin code, it didn’t produced a compile error, but produced a runtime error

java.lang.NoClassDefFoundError

#2

You want to be careful which version of the standard library you use. Use the default (jre6) or jre7 versions as appropriate. The kotlin version of removeIf is provided in the jre8 kotlin standard library. As it is the library using Collection.removeIf and not your own code lint doesn’t warn. It is however something that Lint could be taught about.


#3

@sw926 Could you provide the code? Which version of Kotlin plugin and Android Studio are you using? Since version 1.1.3 there should be lint error.


#4

My Android Studio version is 2.3.3, Kotlin version is 1.1.3. I found it because I used RealmList.

Post().queryFirst { it.equalTo("uuid", post.uuid) }?.let {
    it.getComments()?.removeIf {
        it.id == commentId
    }
    it.save()
    Comment().delete { it.equalTo("id", commentId) }
}

#5

Tried your sample and got lint error. Make sure you have latest Kotlin plugin installed and the corresponding inspection enabled.
Preferences -> Editor -> Inspections -> Android Lint for Kotlin -> Calling new methods on older versions


#6

I used Realm in my project

this is a code segment of RealmList

public class RealmList<E extends RealmModel> extends AbstractList<E> implements OrderedRealmCollection<E> {
    ...
}

when i use RealmList.removeIf, it won’t get a lint error


#7


#8

Got it, thank you! Created issue https://youtrack.jetbrains.com/issue/KT-18837


#9

I have latest Kotlin plugin installed (v1.2.31), but I can’t find this lint warning in AS Preferences.
Could you please suggest another solution?


#10

Which AS version are you using? Just checked AS 3.1 and it’s still there.


#11

I have a similar problem - with Map.getOrDefault(k, v) and don’t have that Inspection option either. Anyone have an alternative suggestion?