简体   繁体   English

IllegalAccessError:类无法访问方法

[英]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,但错误似乎表明BaseActivityclasses3.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.当然,当我将MyActivityanimationKind属性更改为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.因此,请确保您的BaseActivityMyActivity在同一个包下。

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模块中声明, FirstActivityapp模块中声明:

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.

相关问题 类中定义的方法不可访问 - method defined in class inaccessible “___中的方法___()在无法访问的类或接口”编译错误中定义 - “method ___() in ___ is defined in inaccessible class or interface” compilation error 我在尝试使用 main() 方法运行 class 时收到 IllegalAccessError - I am receiving an IllegalAccessError when trying to run the class with the main() method in it IllegalAccessError:访问受保护的方法 - IllegalAccessError: accessing a protected method IllegalAccessError 到方法 copyOfRange Kotlin - IllegalAccessError to method copyOfRange Kotlin xyz中的method()在不可访问的类或接口中定义 - method() in x.y.z is defined in an inaccessible class or interface Java - 调用方法会导致IllegalAccessError - Java - Invoking a method causes IllegalAccessError 通过另一个包的公共子类使用包私有类的公共方法引用时出现IllegalAccessError - IllegalAccessError when using a public method reference of a package-private class through a public subclass from another package Kotlin Java抽象类IllegalAccessError - Kotlin java abstract class IllegalAccessError 两个java文件。 使用主要方法运行类时尝试访问其他文件中的方法时出现 IllegalAccessError - Two java files. Getting IllegalAccessError when running class with main method trying to access a method from the other file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM