Android proguard, keep inner class

JavaAndroidProguard

Java Problem Overview


My android program has a class A, which has two static inner class. They are found to be stripped from .dex after applying proguard.

public class A{

  ...
  static class B{
    ...
  }

  static class C{
    ...
  }
}

I have put the following lines in proguard.flags, but seem no luck.

-keep class com.xxx.A
-keep class com.xxx.A$*

Any hint?

Java Solutions


Solution 1 - Java

Try adding InnerClasses to the keep attributes. e.g:

-keepattributes Exceptions, InnerClasses, ...

Also, try adding a body to the "keep" call with an asterisk, like so:

-keep class com.xxx.A$* {
    *;
}

Solution 2 - Java

This is what I had to do for my config

-keep class com.xxx.A { *; }
-keep class com.xxx.A$B { *; }
-keep class com.xxx.A$C { *; }

Solution 3 - Java

This did the trick for me

-keepattributes InnerClasses
 -keep class com.yourpackage.YourClass**
 -keepclassmembers class com.yourpackage.YourClass** {
    *;
 }

It may be a bit overkill with the wildcards but I wanted to make sure I didn't miss anything. The main thing is you need the InnerClasses attributes the keep on the class and the keepclassmembers on the class.

Solution 4 - Java

if you don't want all inner class and members in some package to be obfuscated you can add lines in proguard-rules.pro

    -keep class com.xxx.task.*$* {
        *;
    }

Solution 5 - Java

Your configuration looks correct. You should double-check that you haven't misspelled the class names. If the spelling in incorrect, ProGuard should print out a note about it. You can also specify -printseeds seeds.txt, and see if your classes are listed in the resulting file. If they are listed, the classes are also in the processed code.

As Alexander Lucas mentioned, you may also want to keep the fields and methods of these classes -- that depends on your requirements.

Solution 6 - Java

Most of the top answers does the job but they are using wildcard (*;) for adding everything which is not the accurate answer. Below configuration only adds the fields and method properties to preserve inner class methods.

-keepattributes Exceptions,InnerClasses,Signature, ...

# preserve class and nested classes' fields and methods
-keep class com.abc.package.clazzes.** {
    <fields>;
	<methods>;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid GuanView Question on Stackoverflow
Solution 1 - JavaAlexander LucasView Answer on Stackoverflow
Solution 2 - JavaajmaView Answer on Stackoverflow
Solution 3 - JavaGameSalutesView Answer on Stackoverflow
Solution 4 - JavaMr235View Answer on Stackoverflow
Solution 5 - JavaEric LafortuneView Answer on Stackoverflow
Solution 6 - JavaImtiaz Shakil SiddiqueView Answer on Stackoverflow