Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProGuard - How to keep methods and obfuscate them at the same time?

I am trying to manually obfuscate my Android application (yes, I know, it's a pain) and for that I need to keep all the methods I implement yet also obfuscate them with ProGuard at the same time.

I have tried changing the config like this:

-keep class com.project.x.* {

}

But it kept all the class names and method names and still removed my unused code.

What can I do to solve this issue?

Thanks in advance!

like image 470
MrByte Avatar asked Nov 03 '12 08:11

MrByte


People also ask

Does ProGuard remove unused classes?

Shrinking Options Specifies not to shrink the input. By default, ProGuard shrinks the code: it removes all unused classes and class members. It only keeps the ones listed by the various -keep options, and the ones on which they depend, directly or indirectly.

What is the difference between ProGuard and R8?

R8 is having a faster processing time than Proguard which reduces build time. R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4.


2 Answers

As CommonsWare explained, it seems unlikely that you want to keep unused methods and yet have their names obfuscated, but ProGuard supports it as follows:

-keepclassmembers,allowobfuscation class com.project.x.** {
    <methods>;
}

This forcibly keeps all methods (but not all classes) in the specified package (and its subpackages), but still allows their names to be obfuscated.

like image 173
Eric Lafortune Avatar answered Nov 10 '22 08:11

Eric Lafortune


Use keep option modifiers - see here

I think you'd do something like this:

-keep,allowoptimization class com.project.x.* {

}

But to quote the documentation: "This modifier is only useful for achieving unusual requirements."

Have you tried to reverse enginner to see that this is something you need to do? My impression of keep is that it will preserve the class as an entry point to the app, but that it doesn't stop it optimising and obfuscating the methods of those classes internally.

Maybe all you want is -keepnames see that link above for details.

like image 24
weston Avatar answered Nov 10 '22 09:11

weston