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

1

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.

Isidora answered 24/9, 2012 at 12:6 Comment(3)
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?Isidora
Why is it a problem to keep the original name only for this class? It is highly used in the code?Autarchy
Not frequently used, but important.Isidora
R
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

Rectal answered 26/9, 2012 at 21:22 Comment(1)
Yes, that was exactly what I wanted. Cheers (y)Isidora

© 2022 - 2024 — McMap. All rights reserved.