繁体   English   中英

当与Kotlin一起使用Instant Run时发生IllegalAccessError

[英]IllegalAccessError when using Instant Run with Kotlin

使用Instant Run运行项目后,出现此异常:

java.lang.IllegalAccessError:非法的类访问:“ com.alla.kotlinexample.MainActivity $ override”正尝试访问“ kotlin.jvm.internal.DefaultConstructorMarker”(“ / com.alla.kotlinexample.MainActivity $ override”的声明显示在/数据/数据/com.alla.kotlinexample/文件/即时运行/dex-temp/reload0x0000.dex)
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)


}

并在此行上的错误点是val persons: List<Person> = listOf<Person>(Person("Person1"), Person("Person2", 27))

Android Studio版本为3.0.1 Gradle: classpath 'com.android.tools.build:gradle:3.0.1' 3.0.1'Gradle版本为4.1

build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.alla.kotlinexample"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

根gradle:

// 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
}

在构造函数上使用@JvmOverloads时,我遇到了类似的问题。 但是看起来当我改为自己定义即时构造器的构造函数时:

代替:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM