Android 与Kotlin一起使用Instant Run时出现IllegaAccess错误
我在使用Instant run运行项目后遇到此异常: java.lang.IllegalAccessError:非法类访问: “com.alla.kotlinexample.MainActivity$override”正在尝试访问 'kotlin.jvm.internal.DefaultConstructorMarker'(声明 “com.alla.kotlinexample.MainActivity$override”出现在中 /data/data/com.alla.kotlinexample/files/instant run/dex temp/reload0x0000.dex)Android 与Kotlin一起使用Instant Run时出现IllegaAccess错误,android,kotlin,android-instant-run,Android,Kotlin,Android Instant Run,我在使用Instant run运行项目后遇到此异常: java.lang.IllegalAccessError:非法类访问: “com.alla.kotlinexample.MainActivity$override”正在尝试访问 'kotlin.jvm.internal.DefaultConstructorMarker'(声明 “com.alla.kotlinexample.MainActivity$override”出现在中 /data/data/com.alla.kotlinexample
位于com.alla.kotlinexample.MainActivity$override.onCreate(MainActivity.kt:21)
位于com.alla.kotlinexample.MainActivity$override.access$dispatch(MainActivity.kt) 代码如下:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val persons: List<Person> = listOf<Person>(Person("Person1"), Person("Person2", 27))
fab.setOnClickListener { view ->
Snackbar.make(view, persons[1].name, Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
tv_person_name.text = persons.maxBy { it.age ?: 34 }.toString()
}
data class Person(val name: String, val age: Int? = null)
}
根梯度:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.10'
//ext.kotlin_version = '1.1.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
当我在构造函数上使用@JVM重载时,我也遇到了类似的问题。然而,当我自己定义构造函数时,它看起来像是即时运行: 而不是:
class StaticRefreshAnimationView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LottieRefreshAnimationView(context, attrs, defStyleAttr) {
我现在使用:
class StaticRefreshAnimationView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
LottieRefreshAnimationView(context, attrs, defStyleAttr) {
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null)
您是否尝试从
Bundle
参数中删除?
?@4gus71n这是不允许的,它说'onCreate'不覆盖任何内容
好的,我正在查看我自己的一个项目,我有完全相同的代码。相同的插件版本。你能发布你的毕业信息吗?这样我就可以检查我是否有不同的东西了?同时发布您的AS版本。@4gus71n我发布了build.gradle文件。AS版本也是我的问题。我看到的唯一区别是,我没有使用实现“org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version”
,我也没有使用多索引。检查更改这些东西是否有帮助,但如果没有,请发布您的根gradle,我可以检查其中是否有差异。我的情况是,相同的错误是由一个同伴对象引发的,该对象需要一个我无法实现的构造函数。解决方案是Gavriel建议的:删除伴随对象并使用标准构造函数。如果您需要,我可以添加一个带有代码的答案
class StaticRefreshAnimationView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
LottieRefreshAnimationView(context, attrs, defStyleAttr) {
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null)