Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep all methods in a class with ProGuard

Tags:

I use ProGuard to optimize my Android Application. However for Android instrumentation test I need some (but not all) classes to keep all there members. I tried various approaches, the last being:

-keepclassmembers public class com.mycompany.myclass {
    *;
}

But surprisingly I still get

java.lang.NoSuchMethodError: com.mycompany.myclass.<init>

The painful part here is that there are two constructors and one has quite a few parameters.

Does anyone know the correct syntax to keep a class completely unchanged and untouched by ProGuard?

like image 884
Martin Avatar asked Apr 08 '11 08:04

Martin


People also ask

How do you keep all classes in ProGuard?

-keepclassmembernames. This is the most permissive keep directive; it lets ProGuard do almost all of its work. Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names.

What does ProGuard keep do?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.

Does ProGuard remove unused classes?

ProGuard is an open source command-line tool that detects and removes unused classes, fields, methods, and attributes. It helps in creating a production-ready application in the Android Platform.


1 Answers

Well, it is confession time. The question is bollocks. The -keepclassmembers is correct. The problem occurred because a team mate broke the code and the constructor was truly not there.

Note that if there is a change that the whole class is optimized away then you should use -keep as kharles suggested but the {*;} is needed to ensure that all methods stay in place.

Note that the {*;} is for testing only. For production one should use a more fine grained approach.

I keep the question for anybody with the same problem.

like image 160
Martin Avatar answered Oct 20 '22 20:10

Martin