Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard: How to keep everything except specific condition?

Tags:

proguard

I'm using Proguard to obfuscate my code, and I need to keep every third party libraries like:

-keep class com.layer.**
-dontwarn com.layer.**
-keep class com.twitter.**
-keep class android.support.**
-keep class com.crashlytics.** { *; }
-dontwarn com.crashlytics.**
...

And whenever I add a new third party library, I need to check its package name and add it to my proguard config file, or the app may crashes.

Can't I write the rule just like this? I don't care about codes that's not mine.

-keep class !(my.package.name.**)
-dontwarn !(my.package.name.**)

Thanks!

like image 650
Romulus Urakagi Ts'ai Avatar asked Sep 10 '16 02:09

Romulus Urakagi Ts'ai


People also ask

Should I use ProGuard?

No shrinking, no obfuscation; not for classes, not for members. In real use cases, you can let ProGuard do at least some of it’s work. Even if your variables are accessed by reflection, you could remove and rename unused classes, for example. So let’s look through the more specific -keep variants.

What is the most permissive keep in ProGuard?

This is the most permissive keep directive; it lets ProGuard do almost all of its work. Unused classes are removed, the remaining classes are renamed, unused members of those classes are removed, but then the remaining members keep their original names. This one doesn’t get a table, because it’s the same as -keep.

How to prevent ProGuard from removing code from a specific package?

You can prevent ProGuard from removing anything in a certain package as follows; -keep,allowoptimization,allowobfuscation class com.example.mypackage.** { *; } Using the modifiers allowoptimization and allowobfuscation will make sure that ProGuard still obfuscates and optimizes the code.

What does ProGuard do with unused classes?

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.


1 Answers

To keep everything except classes in your own package you can use the rule that you already pointed out (excluding the brackets):

-keep class !my.package.name.** { *; }

This will implicitly keep everything else. You still can add additionally -keep rules for your classes if needed.

The rule for the -dontwarn should work in a similar way:

-dontwarn !my.package.name.**,**

You can also add similar -dontnote rules if needed.

like image 190
T. Neidhart Avatar answered Oct 22 '22 07:10

T. Neidhart