Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard keep classmembers

How can I tell proguard to keep my classmembers?

I need them for JSON compatibility...

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 already tried, but it doesnt work...

-keepclassmembers class com.tools.app.holiday.Holiday 

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

I also implemented serialisable and did:

-keepclassmembers class * implements java.io.Serializable {

But it all doesn't keep my member names. Proguard always changes ist to a, b, c :-( What am I missing?

like image 767
Tobias Avatar asked Dec 07 '11 16:12

Tobias


People also ask

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.

How do you keep all classes in ProGuard?

-keepclassmembernames. 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.

Does ProGuard remove unused classes?

ProGuard Facts: Helps to remove unused classes, members & fields. It removes unused codes from your libraries as well. (Which means, any method or field that is not referenced anywhere will be removed by ProGuard Shrinker). Helps to reduce the build size by obfuscating the classes, methods, & fields with short names.

Should I use R8 or ProGuard?

R8 is having a faster processing time than Proguard which reduces build time. R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4.


1 Answers

All class names must be fully qualified:

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

By default, ProGuard even prints out messages if you forget this.

like image 145
Eric Lafortune Avatar answered Sep 18 '22 13:09

Eric Lafortune