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
Related
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.
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
I am using ProGuard with my Android Studio Project. When I build my release I get the following note:
Note: com.google.common.cache.Striped64 accesses a declared field 'base' dynamically
Maybe this is program field 'com.google.common.cache.Striped64 { long base; }'
Maybe this is program field 'org.jsoup.nodes.Entities { java.util.Map base; }'
Maybe this is program field 'org.jsoup.nodes.Entities$EscapeMode { org.jsoup.nodes.Entities$EscapeMode base; }'
I tried to solve it with:
-keep class org.jsoup.** { *; }
-keeppackagenames org.jsoup.nodes
-keep class com.google.common.cache.Striped64 { *; }
-keep class com.google.common.base.** { *; }
but it did not help.
How do I resolve this Note?
To fix this you have to provide the following to your proguard configuration:
-keepclassmembers class ** {
** base;
}
This simply means do not rename all members named "base" of any type in any class.
Works like a charm.
Com4j integration works fine in my Java application, but not when using Proguard obfuscation (other options turned off). What could I be doing wrong ?
Program hangs at at the COM4J.createInstance(...). I have seen this code stop at the same position before, namely when the Com4j library could not find the underlying com4j.dll native file. However, this code is indeed working without Proguard.
package mypackage;
public abstract class ClassFactory {
private ClassFactory() {}
public static MyInterface createMyComObjectInstance() {
return COM4J.createInstance(MyInterface.class, ComObject_idString );
}
}
The interface is defined thus:
public interface MyInterface extends Com4jObject {....}
Ditto ProGuard config file (tried several variants).
-libraryjars myPath\com4j.jar
-keep public class mypackage.** {*;}
-keep public interface MyInterface {*;}
-keep public abstract class mypackage.ClassFactory {
public static MyInterface createMyComObjectInstance();
//also tried this
*;
}
Note also that Proguard works just fine on the rest of the code, including some JNI/native modules.
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)