In order to prevent the results of your labor from being stolen by others, obfuscated code can effectively prevent it from being decompiled. Let’s summarize the following steps for obfuscating code:
1. Everyone may notice that when building a new project, you will see such a file under the project., This is very important for obfuscating code. If you delete it accidentally, it doesn't matter. Copy one from other places.
2. The most important thing is to add an obfuscation statement:
a. Declare all your jar packages, for example:
-libraryjars libs/apns_1.0.
-libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so
-libraryjars libs/armeabi/
-libraryjars libs/baidumapapi_v2_3_1.jar
-libraryjars libs/
-libraryjars libs/
-libraryjars libs/gson-2.
-libraryjars libs/
-libraryjars libs/locSDK_4.
-libraryjars libs/ormlite-android-4.
-libraryjars libs/ormlite-core-4.
-libraryjars libs/universal-image-loader-1.9.
b. Declare the part you don't need to be confused, because some classes will cause the program to be compiled unsuccessful, as follows:
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends
-keep public class * extends .v4.**
-keep public class
--The above are all classes in the API, so it is best to avoid confusion
Some are very special, such as Baidu Map, you need to add the following statement:
-keep class .** { *; }
-keep class .**{*;}
According to my experience, it is best to avoid confusion in general models (model doesn't matter, and it doesn't matter much if it is not confusing) such as:
-keep class .** { *; }
Below is a statement about Umeng sharing statistics to avoid confusion
-dontwarn .v4.**
-dontwarn .**
-dontwarn .**
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembernames class * {
public <init>(, );
}
-keepclasseswithmembernames class * {
public <init>(, , int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf();
}
-keep class * implements {
public static final $Creator *;
}
-keepclasseswithmembers class * {
public <init>();
}
-dontshrink
-dontoptimize
-dontwarn .**
-dontwarn
-dontwarn .**
-dontwarn .**
-dontwarn .**
-keep enum .**
-keepattributes Exceptions,InnerClasses,Signature
-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep public interface .**
-keep public interface .**
-keep public interface .**
-keep public interface .**
-keep public interface .**
-keep public class .* {*;}
-keep public class javax.**
-keep public class .**
-keep class .**
-keep class .**
-keep public class .** {*;}
-keep class .**
-keep class {*;}
-keep class .** implements $IMediaObject {*;}
-keep class {*;}
-keep class .** implements $YXMessageData{*;}
-keep public class [your_pkg].R$*{
public static final int *;
}
3. After completing the above work, most of the obfuscation work has been completed. The final thing to do is to add your obfuscation file declaration to the file, as follows:
=${}/tools/proguard/:
4. OK, the last step is to test the signature package. If there is any problem, take a closer look at the Log class and may not be confused, then you have to add it to the file.
---------The above is the whole process of obfuscating the code.
The last codes are posted:
# To enable ProGuard in your project, edit # to define the property as described in that file. # # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in ${}/tools/proguard/ # You can edit the include path and order by changing the ProGuard # include property in . # # For more details, see # /guide/developing/tools/ # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class { # public *; #} -optimizationpasses 5 -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontpreverify -verbose -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* -keepattributes *Annotation* -keepattributes Signature -libraryjars libs/apns_1.0. -libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so -libraryjars libs/armeabi/ -libraryjars libs/baidumapapi_v2_3_1.jar -libraryjars libs/ -libraryjars libs/ -libraryjars libs/gson-2. -libraryjars libs/ -libraryjars libs/locSDK_4. -libraryjars libs/ormlite-android-4. -libraryjars libs/ormlite-core-4. -libraryjars libs/universal-image-loader-1.9. -keep class .** { *; } -keep class .**{*;} -keep public class * extends -keep public class * extends -keep public class * extends -keep public class * extends -keep public class * extends -keep public class * extends -keep public class * extends -keep public class * extends -keep public class * extends .v4.** -keep public class -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep public class * extends com. -keep public class * extends com. -keep class -keep class .v4.** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class .** { *; } -keep class com..** { *; } -keep class com..** { *; } -keep class com..** { *; } -keep class com..** { *; } -dontwarn .v4.** -dontwarn .** -dontwarn .** -keepclasseswithmembernames class * { native <methods>; } -keepclasseswithmembernames class * { public <init>(, ); } -keepclasseswithmembernames class * { public <init>(, , int); } -keepclassmembers enum * { public static **[] values(); public static ** valueOf(); } -keep class * implements { public static final $Creator *; } -keepclasseswithmembers class * { public <init>(); } -dontshrink -dontoptimize -dontwarn .** -dontwarn -dontwarn .** -dontwarn .** -dontwarn .** -keep enum .** -keepattributes Exceptions,InnerClasses,Signature -keepattributes *Annotation* -keepattributes SourceFile,LineNumberTable -keep public interface .** -keep public interface .** -keep public interface .** -keep public interface .** -keep public interface .** -keep public class .* {*;} -keep public class javax.** -keep public class .** -keep class .** -keep class .** -keep public class .** {*;} -keep class .** -keep class {*;} -keep class .** implements $IMediaObject {*;} -keep class {*;} -keep class .** implements $YXMessageData{*;} -keep public class [your_pkg].R$*{ public static final int *; }
Updated on 2015-05-25
1. JAR statement that depends on the project:
The following is an example of email sending
-libraryjars ../KlowerBase/libs/
-libraryjars ../KlowerBase/libs/
-libraryjars ../KlowerBase/libs/
2. Solve the exception of email sending time
-keep class .**{*;} -keep class .**{*;} -keep class .**{*;} -keep class .**{*;} -keep class .**{*;}
#Solution: can't find referenced class-dontwarn -dontwarn .** #Solution: can't find referenced class-dontwarn .**
Through this article, I hope to help friends who use obfuscated code for Android development applications. Thank you for your support for this site!