Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 697
CookieJarApps Avatar asked Oct 20 '20 18:10

CookieJarApps


2 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.** { *; }
like image 139
Hirbod Behnam Avatar answered Oct 22 '22 07:10

Hirbod Behnam


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

like image 26
headsvk Avatar answered Oct 22 '22 08:10

headsvk