Note: com.google.common.cache.Striped64 accesses a declared field 'base' dynamically - java

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.

Related

Keep methods and fields with ProGuard

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.

Advanced pattern matching in proguard to keep multiple packages

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.** { *; }

Android proguard, keep inner class of Inner class

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

Proguard vs Annotations

I have an app that uses ActiveAndroid, a database ORM library, that relies on annotations.
#Table(name="test")
public class DatabaseItem extends ActiveRecordBase<DatabaseItem> {
public DatabaseItem(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Column(name="counter")
public int counter;
}
How do I get Proguard working nicely with this? Currently, I get errors about not finding a column name by ActiveAndroid when using Proguard. I guess it somehow mangles the annotation.
My relevant Proguard configuration:
#ActiveAndroid
-keep public class com.activeandroid.**
-keep public class * extends com.activeandroid.ActiveRecordBase
-keepattributes Column
-keepattributes Table
Column and Table aren't existing java class file attributes. You'll at least have to specify
-keepattributes *Annotation*
Cfr. the ProGuard manual.
In March 2013, Proguard version 4.9 was released, one of the fixes were:
Fixed overly aggressive shrinking of class annotations.
So make sure that your Proguard version is up to date and then use Eric Lafortune's solution:
-keepattributes *Annotation*
You can also use this configuration to store all class members that has a specific annotation:
-keepclassmembers class * {
#fully.qualified.package.AnnotationType *;
}
Solution was to keep all members of the library and the database classes
-keep class com.activeandroid.**
{
*;
}
-keep public class my.app.database.**
{
*;
}
-keepattributes Column
-keepattributes Table
For those only using Gradle, the solution is very similar (note the single quotes around the Annotation):
keep 'public class java.package.** { *; }'
keepattributes '*Annotation*'
This is especially useful if you are using JSON serialization annotations (e.g., Jackson or the like) in a vanilla Gradle project.
This what worked in my case:
-keep class com.activeandroid.** { *; }
-keep class com.activeandroid.**.** { *; }
-keep class * extends com.activeandroid.Model
-keep class * extends com.activeandroid.serializer.TypeSerializer
-keep public class * extends com.activeandroid.ActiveRecordBase
-keepattributes Column
-keepattributes Table
-keepattributes *Annotation*
-keepclasseswithmembers class * { #com.activeandroid.annotation.Column <fields>; }

Android Proguard Javascript Interface Fail

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)

Categories

Resources