Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ProGuard. Class keep (do not delete) but yet allow to be obfuscated

I want to skip a particular class from deletion. It isn't normally referenced anywhere in my application, but only by reflection, therefore it does get removed by the shrinker. It is referenced by other "adjacent" classes in its package, but yet, not used for my application directly, but only by reflection.

I decided to treat specifically for this particular class, a mapping:

org.mypckg.Helper -> gh6

...then ofcourse I changed the reflection call withing my app:

forName("gh6")

There seems to be no problem with my mapping input, but the mapping rule itself is not enough to prevent the class from removal. In addition I still cannot keep the class using the -keep switches, because it does preserve it using its original name (org.mypckg.Helper), which I don't want.

For one reason or another, I cannot manually refractor(rename) the class to 'gh6' within the project.

like image 444
PatlaDJ Avatar asked Sep 24 '12 12:09

PatlaDJ


1 Answers

ProGuard recognizes the construct Class.forName("org.mypckg.Helper"); it then keeps and obfuscates org.mypckg.Helper without further configuration.

Otherwise:

-keep,allowobfuscation class org.mypckg.Helper
-adaptclassstrings org.mypckg.AdjacentClass*

Cfr. ProGuard manual > Usage > Overview of Keep Options

Cfr. ProGuard manual > Usage > -adaptclassstrings

like image 196
Eric Lafortune Avatar answered Oct 10 '22 13:10

Eric Lafortune