Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell ProGuard to keep private fields without specifying each field

This is my class:

package com.tools.app.holiday;  public class Holiday {        private String name;      private Calendar dateFrom = Calendar.getInstance();      private Calendar dateTo = Calendar.getInstance();      ... 

I can keep these private fields by putting the following in my ProGuard rules file:

-keepclassmembers class com.tools.app.holiday.Holiday {     private java.lang.String name;         private java.util.Calendar dateFrom;     private java.util.Calendar dateTo; } 

But I'd prefer not to have to specify each field individually. How can I do this?

P.S. I stole most of this from Proguard keep classmembers because that question was close to what I'm asking.

like image 645
cja Avatar asked Apr 29 '14 12:04

cja


People also ask

What is ProGuard obfuscation?

ProGuard is a command-line tool that reduces app size by shrinking bytecode and obfuscates the names of classes, fields and methods. It's an ideal fit for developers working with Java or Kotlin who are primarily interested in an Android optimizer.

What is keep in ProGuard?

To fix errors and force R8 to keep certain code, add a -keep line in the ProGuard rules file. For example: -keep public class MyClass. Alternatively, you can add the @Keep annotation to the code you want to keep. Adding @Keep on a class keeps the entire class as-is.

Where do you put ProGuard rules?

Android Studio adds the proguard-rules.pro file at the root of the module, so you can also easily add custom ProGuard rules specific to the current module. You can also add ProGuard files to the getDefaultProguardFile directive for all release builds or as part of the productFlavor settings in the build.


1 Answers

According to ProGuard documenation the wildcard <fields> matches any field. Thus it should be something like:

-keepclassmembers class com.tools.app.holiday.Holiday {     private <fields>;     } 

If you want to preserve private fields in all classes use:

-keepclassmembers class * {     private <fields>;     } 
like image 54
Idolon Avatar answered Sep 30 '22 17:09

Idolon