Android Proguard - Classnotfoundexception
Solution 1:
You'll want to also make sure to add in the New Relic proguard exceptions found here: https://docs.newrelic.com/docs/mobile-monitoring/mobile-monitoring-installation/android/installing-android-apps-gradle-android-studio#proguard
-keep class com.newrelic.** { *; }-dontwarn com.newrelic.**-keepattributes Exceptions, Signature, InnerClasses
Solution 2:
Remove the obfuscation rules related to activity and all also related to support libaries.Android studio by default has those templates.You could enable them by adding this rule in your gradle
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
Also please edit the rule for Jackson processor
-keep publicclassyour.class.** {
publicvoid set*(***);
public *** get*();
}
In this your class.** is the POJO(getter/setter) class that you have created for parsing your response
Solution 3:
I know this is a rather old post but I would like to highlight the following should it help someone in the future.
The ClassNotFoundException
is an exception caused by applying name obfuscation to a class that's accessed using reflection. For example if ProGuard renames the class MyClass
, which is accessed in the code like this: Class.forName("MyClass");
, a ClassNotFoundException
will be thrown if the string still refers to MyClass
.
This is where we would need to add a -keep
option in order for ProGuard to not rename that class;
-keep class com.example.MyClass
When using broad -keep
options (ending with .** { *; }
), you will instruct ProGuard not to shrink, optimize or obfuscate all classes and classmembers for the package name mentioned before the wildcards. This will eventually lead in a poorly optimized project. Therefore it's better to narrow down such -keep
option to only target the missing class. In OP's example, the following error is thrown:
Caused by: java.lang.ClassNotFoundException: Didn't find class "mypackage.activities.MainActivity"
Adding a -keep
option for the missing class like shown below will tackle this particular issue;
-keep class mypackage.activities.MainActivity
ProGuard can help you set up narrowed down -keep
options if you add -addconfigurationdebugging
to the configuration file, more details on this feature is documented in the ProGuard manual, here.
Recently the ProGuard Playground was released, you can quickly visualise the effect of the broad -keep
options vs the narrowed down ones. A nice benefit here is that you do not need to continuously (re-)build the project.
Post a Comment for "Android Proguard - Classnotfoundexception"