“kotlin-noarg” plugin don't work in Realm

旧城冷巷雨未停 提交于 2019-12-05 07:13:43

问题


"kotlin-allopen" plugin work but "kotlin-noarg" plugin don't work. How can I do?

Below is the code.

build.gradle

buildscript {
    ext.kotlin_version = '1.1.3-2'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:3.5.0"
    }
}
apply plugin: "kotlin-allopen"
apply plugin: "kotlin-noarg"

allOpen {
    annotation("sample.AllOpen")
}
noArg {
    annotation("sample.NoArg")
    invokeInitializers = true
}

app/build.gradle

apply plugin: 'realm-android'

NoArg.kt

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class NoArg

MyApplication.kt

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Realm.init(this)
    }
}

AndroidManifest.xml

<application
    android:name=".MyApplication"

SampleEntity.kt

@NoArg
@AllOpen
@RealmClass
data class SampleEntity(var sample: String?) : RealmModel

when build, the following error was displayed.Class "SampleEntity" must declare a public constructor with no arguments if it contains custom constructors.

Does it work with realm?


回答1:


This is intended behaviour. From the documentation: https://kotlinlang.org/docs/reference/compiler-plugins.html#no-arg-compiler-plugin

The generated constructor is synthetic so it can’t be directly called from Java or Kotlin, but it can be called using reflection



来源:https://stackoverflow.com/questions/45768906/kotlin-noarg-plugin-dont-work-in-realm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!