Android APK crashes on launch with 'Illegal class access' error

Viewed 2350

So I recently updated to Android Studio 4.1 and updated my project to Android 10. Ever since, debug app builds work fine but whenever I launch a built APK on an Android device I get the below error:

FATAL EXCEPTION: main
Process: com.cookiegames.smartcookie, PID: 12800
java.lang.IllegalAccessError: Illegal class access: 'androidx.appcompat.widget.ContentFrameLayout' attempting to access 'androidx.appcompat.app.w' (declaration of 'androidx.appcompat.widget.ContentFrameLayout' appears in base.apk)
    at androidx.appcompat.widget.ContentFrameLayout.onAttachedToWindow(Unknown Source:7)
    at android.view.View.dispatchAttachedToWindow(View.java:18347)
... and so on

I've rolled back to Android Studio 4.0 and moved my project back to SDK 29, but the error persists. I have no idea what to try next.

3 Answers

Update:

@headsvk answer is now correct. Android studio didn't automatically prompted me to update the gradle so I had to do it manually. You have to use gradle 6.5.1 (change this in gradle-wrapper.properties file) and then change the com.android.tools.build:gradle version to 4.1.1 (in build.gradle file in project root). Clean and rebuild the project and it should be fixed.

Old answer:

I found a temporary way to fix this issue for myself. I realized that disabling proguard fixes the problem. So I just added a rule to tell proguard to keep androidx.appcompat class.

To do so, just add this line of code to proguard-rules.pro file:

-keep class androidx.appcompat.** { *; }

I got the same error, simply updating to the latest Androidx libraries solved the issue.

I am not sure if this would work for you Disclaimer

However in my app/build

buildTypes {
    ndk {
        abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'mips', 'mips64', 'arm64-v8a'
    }
    release {
        signingConfig signingConfigs.release
    }
}

The ndk insertion helped for my situation and stopped my constant uploading to google play store and praying it would work out. So hopefully this can help someone who may run into this too!

Related