IllegalAccessError when using Instant Run with Kotlin
Asked Answered
P

1

10

I got this exception after I run the project using Instant Run:

java.lang.IllegalAccessError: Illegal class access: 'com.alla.kotlinexample.MainActivity$override' attempting to access 'kotlin.jvm.internal.DefaultConstructorMarker' (declaration of 'com.alla.kotlinexample.MainActivity$override' appears in /data/data/com.alla.kotlinexample/files/instant-run/dex-temp/reload0x0000.dex)
at com.alla.kotlinexample.MainActivity$override.onCreate(MainActivity.kt:21)
at com.alla.kotlinexample.MainActivity$override.access$dispatch(MainActivity.kt)

Here is the code:

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)


}

And the error points on this line val persons: List<Person> = listOf<Person>(Person("Person1"), Person("Person2", 27))

Android Studio version is 3.0.1 Gradle: classpath 'com.android.tools.build:gradle:3.0.1' Gradle version is 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'
}

root 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
}
Perianth answered 8/1, 2018 at 14:56 Comment(8)
Did you try to remove the ? from the Bundle argument?Pennate
@Pennate it's not allowed, it says then 'onCreate' overrides nothingPerianth
Okay, I'm looking through a project of my own and I have exactly the same code. Same plugin version. Could you post your gradle information so I can check if I have something different? Post your AS version as well.Pennate
@Pennate I posted my build.gradle file. AS version is also in my question.Perianth
Only difference that I'm seeing is that instead of having implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" and I'm not using multidex. Check if changing those things helps, but if not post your root gradle and I can check if there's some difference there.Pennate
@Pennate it didn't help. I also increased kotlin version from 1.1.51 to 1.2.10 and it also didn't help. I also posted my root gradle file. And without kotlin-stdlib-jre7 I cannot configure KorlinPerianth
Only tip that I can give you is to try to delete that task clean(type: Delete) { block from your code. Other than that is exactly the same thing that I have.Pennate
I have similar problem with my custom layout. It looks like instant run thinks it can call a default constructor to recreate it. No idea how it's supposed to work without the contextSwamper
S
5

I had a similar problem when I used @JvmOverloads on my constructors. However it looks like when instead I define the constructors myself Instant Run works:

Instead of:

class StaticRefreshAnimationView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LottieRefreshAnimationView(context, attrs, defStyleAttr) {

I use now:

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)
Swamper answered 25/1, 2018 at 10:44 Comment(1)
I'm my case, the same error was thrown by a companion object which needed a constructor that I was not able to implement. The solution is the one suggested by Gavriel: delete the companion object and use a standard constructor. If you need I can add an answer with the codeSuperman

© 2022 - 2024 — McMap. All rights reserved.