Obfuscation and packaging of android code

Source: Internet
Author: User

Obfuscation premise

1. First, your project already has at least a few packages (for different paths). Don't think you can write a Helloworld in MainActivity to see the effect...

Try it with a demo that has been completed... It should also be fast...

2. You must generate the apk file in the form of packaging... Otherwise, the apk generated by dug does not have the obfuscation function (at least I did not generate it when I tried it. Others ).

Remember: The above two points can be used for code obfuscation...

The following are reproduced in: http://www.th7.cn/Program/Android/201301/122409.shtml

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-Keep class com. badlogic. gdx. backends. android .**{*;}

This is just not obfuscation: the class under this package (excluding the sub-Package) will use the class in the third article to store the package of this class, like the above and pay attention to the problem of the android-support-v4.jar package, here with the processing of this jar package third-party jar obfuscation,-optimizationpasses 5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontpreverify-verbose! Code/simplification/arithmetic ,! Field /*,! Class/merging/*-keep public class * extends android. app. activity // inherits activity, application, service, broadcastReceiver, contentprovider .... do not confuse-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 androi D. app. backup. backupAgentHelper-keep public class * extends android. preference. preference-keep public class com. android. vending. licensing. ILicensingService // here handle third-party jar packages, third-party JAR package processing starts-libraryjars/libs/android-support-v4.jar-libraryjars/libs/gdx-backend-android.jar-libraryjars/libs/gdx. jar // The third-party jar package WARN-dontwarn com. badlogic. **-dontwarn android. support. v4. **-dontwarn android. support. v4. View. ** // do not confuse the classes of Third-Party jar packages-keep class com. badlogic. gdx. backends. android. ** {*;}-keep class com. badlogic. gdx. ** {*;}-keep class com. badlogic. gdx. graphics. g2d. ** {*;}-keep class com. badlogic. gdx. graphics. ** {*;}-keep class android. support. v4.view. ** {*;} // third-party JAR package processing ends-keepclasseswithmembernames class * {// The natvie method is not obfuscated with native <methods> ;} -keepclasseswithmembers class * {// For all classes, this constructor does not enter Line obfuscation is mainly used to customize the view 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 {// The onclick method android: onclick = "onClick" written in layout. Do not confuse public void * (android. view. view);}-keepclassmembers enum * {public static * * [] Values (); public static ** valueOf (java. lang. string);}-keep class * implements android. OS. parcelable {public static final android. OS. parcelable $ Creator *;} ------------------------------------------------------ 1 above is to parse which operations else if the project introduces the jar class library of the Android-support-v4, then in the project packaging obfuscation, there will be an error message. For example, You may need to specify additional library jars (using '-libraryjars '). The solution is provided here, and we will explain how to deal with similar situations later: In proguard. add the following content after cfg: 1. -libraryjars/android-support-v4.jar 2. -dontwarn android. support. v4. ** 3. -keep class android. support. v4. ** {*;} 4. -keep public class * extends android. support. v4. ** 5. -keep public class * extends android. app. fragment, and then you can package it again. You should be able to generate the apk installation package normally. Packaging error: Case 1: "class 1 can't find referenced class 2" literally means class 1 cannot find the reference of class 2; It will suggest you: "You may need to specify additional library jars (using '-libraryjars '). "; Use-libraryjars and add the third-party libraries used in the project. For example:-libraryjars/android-support-v4.jar Note: here the reference method is the root directory of the current project (you can also configure other directories), that is, you need to put the third-party jar under the current directory, otherwise, a warning is reported that the jar file cannot be found! Scenario 2: can't find superclass or interface android. OS. parcelable $ ClassLoaderCreator. In this case, you can use-dontwarn com. xx. yy. **, do not give a warning about the error. Note: to use this method, make sure you do not use the class in this library! Otherwise, the ClassNotFoundException will be thrown! Case 3: The class is used in the project, but the above method is not enough. At this time, we need to add another item:-keep class com. xx. yy. ** {*;} to keep the current class from being confused. Summary: to reference a third-party package, you can use the following method to avoid packaging errors:-libraryjars/aaa. jar-dontwarn com. xx. yy. **-keep class com. xx. yy. ** {*;} Listen 2 ------------------------------------------------------------------------ proguard. if you think about the obfuscation of the cfg configuration, you will find that some classes, methods, variables, and other names provided to the external are changed, the functions of the program cannot be implemented normally. So how does Proguard know what can be renamed, But what cannot be changed? This is configured by using the proguard. cfg file. The default automatically generated proguard. cfg in the Android project has been configured for the general situation of Android. We open this configuration file. The content is roughly as follows: 1.-optimizationpasses 5 2.-dontusemixedcaseclassnames 3.-dontskipnonpubliclibraryclasses 4.-dontpreverify 5.-verbose 6.-optimizations! Code/simplification/arithmetic ,! Field /*,! Class/merging/* 7. 8. -keep public class * extends android. app. activity 9. -keep public class * extends android. app. application 10. -keep public class * extends android. app. service 11. -keep public class * extends android. content. broadcastReceiver 12. -keep public class * extends android. content. contentProvider 13. -keep public class * extends android. app. backup. backupAgentHelper 14. -keep public class * Extends android. preference. preference 15. -keep public class com. android. vending. licensing. ILicensingService 16. 17. -keepclasseswithmembernames class * {18. native <methods>; 19 .} 20. 21. -keepclasseswithmembers class * {22. public <init> (android. content. context, android. util. attributeSet); 23 .} 24. 25. -keepclasseswithmembers class * {26. public <init> (android. content. context, android. util. attrib UteSet, int); 27 .} 28. 29. -keepclassmembers class * extends android. app. activity {30. public void * (android. view. view); 31 .} 32. 33. -keepclassmembers enum * {34. public static ** [] values (); 35. public static ** valueOf (java. lang. string); 36 .} 37. 38. -keep class * implements android. OS. parcelable {39. public static final android. OS. parcelable $ Creator *; 40 .} it mainly retains the attributes inherited from Activity, Application, and Servic. E. Subclass of BroadcastReceiver, ContentProvider, BackupAgentHelper, Preference, and ILicensingService. These subclasses may be called externally. In addition, it retains the classes containing native methods and the classes constructed by constructor from xml (generally the subclass of View) the values and valueOf static methods in the enumerated type, and the cross-process data class that inherits Parcelable. In an actual project, the configuration automatically generated by Google may not be competent for our obfuscation work. Therefore, we often need to write some ProGuard configurations by ourselves. Please refer to Manual> Usage on the official website for details.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.