IllegalAccessError:类无法访问方法 [英] IllegalAccessError: Method is inaccessible to class
问题描述
我有一个非常奇怪的错误,因为它仅在从生成的.apk
安装应用程序之后发生.当我尝试通过IDE运行该应用程序时,它运行良好.
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)
如您所见,有一个名为BaseActivity
的类,它看起来像这样:
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
}
现在,每个活动都扩展了该类,并经常像这样覆盖animationKind
:
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
}
问题是MyActivity
无法访问ANIMATION_SLIDE_FROM_RIGHT
.我将重复一遍,它仅在手动生成的.apk
上发生.有趣的是,我没有使用multidex,但是错误似乎表明BaseActivity
在classes3.dex
中.这是我的gradle文件:
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:
应用插件:"com.android.application" 套用外挂程式:'kotlin-android' 应用插件:"kotlin-android-extensions" 应用插件:"kotlin-kapt"
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
}
我尝试使用multidexEnabled false/true
,但是唯一的变化是在false
状态下,classes3.dex
后缀消失了.
I tried to play with multidexEnabled false/true
, but the only change is that in false
state the classes3.dex
suffix disappears.
更新
当然,当我将MyActivity
的animationKind
属性更改为1
时,一切正常.
Of course when I change MyActivity
's animationKind
property to 1
, then everything works fine.
更新2
在删除@JvmStatic
并保护可见性后,它可以正常工作.
After removing @JvmStatic
and protected visibility it works fine.
推荐答案
来自科特林官方文档:
Java允许从同一类中的其他类访问受保护的成员 包,而Kotlin则没有,因此Java类将具有更广泛的访问权限 到代码
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
因此,请确保您的BaseActivity
和MyActivity
位于同一软件包中.
So, please make sure that your BaseActivity
and MyActivity
are under the same package.
如果两个活动都不在同一个程序包中,则可以通过直接从Studio直接运行来完美运行,但是在生成.apk并尝试通过安装该apk在设备上运行时会崩溃(IllegalAccessError).
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.
这篇关于IllegalAccessError:类无法访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!