Obfuscate the dandroid jar (sdk) package through the SDK's own ProGuar tool

        I haven't written a technical blog for a long time, and I haven't written it for 5 years in a blink of an eye. I have a little time recently, and I plan to publish all the contributions accumulated in the past few years. I hope that the majority of bloggers will support and beat bricks and enter the topic.

        In the process of android development, you will encounter jar packages developed (packaged) by yourself that need to be provided to others. At this time, you don't want to expose the source code to others, so you will use jar packages to obfuscate. Then there are many examples of Internet promotion or methods written by other people that do not work. After more than a week of hard work, I finally succeeded. The following is the experience.

1. Download the latest obfuscation tool

      If jdk is 1.8 and above, download address : http://proguard.sourceforge.net/index.html#downloads.html

      If the jdk is below 1.8, you can use the one that comes with the sdk. The path is in "your sdk path\tools\proguard"

 

2. The next step is to write the pro file.

     I believe that many people have tried more than N obfuscated files, but although the obfuscation succeeded, the final operation still failed. The following is my file. The following files can be obfuscated and run without errors. You can modify it with confidence. Remember to put Comments are removed to use.

    

//The jar package that needs to be obfuscated
-injars 'D:\ProGuard\acquireurlhistory.jar'
//The jar package generated after the confusion is successful
-outjars 'D:\ProGuard\output\mj_sdk_20160425.jar'
//The third-party jar package you need to reference in the jar package you opened, I put these jar packages on my D drive
-libraryjars 'D:\Program Files\Java\jdk1.8.0_73\jre\lib\rt.jar'
-libraryjars 'D:\ProGuard\libs\android.jar'
-libraryjars 'D:\ProGuard\libs\annotations.jar'
-libraryjars 'D:\ProGuard\libs\dbutil.jar'
-libraryjars 'D:\ProGuard\libs\mjoysmaster.jar'
-libraryjars 'D:\ProGuard\libs\protobuf-java-2.5.0.jar'

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-optimizationpasses 5
-dontusemixedcaseclassnames
//The original data cannot be confused
-keepattributes *Annotation*
-verbose



//The R file referenced in the jar package you develop does not need to be confused
-dontwarn com.androld.tools.R*


-keep class org.apache.3rdparty.stuff.**

-keep public class * extends android.app.Activity

-keep public class * extends android.app.Application

-keep public class * extends android.app.Service

-keep public class * extends android.content.BroadcastReceiver

-keep public class * extends android.content.ContentProvider

-keep public class * extends android.app.backup.BackupAgentHelper

-keep public class * extends android.preference.Preference


//This is the sdk entry that I have opened up for others to call. It should be excluded and not confused.
-keep class com.androld.tools.mjoys.MjoysSdkAgent {
    <fields>;
    <methods>;
}
//Similarly, the initialization of DB should be open
-keep class com.androld.tools.common.MJDBHelper {
    <fields>;
    <methods>;
}
//The methods in the open interface can't be confused either
-keepclassmembers public class com.androld.tools.mjoys.MjoysSdkAgent {
    public static <fields>;
}
// same as above
-keepclassmembers public class com.androld.tools.common.MJDBHelper {
    public static <fields>;
}



-keepclasseswithmembers class * {
    public <init>(android.content.Context,android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context,android.util.AttributeSet,int);
}

-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

-keep class * extends android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

-keepclasseswithmembers,allowshrinking class * {
    native <methods>;
}

# Also keep - Enumerations. Keep the special static methods that are required in
# enumeration classes.
-keepclassmembers enum  * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

 The places with comments above are for my own needs, so I have made changes. Others without comments should not be changed. I believe that everyone else has written the same as mine, so I will not explain more.

 

3. Enter the bin directory of proGuard

     

 

 

 See the following interface:

   

 

If the file is written incorrectly, it will prompt the wrong Wu, and carefully observe whether the comment in the file is not deleted or the grammar is wrong. Then click the process on the left to enter the obfuscation interface, and click the process in the lower right corner to start the obfuscation. If you see "successfully" in the content, it means that the obfuscation is successful, but it does not necessarily run without error, so to test whether the obfuscated jar package is available, you can also see you There is an error in the code, and then follow the above method to exclude your class or method from confusion until it succeeds.

 



 

So far, it has been completed. It took me a week to complete it. Come on, everyone, I will put my proGuard file in the attachment for everyone to download.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326830519&siteId=291194637