[英]IllegalAccessError: Method is inaccessible to class
I've got very strange error, because it only happens after installing app from generated .apk
.我有一个非常奇怪的错误,因为它只发生在从生成的.apk
安装应用程序之后。 When I try to run the app through IDE, it works fine.当我尝试通过 IDE 运行该应用程序时,它运行良好。
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:如您所见,有一个名为BaseActivity
类,如下所示:
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:现在每个活动都扩展了这个类,并且经常像这样覆盖animationKind
:
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
.问题是MyActivity
无法访问ANIMATION_SLIDE_FROM_RIGHT
。 I will repeat that it only happens on manually generated .apk
.我会重复一遍,它只发生在手动生成的.apk
。 The funny thing is that I'm not using multidex, but error seems to show that BaseActivity
is in classes3.dex
.有趣的是,我没有使用 multidex,但错误似乎表明BaseActivity
在classes3.dex
。 Here is my gradle file:这是我的gradle文件:
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt'应用插件:'com.android.application' 应用插件:'kotlin-android' 应用插件:'kotlin-android-extensions' 应用插件:'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.我尝试使用multidexEnabled false/true
,但唯一的变化是在false
状态下classes3.dex
后缀消失。
UPDATE更新
Of course when I change MyActivity
's animationKind
property to 1
, then everything works fine.当然,当我将MyActivity
的animationKind
属性更改为1
,一切正常。
UPDATE 2更新 2
After removing @JvmStatic
and protected visibility it works fine.删除@JvmStatic
和受保护的可见性后,它工作正常。
From the official Kotlin documentation :来自Kotlin 官方文档:
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 Java 允许从同一包中的其他类访问受保护的成员,而 Kotlin 不允许,因此 Java 类可以更广泛地访问代码
So, please make sure that your BaseActivity
and MyActivity
are under the same package.因此,请确保您的BaseActivity
和MyActivity
在同一个包下。
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.如果这两个活动不在同一个包下,那么它可以通过从 Studio 直接运行完美运行,但是当您生成 .apk 并尝试通过安装该 apk 在设备上运行时它会崩溃(IllegalAccessError)。
While I'm not sure why this results in IllegalAccessError, you should be defining those constants like this:虽然我不确定为什么会导致 IllegalAccessError,但您应该像这样定义这些常量:
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.这应该可以解决您的问题,否则使用@JvmField
而不是@JvmStatic
会是更好的选择。
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:其中getViewModel()
在common
模块中声明, FirstActivity
在app
模块中声明:
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.将getViewModel()
从common
模块移动到app
模块后,没有发现任何问题。
open class BaseActivity : AppCompatActivity() {
@JvmField
protected val ANIMATION_DEFAULT = 0
@JvmField
protected val ANIMATION_SLIDE_FROM_RIGHT = 1
@JvmField
protected val ANIMATION_SLIDE_FROM_BOTTOM = 2
protected open var animationKind = ANIMATION_DEFAULT
// Some other stuff
}
How About this?这个怎么样?
In my opinion, Kotlin does not yet fully support companion - jvm static member.在我看来,Kotlin 还没有完全支持伴侣——jvm 静态成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.