7

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.

PatlaDJ
  • 1,226
  • 2
  • 17
  • 31
  • After I wrote the question, it occurred to me a very simple solution. 'fake use' of the class somewhere in my application, but directly, not thru reflection. However, I'd like to know if there's a way to preserve it with ProGuard? – PatlaDJ Sep 24 '12 at 12:22
  • Why is it a problem to keep the original name only for this class? It is highly used in the code? – Daniel Pereira Sep 24 '12 at 13:10
  • Not frequently used, but important. – PatlaDJ Sep 27 '12 at 10:37

1 Answers1

15

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

Eric Lafortune
  • 45,150
  • 8
  • 114
  • 106