Can we shrink all classes but only obfuscate some with proguard?
Asked Answered
V

2

4

Typically, excluding a class with -keep prevents the class from being obfuscated

However it also prevents it from being shrunk.

Is it possible to define a proguard-project.txt that will shrink all classes except those that are excluded with -keep, but also obfuscate only a specific subset of the classes?

The aim is to use proguard to keep below the android 65k method limit, while also obfuscating first party code ONLY within the APK.

Thanks

Vitriform answered 17/7, 2014 at 17:4 Comment(0)
H
5

Yes, you can add the modifier allowshrinking to the -keep options that should only apply to the obfuscation (and optimization) steps. For instance:

-keep,allowshrinking class com.example.SomeClass

The specified class may be removed if it appears unused in the shrinking step, but otherwise, its name will be preserved in the obfuscation step.

Hydrometallurgy answered 20/7, 2014 at 23:26 Comment(0)
L
1

Eric's answer is good, there is also another way.

First, there is shorthand for:

-keep,allowshrinking

You can use:

-keepnames

You can also do it using the inverse:

-keepnames class !com.example.apackage.** {*;}

So this will obfuscate all classes inside apackage and nothing else, while still allowing dead code stripping on everything.

Another note is if you have obfuscation turned on, it will strip all of the meta data like file names and line numbers which will break debuggers and stack traces. If you want them to work you can add these lines:

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
Leprechaun answered 12/6, 2015 at 20:41 Comment(1)
I can't get the inverse running: Proguard (R8) negate operator not working to keep anything except certain packagesCroton

© 2022 - 2024 — McMap. All rights reserved.