Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stop Proguard from removing type parameters?

I am currently attempting to obfuscate a series of libraries. My base library, which contains several classes and methods that use type parameters, is unusable by other code due to the type parameters being removed by Proguard obfuscation. Eliminating the obfuscation removes these issues. I have read through all of the ProGuard usage documents, examples, and troubleshooting, but have been unable to find any documentation on how to deal with type parameters or which aspect of ProGuard strips the type parameters.

Constructor Type Parameter Issue:

Library 1 contains the following class:

public abstract class AbstractFactoryFactory<T> 

Library 2 contains several classes that extend the above class but the constructor throws a compiler error that states:

error: type AbstractFactoryFactory does not take parameters 

Return Type Parameter Issue:

Library 1 has a Foo class with the following method:

public List<String> doSomething() 

Libary 2 tries to use the doSomething method, but after obfuscation the method returns an untyped list that generates the following compiler error that states:

error: incompatible types Object 

Proguard.cfg

-dontoptimize  -renamesourcefileattribute SourceFile -keepparameternames -keepattributes Exceptions,*Annotation*,InnerClasses,SourceFile,LineNumberTable,Deprecated  -keep public class * {     public protected *; }  -keepclassmembers enum * {     public static **[] values();     public static ** valueOf(java.lang.String); }  -keepclassmembernames class * {     java.lang.Class class$(java.lang.String);     java.lang.Class class$(java.lang.String, boolean); }  -keepclasseswithmembernames class * {     native <methods>; }  -keepclassmembers class * implements java.io.Serializable {     static final long serialVersionUID;     private static final java.io.ObjectStreamField[] serialPersistentFields;     private void writeObject(java.io.ObjectOutputStream);     private void readObject(java.io.ObjectInputStream);     java.lang.Object writeReplace();     java.lang.Object readResolve(); } 
like image 851
Nick Johnson Avatar asked Oct 16 '12 22:10

Nick Johnson


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?

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 obfuscate code?

You can obfuscate Android code to provide security against reverse engineering. You can use the Android ProGuard tool to obfuscate, shrink, and optimize your code.


1 Answers

According to the ProGuard Typical Library usage guide:

The "Signature" attribute is required to be able to access generic types when compiling in JDK 5.0 and higher.

Adding the following line fixed my issues with missing type parameters:

-keepattributes Signature 
like image 155
Nick Johnson Avatar answered Oct 31 '22 15:10

Nick Johnson