I have the following location in my project:
package com.example.data
Where I have placed four classes, A, B, C and D that look like this:
public class A {
public List<B> bList = new ArrayList<>();
}
public class B {
public List<C> CList = new ArrayList<>();
}
public class C {
public D d;
}
public class D {
public String s;
}
Now, in my ProGuard I can add simething like this:
-keepclassmembers class com.example.data.data.A{*;}
-keepclassmembers class com.example.data.data.B{*;}
-keepclassmembers class com.example.data.data.C{*;}
-keepclassmembers class com.example.data.data.D{*;}
But is there a more simpler way I can avoid obfuscation in my classes configuration?
You can give a rule to the package level. So all classes under this package will not be obfuscated. In your proguard rules file, for example in proguard-common.pro file, add the below code.
-keep class com.example.data.** { *; }
Related
Can someone help me about, obfuscated or give me example to do this?
I created an .aar file and .jar file and put the class of getter and setter that will give value if they access on it.
but the thing in need to put the hidden values that someone will not see what is the value on it.
package com.example.test;
public class MyClass extends privateClass{
String testing;
public MyClass() {
this.testing = getStringExample();
}
public String getTesting() {
return testing;
}
public void setTesting(String testing) {
this.testing = testing;
}
}
and this class must be hide/obfuscated to the other developers if i give my library
package com.example.test;
public class privateClass {
String getStringExample()
{
return "TEST RESULT";
}
}
Note: I tried to put proguard too, and check the library but still they can see my private class, , i tried to use interface and extends the class but still the same,
here is my proguard example:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontwarn ccom.example.test.R*
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class
-keepclassmembers class com.example.test.** { *; }
-keep class com.example.eyefixdata.** {
void set*(***);
void set*(int, ***);
boolean is*();
boolean is*(int);
*** get*();
*** get*(int);
}
Please save my day. hope you help me.
Thanks in advance.
You can move your private classes/interfaces to other packages, e.g. put your privateClass to an internal package package com.example.your.library.internal; to distinguish with your public classes/interfaces.
package com.example.your.library.internal;
public class privateClass {
String getStringExample()
{
return "TEST RESULT";
}
}
And add below line to your proguard configuration
-keep public class com.example.your.library.* { public *; }
Note that you should use single wild char * to NOT obfuscate the internal packages.
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?
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.
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>;
}
-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)
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 {*;}.
package com.hello
public class Outer extends Activity{
int a;
int b;
array c;
Boolean bol;
public void onCreate() {
a=1;
b=2;
c=new int[10];
bol=true;
}
class Inner implements Runnable {
public void run() {
if (bol)
for (int e=0;e<10;e++) c[e]=a+b;
}
}
}
How can I tell Progruad to keep the name of the inner class and the variables used in the inner class even though they are defined in the onCreate method?
I don't want to list each variable one by one.
to keep inner classes:
# specific class (in brackets for example we keep all)*
-keep class OuterFQNClassName$InnerClassName { *; }
# all inner classes of outer class
-keep class OuterFQNClassName$** { *; }
# all inner classes
-keepattributes InnerClasses
to keep fields with desired public/protected/private/default access level modifiers):
# for specific class
-keepclassmembers class FQNClassName {
protected <fields>;
}
# for all classes
-keepclassmembers class * {
protected <fields>;
}
# other approach for default acl
# be careful as method could have also native modifiers :)
-keepclassmembers class * {
!public !protected !private *;
}
*naming convention with a '$' separator between the names of inner classes and outer classes
please study a proguard docs:
https://www.guardsquare.com/en/proguard/manual/attributes
I want to know if its possible to keep the name of a public static inner Class but renaming the parent classname.
My code looks like this:
public class MyDao extends AbstractDao {
public static final String TABLENAME = "BOX_DOWNLOAD";
public static class Properties {
public final static Property ID = new Property(0, Long.class, "ID", true, "ID");
public final static Property Name = new Property(1, String.class, "name", false, "NAME");
public final static Property Done = new Property(2, Boolean.class, "done", false, "DONE");
// SOME MORE CONSTANTS
};
// SOME CODE WHICH CAN BE OBFUSCATED
}
I want ProGuard to replace:
MyDao (the className)
the PROPERTIES' variables (ID, Name, Done)
I want ProGuard NOT to replace:
TABLENAME (variable name)
PROPERTIES (className only)
I tried this
-keepclassmembers class * extends de.greenrobot.dao.AbstractDao {
public static <fields>;
public static class *;
}
But this is not working. The classNames are not obfuscated.
// EDIT
I forgot to say that there are several classes like MyDao. Eg. MyDao1, MyDao2, etc.
I want to use wildcards.
Cfr. ProGuard manual > Usage > Keep Options
-keepclassmembers class de.greenrobot.dao.MyDao {
String TABLENAME;
}
-keep class de.greenrobot.dao.MyDao$Properties
Update: When keeping the name "MyDao$Properties", the current version of ProGuard appears to keep the name "MyDao" as well (even if the InnerClasses attribute is not preserved). This is somewhat more conservative than strictly necessary.
I have made following scripts, which works for me.
-keep class my.dao.package.*$Properties {
public static <fields>;
}
-keepclassmembers class my.dao.package.** {
public java.lang.String TABLENAME;
}
I have used wildcard for inner classes, which worked.
For my Android instrumentation test I need a few extra entry point into my classes. Those methods are not used in the actual application. My idea was to start them all with test_ and have a general rule to exclude them from being optimized away. This is how far I got:
-keepclassmembers class com.xxx.**.* {
public ** test_* ();
public ** test_* (**);
public static ** test_* ();
public static ** test_* (**);
}
But it still does not work. public static void test_destroy (final android.content.Context context) and private void dropTables (final SQLiteDatabase db) has just been removed from the code. And I have no idea why.
How is it properly used for wildcard patterns?
The solution is
-keepclassmembers class com.XXX.**.* {
*** test_* (...);
}
Another way to do this is to use an annotation (i.e. guava's #VisibleForTesting) to mark those methods. Then in proguard you can keep all entry points and members with that annotation:
-keep #com.google.common.annotations.VisibleForTesting class *
-keepclasseswithmembers class * {
#com.google.common.annotations.VisibleForTesting *;
}