Proguard rules to keep public static method name unchanged in AAR library - android

How to keep static method name unchanged and obfuscate everything inside the method.
Example code:
public class MyClass {
public static void MyFunc(Conetext context, String str, int i) {
Toast.makeText(context, "Hello world "+ str + i, Toast.LENGTH_SHORT).show();
}
}
Here I want to keep MyFunc unchanged.
Amit Vaghela why are you doing this? If you don't understand what I asked please ignore my post.
Do you think -keepclassmembernames will obfuscate entire code inside the MyFunc? I tested:
-optimizationpasses 5
-keep,allowobfuscation class *
-keepclassmembers class * {
public static void * (...);
}
And it didn't work and that's why I am here to ask. If you cannot help you better ignore it.

Related

How to Obfuscated Jar or AAR files

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.

-keep option in proguard-rules.pro

sorry for the beginner level question because I am also beginner.So this question helpful for others.
My Java Class is :
public class Dog{
String breed;
int ageC
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
proguard keep line for this object
-keep public class mypackageName.Dog
my question is after compile class Dog's member variables and method obfuscated or not??
sorry for the bad English :(
if u use the keep option it wont be obfuscated .
like
-keep class mypackageName.Dog.** { *; }

Remove LibGDX logs in Android using ProGuard

I have a proguard.cfg file which contains several statements including optimization passes and logs suppression as :
-assumenosideeffects class android.util.Log { *; }
-assumenosideeffects class com.badlogic.gdx.Application {
public static void debug(...);
public static void error(...);
public static void log(...);
}
Calls to Log.* are correctly removed in the final output APK file. But gdx log calls are still in the code. For example I can still see things like that in the output :
Gdx.app.debug("debug()", "^");
Gdx.app.error("error()", "^");
Gdx.app.log("log()", "^");
I also tried to put this part of my config in a proguard-optimize.txt file as I have seen on similar questions and then setting the proper value in project.properties files like this : proguard.config=proguard-optimize.txt:proguard.txt but it doesn't work !
These calls get removed only if I put a general wildcard :
-assumenosideeffects class com.badlogic.gdx.Application {
*;
}
But I don't want to remove calls to other Application's static methods, like add* and get*() ones.
Optimization step is enabled (6 passes).
Gdx.app.debug is not static its an instance method (app is a static field of the Gdx class).
Try:
-assumenosideeffects class com.badlogic.gdx.Application {
public void debug(...);
public void error(...);
public void log(...);
}
Try something like this in your application's code:
Gdx.app.setLogLevel(Application.LOG_NONE);
That will prevent messages from being logged.
Cheers!

ProGuard: Keep inner static class but rename parent class

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.

How to keep my test methods with proguard.cfg

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

Categories

Resources