Android ProGuard +MultiDex causes ClassNotFoundException

心已入冬 提交于 2019-12-03 13:57:40

This page says:

The default proguard.cfg file tries to cover general cases, but you might encounter exceptions such as ClassNotFoundException, which happens when ProGuard strips away an entire class that your application calls.

You can fix errors when ProGuard strips away your code by adding a -keep line in the proguard.cfg file. For example:

-keep public class <MyClass>

There are many options and considerations when using the -keep option, so it is highly recommended that you read the ProGuard Manual for more information about customizing your configuration file. The Overview of Keep options and Examples sections are particularly helpful. The Troubleshooting section of the ProGuard Manual outlines other common problems you might encounter when your code gets stripped away.

Solution 1

You could try the following proguard.cfg I used to obfuscate the code while not optimising, and hence not remove classes/methods from your APK.

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-dontoptimize
-verbose

Solution 2

You have asked some classes to be kept but not for MainActivity. So you have to add this line:

-keep class com.mypackage.activities.MainActivity { *; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!