IllegalAccessError: Method is inaccessible to class

前端 未结 3 1815
耶瑟儿~
耶瑟儿~ 2021-02-10 02:09

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.



        
相关标签:
3条回答
  • 2021-02-10 02:51

    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.

    0 讨论(0)
  • 2021-02-10 03:00

    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.

    0 讨论(0)
  • 2021-02-10 03:10

    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.

    0 讨论(0)
提交回复
热议问题