I am trying to ProGuard a public-facing class (MyClass) file containing an inner enum (MyInnerEnum). However, after ProGuarding, the inner enum is separated out into its own class file MyClass$MyInnerEnum. How can I prevent ProGuard from separating out the inner enum? I need to be able to access the inner enum using dot notation, like MyClass.MyInnerEnum.ENUM_VALUE and not using the dollar sign like MyClass$MyInnerEnum.ENUM_VALUE.
My public-facing class:
package com.myclass;
public interface MyClass {
enum MyInnerEnum {
ENUM_VALUE
}
}
I've tried these ProGuard configuration options to no avail:
<option>-keep enum com.myclass.MyClass** { *; }</option>
<option>-keep enum com.myclass.MyClass$* { *; }</option>
<option>-keep enum com.myclass.MyClass$MyInnerEnum { *; }</option>
You can give it a try by keeping the outer class (in your case interface) and the inner enum. By adding the following lines to your proguard config, this should work:
-keep interface com.myclass.MyClass
-keep enum com.myclass.MyClass$MyInnerEnum
I meet the same problem. I resolve it by adding:
# don't proguard all inner enum, need to cooperate with "-keep enum com.companyname.abcd.ClassName$EnumName"
-keepattributes InnerClasses
...
-keep enum com.myclass.MyClass$MyInnerEnum
Related
Using -keep does not keep methods and fields. They are obfuscated but I do not want to obfuscate some classes with methods and fields.
Rules used
-target '11'
-keep public class com.example.MyClass
If I use and proguards returns a class which Java Decompiler can not decompile (message 'Internal Error' after decompiling) e.g.
-keep public class com.example.MyClass {
<methods>;
<fields>;
}
I tried also [*;}.
Is there something wrong/bug with ProGuard Version 7.3.0 and using option target '11'?
-keep interface com.example.ApiContext { *; }
-keep abstract class com.example.ControlContext { *; }
-keep class com.example.CloseInstanceContext { *; }
I think 'abstract' was the problem, that it did not work.
In order to configure proguard to keep all classes from a single package, say org.myorg.special, the following notation works:
-keep class !org.myorg.special { *; }
I would like to configure proguard to keep all classes except when they are from either of two packages, say org.myorg.special and org.myorg.another.
I have tried for instance
-keep class !(org.myorg.special,org.myorg.another) { *; }
but the above syntax is not supported by proguard.
What is the correct syntax?
-keep class ![org.myorg.special,org.myorg.another],org.** { *; }
I defined an annotation named #KeepAll.
I have an interface like
#KeepAll
public interface MainEntity {
//some methods
}
I want to keep all classes which implement this interface from obfuscation. Is this possible on ProGuard?
NOTE I know I can define it as
-keep public class * implements **.MainEntity
But I don't want to specify interface name but annotation name.
After a long trial and error process I get what I want. Here is the solution
Keep class names with annotation KeepAll;
-keep #com.package.name.KeepAll public class **
Keep class members of classes and interface with annotation KeepAll;
-keepclassmembers #com.package.name.KeepAll class ** { public <methods>; <fields>;}
Keep class members of a class which implemets a class that has KeepAll annotation. (This was what I want)
-keepclassmembers public class * implements #com.package.name.KeepAll ** { public <methods>; <fields>;}
You can tell ProGuard to keep everything with an annotation like this:
-keep #com.google.inject.Singleton public interface *
The above would keep the interface itself from obfuscation.
To get the implementations of the interface you can do something like this:
-keep public class * implements **.MainEntity
So Right now I am confused what you want to achieve. If you only annotate the interface it wont be a help for ProGuard. The classes would need this annotation.
Parent question: Android proguard, keep inner class
My problem is with inner class of inner class
One of the SDKs in my android project has a class A, which has two static inner class. They are found to be stripped after applying proguard.
public class A{
....
static class B{
...
static class D {
....
}
}
static class C{
...
}
}
My proguard looks like this
-keepattributes Exceptions, InnerClasses
-keep class com.xxx.A
-keep class com.xxx.A$*
Which prevents class B, C from proguard. But no luck with class D. I have tried -keep class com.xxx.A$** as well.
I think you're missing the Class specification as shown in the ProGuard manual.
Try replacing:
-keep class com.xxx.A
With:
-keep class com.xxx.** {*;}
I'm using that rule with the following file and it's working fine on Android Studio 2.2.3 with build tools 25.0.1 (just in case those might affect the version of ProGuard being used)
A.java
package com.xxx;
public class A {
....
public class B {
....
public class C {
....
}
}
}
As you can see the only real difference between my file and yours is that my inner classes are public and non-static.
If that doesn't work
You can always use a rule without wildcards. The following will do the trick:
-keep class com.xxx.A$B$D
I use in my project a piece of code as described here
http://lexandera.com/2009/01/extracting-html-from-a-webview/
I create the .apk file, install it on my device and it correctly works. If I try to use the obfuscation with proguard the project fails, the method showHTML(String html) of MyJavaScriptInterface is not reached.
My proguard configuration regarding that
-keep public class com.mypackage.MyClass.MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass.MyJavaScriptInterface
-keepclassmembers class * implements com.mypackage.MyClass.MyJavaScriptInterface {
<methods>;
}
according to this this answer Android proguard Javascript Interface problem.
SOLVED.
As Eric suggested, I changed the Proguard configuration file like this:
-keep public class com.mypackage.MyClass$MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
<methods>;
}
Now my project works perfectly.
For API 17+ you also need to preserve the #JavascriptInterface annotations:
-keepattributes JavascriptInterface
http://developer.android.com/reference/android/webkit/JavascriptInterface.html
If MyJavaScriptInterface is an inner class of MyClass, ProGuard expects a fully qualified name com.mypackage.MyClass$MyJavaScriptInterface. The naming convention with $ is used in the compiled class files on which ProGuard operates. Note that ProGuard mentions class names in the configuration that it can't find in the input jar, suggesting that these names may have been misspelled.
-keepclassmembers class com.mypackage.MyClass$JavaScriptInterface {
public *;
}
Use only this. It works for me.
Those Who are laze to provide the entire package path.
-keepclassmembers class **.*$PaymentJavaScriptInterface{
public *;
}
As suggested by edit in question,
out of those suggestions,
only using
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {
public *;
}
with Important -
For API 17+ to preserve #JavascriptInterface annotations:
-keepattributes JavascriptInterface
(Which was stopping my app to work on Marshmallow)