“kotlin-allopen”插件工作,但“kotlin-noarg”插件不起作用。 我该怎么办?
以下是代码。
的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
}
应用程序/的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
构建时,显示以下错误。Class "SampleEntity" must declare a public constructor with no arguments if it contains custom constructors.
它是否适用于领域?
答案 0 :(得分:5)
这是预期的行为。来自文档:https://kotlinlang.org/docs/reference/compiler-plugins.html#no-arg-compiler-plugin
生成的构造函数是合成的,因此无法直接调用 来自Java或Kotlin,但可以使用反射来调用