Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalAccessError: Method is inaccessible to class

I've got very strange error, because it only happens after installing app from generated .apk. When I try to run the app through IDE, it works fine.

java.lang.IllegalAccessError: Method 'int <package>.BaseActivity$Companion.getANIMATION_SLIDE_FROM_RIGHT()' is inaccessible to class '<package>.MyActivity' (declaration of '<package>.MyActivity' appears in /data/app/<package>-mg7eYmJ8hX5WvkNWNZWMVg==/base.apk!classes3.dex)

As you can see there is class called BaseActivity which looks like this:

open class BaseActivity : AppCompatActivity() {

    companion object {
        @JvmStatic
        protected val ANIMATION_DEFAULT = 0
        @JvmStatic
        protected val ANIMATION_SLIDE_FROM_RIGHT = 1
        @JvmStatic
        protected val ANIMATION_SLIDE_FROM_BOTTOM = 2
    }

    protected open var animationKind = ANIMATION_DEFAULT

    // Some other stuff
}

Now every activity extends this class and often overrides the animationKind like this:

class MyActivity: BaseActivity() {

    override var animationKind = ANIMATION_SLIDE_FROM_RIGHT

    // Some other stuff
}

The problem is that ANIMATION_SLIDE_FROM_RIGHT is inaccessible for MyActivity. I will repeat that it only happens on manually generated .apk. The funny thing is that I'm not using multidex, but error seems to show that BaseActivity is in classes3.dex. Here is my gradle file:

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt'

android {

    compileSdkVersion 28

    defaultConfig {
        applicationId <package>
        versionCode <versionCode>
        versionName <versionName>
        minSdkVersion 21
        targetSdkVersion 28
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    androidExtensions {
        experimental = true
    }
}

dependencies {

    // Dependencies
}

I tried to play with multidexEnabled false/true, but the only change is that in false state the classes3.dex suffix disappears.

UPDATE

Of course when I change MyActivity's animationKind property to 1, then everything works fine.

UPDATE 2

After removing @JvmStatic and protected visibility it works fine.

like image 339
Nominalista Avatar asked Jul 20 '18 05:07

Nominalista


3 Answers

While I'm not sure why this results in IllegalAccessError, you should be defining those constants like this:

companion object {
    const val ANIMATION_DEFAULT = 0
    const val ANIMATION_SLIDE_FROM_RIGHT = 1
    const val ANIMATION_SLIDE_FROM_BOTTOM = 2
}

And that should solve your problem, otherwise using @JvmField instead of @JvmStatic would have been a better choice.

like image 41
EpicPandaForce Avatar answered Sep 20 '22 14:09

EpicPandaForce


Make sure you've declared the method that is failing inside the same module as the calling code.

In my case I was experiencing following error:

java.lang.IllegalAccessError: Method 'boolean[] my.package.common.kotlin.AndroidExtensionsKt.$jacocoInit()' is inaccessible to class 'my.package.ui.first.FirstActivity$viewModel$2' (declaration of 'my.package.ui.first.FirstActivity$viewModel$2' appears in /data/app/my.package.dev-fdHNodmdXHv-b_heK4MXeA==/base.apk!classes8.dex)
    at my.package.ui.first.FirstActivity$viewModel$2.invoke(FirstActivity.kt:18)
    at my.package.ui.first.FirstActivity$viewModel$2.invoke(FirstActivity.kt:14)
    at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
    at my.package.ui.first.FirstActivity.getViewModel(Unknown Source:11)
    at my.package.ui.first.FirstActivity.onCreate(FirstActivity.kt:23)

Where getViewModel() was declared in common module and FirstActivity was declared in app module:

inline fun <reified T : ViewModel> FragmentActivity.getViewModel(
    factory: ViewModelProvider.Factory = ViewModelProvider.NewInstanceFactory()
) = ViewModelProviders.of(this, factory).get(T::class.java)

After moving getViewModel() from common module to app module no issues were seen.

like image 27
azizbekian Avatar answered Sep 22 '22 14:09

azizbekian


From the official Kotlin documentation:

Java allows accessing protected members from other classes in the same package and Kotlin doesn't, so Java classes will have broader access to the code

So, please make sure that your BaseActivity and MyActivity are under the same package.

If both activities are not under the same package then it will run perfectly by direct run from Studio, but it will crash (IllegalAccessError) while you generate .apk and try to run on the device by installing that apk.

like image 64
Vinay Rathod Avatar answered Sep 18 '22 14:09

Vinay Rathod