Pushpa
Pushpa

Reputation: 942

How to not obfuscate interface methods & its parameters using Proguard in Android?

I have the following code:

public class MyClass {
    public void method1(Integer marks) {

    }

    private String method3(String name){

    }
    public interface interface1 {
        void method4(Integer ID);
        void method5(Integer rate, boolean status);
    }
}

I have used progaurd-rules.pro

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keepparameternames

-keep public class *
-keepclassmembers public class *{
   public *;
 }
-keep public interface packageName.MyClass$interface1 { *; }

Obfuscated code as below:

public class MyClass {
    public void method1(Integer marks) {

    }

    private String a(String var1){

    }
    public interface interface1 {
        void method4(Integer var1);
        void method5(Integer var1, boolean var2);
    }
}

I want the interface methods variables (ID, rate & status) not to obfuscate. i.e as below

public interface interface1 {
    void method4(Integer ID);
    void method5(Integer rate, boolean status);
} 

How can it be possible?

Upvotes: 16

Views: 5947

Answers (4)

scottyab
scottyab

Reputation: 24039

-keepattributes LocalVariableTable,LocalVariableTypeTable

The above keepattributes didn't work for me. However -keepparameternames did. I added this to the internal Proguard config that our Android Library uses. The other non keot classes still have their params obfuscated.

Note: I'm using R8 to actually obfuscate which is the default when using the Android Gradle Plugin since 3.4.0 also we are enforcing source and target compatibility to 1.8 (due to unrelated okhttp dependency)

Upvotes: 0

Kuldip Sharma
Kuldip Sharma

Reputation: 792

public interface SSOListener {
    void sendDataToAnalytics(String event, JSONArray object);
}

// In my case JsonArray was obfuscated.


Solution :
-keep class org.json.JSONArray**, ** {
    protected <fields>;
    public <fields>;
    <methods>;
}

Upvotes: 1

andrei_zaitcev
andrei_zaitcev

Reputation: 1458

You could keep method's arguments by adding extra flags to -keepattributes. They look like this:

-keepattributes LocalVariableTable,LocalVariableTypeTable

Unfortunately, this keeps arguments from obfuscation not only in the interface you want, but in the entire project. Maybe that's fine for you.

If you're using a default proguard configuration shipped along with Android SDK then you could also use a special annotation to prevent some classes from obfuscation. Check it out.

Upvotes: 3

AAryan
AAryan

Reputation: 20140

ProGuard uses the naming convention of Java bytecode, as seen in class file names and stacktraces. Therefore:

-keep public interface com.somepackage.SomeClass$someInterface {*;}

In case if your interface is not public.

-keep interface com.somepackage.SomeClass$someInterface {*;}.

Upvotes: -1

Related Questions