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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With