Realm-java: RealmList of primitives in kotlin

Created on 8 Nov 2017  ·  9Comments  ·  Source: realm/realm-java

I am trying to implement RealmList within a model in kotlin but application crashes during compilation. Output:
"Error:Element type of RealmList must be a class implementing 'RealmModel' or one of the 'java.lang.String', 'byte[]', 'java.lang.Boolean', 'java.lang.Long', 'java.lang.Integer', 'java.lang.Short', 'java.lang.Byte', 'java.lang.Double', 'java.lang.Float', 'java.util.Date'. "

(I am aware that im not using java.lang.String)
Isn't there support for list of primitives on kotlin yet?

Code Sample

My model:
-->EDITED: i had an error copying the model
open class Champion(
var id: Int = 0,
var name: String = "",
var title: String = "",
var image: String = "",
var info: ChampionInfo? = null,
var passive: ChampionPassive? = null,
var spells: RealmList<ChampionSpell> = RealmList(),
var skins: RealmList<ChampionSkin> = RealmList(),
var rols: RealmList<String> = RealmList(),
var allyTips: RealmList<String> = RealmList(),
var enemyTips: RealmList<String> = RealmList()
) : RealmObject()

Same model in java works fine

Version of Realm and tooling

Realm version(s): 4.1.1
Realm sync feature enabled: no

Android Studio version: 3.0

Which Android version and device: pixel emulator API 23

T-Help

Most helpful comment

Oh wait. You're not even applying kotlin-kapt. That explains everything.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

All 9 comments

You're missing the generic type on the RealmList

Sorry @Zhuinden that was my bad, i copied my code to github directly and the generics were interpreted as HTML tags. I have updated the issue

@espinomlg What version of Kotlin are you using, also especially, in what order are you applying your gradle plugins?

kotlin_version = '1.1.51'

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'

Are you sure this is the class that throws? I think this worked for me.

I double check commenting each of those lines with RealmList of strings and application only compiles when all of them are commented. This is the output of gradle console:

Executing tasks: [:app:assembleDebug]

Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
app: 'androidProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.github.bumptech.glide:compiler:4.2.0', 'android.arch.lifecycle:compiler:1.0.0' and apply the kapt plugin: "apply plugin: 'kotlin-kapt'".
:app:buildInfoDebugLoader
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:compileDebugKotlin
Using Kotlin incremental compilation
:app:prepareLintJar UP-TO-DATE
:app:generateDebugSources
:app:javaPreCompileDebug
:app:compileDebugJavaWithJavac
app: Original kapt is deprecated. Please add "apply plugin: 'kotlin-kapt'" to your build.gradle.
Processor path was modified by kapt. Previous value = configuration ':app:debugAnnotationProcessorClasspath'
Destination for generated sources was modified by kapt. Previous value = /home/espino/AndroidStudioProjects/SmartLoL/app/build/generated/source/apt/debug

Note: Processing class ChampionPassive
Note: Processing class ChampionSkin
Note: Processing class SummonerTopChampions
Note: Processing class ChampionInfo
Note: Processing class SummonerLeague
Note: Processing class Summoner
Note: Processing class ChampionList
Note: Processing class ChampionJava
Note: Processing class Champion
error: Element type of RealmList must be a class implementing 'RealmModel' or one of the 'java.lang.String', 'byte[]', 'java.lang.Boolean', 'java.lang.Long', 'java.lang.Integer', 'java.lang.Short', 'java.lang.Byte', 'java.lang.Double', 'java.lang.Float', 'java.util.Date'.
1 error

:app:compileDebugJavaWithJavac FAILED
:app:buildInfoGeneratorDebug

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.

    Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1s

17 actionable tasks: 5 executed, 12 up-to-date

Are your other dependencies marked with kapt instead of annotationProcessor?

Oh wait. You're not even applying kotlin-kapt. That explains everything.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

Okk reading this line:
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
app: 'androidProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.github.bumptech.glide:compiler:4.2.0', 'android.arch.lifecycle:compiler:1.0.0' and apply the kapt plugin: "apply plugin: 'kotlin-kapt'".

I add kotlin-kapt plugin and works now

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlbertVilaCalvo picture AlbertVilaCalvo  ·  3Comments

wezley98 picture wezley98  ·  3Comments

tloshi picture tloshi  ·  3Comments

yuwu picture yuwu  ·  3Comments

CNyezi picture CNyezi  ·  3Comments