Android program added code obfuscation

Source: Internet
Author: User

Add code obfuscation, mainly to add proguard-project.txt file rules to confuse, before the new Android program is proguard.cfg File

Take a look at the general rules I'm using (proguard-project.txt file)

-optimizationpasses 5- Dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontskipnonpubliclibraryclassmembers-dontpreverify-verbose-opt Imizations!code/simplification/arithmetic,!field/*,!class/merging/*-keepattributes *Annotation*- Renamesourcefileattribute sourcefile-keepattributes sourcefile,linenumbertable# The following two commands match the path of the class to delete the-allowaccessmodification-repackageclasses "# Record generated log data, under the Proguard directory-dump Class_ Files.txt-printseeds seeds.txt-printusage unused.txt-printmapping mapping.txt# exception can be ignored to open #-dontwarn-keep public Class * Extends Android.app.activity-keep public class * extends Android.app.application-keep public class * extends Andro Id.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-keep public class Com.android.vending.licensing.ilicensingservice-dontnote Com.android.vEnding.licensing.ilicensingservice-keepnames class * Implements java.io.serializable# explicitly preserve all Serialization. The Serializable interface# is only a marker interface, so it wouldn ' t save them.-keepclassmembers class * Implements Java . Io. Serializable {static final long serialversionuid;private static final java.io.objectstreamfield[] serialpersistentfields;private void WriteObject (java.io.ObjectOutputStream);p rivate void ReadObject ( Java.io.ObjectInputStream); Java.lang.Object writereplace (); Java.lang.Object readresolve ();} # Preserve All native method names and the names of their Classes.#-keepclasseswithmembernames class * {#native; #}#-keepc Lasseswithmembernames class * {#public (android.content.Context, android.util.AttributeSet); #}#- Keepclasseswithmembernames class * {#public (android.content.Context, android.util.AttributeSet, int); #}# Preserve Static fields of inner classes of R classes, might be accessed# through Introspection.#-keepclassmembers class**. r$* {#public static; #}# Preserve the special static methods that is required in all enumeration Classes.-keepclassmember s enum * {public static **[] values ();p ublic static * * VALUEOF (java.lang.String);} -keep class * Implements android.os.Parcelable {public static final Android.os.parcelable$creator *;} # Open #-keep public class * {# public protected *;#} If your project is externally available method call

If there's a rule, add it to the back.

In addition, we generally need to confuse the code of their own, the introduction of the third-party library files are not to be confused, otherwise the following error will occur:


Just can't find the class you need. So third-party libraries need to be pointed out to be not confused by the following methods:

Above is android.support.v4.x cannot find the required class, see the package name of the incoming


Add the Proguard-project.txt behind the

-dontwarn android.support.v4.**-keep class android.support.v4.** {*;}

The first sentence is to ignore this warning, the second sentence is to keep the class not confused

After you have finished all the third-party libraries you have introduced, add a sentence in the project.properties file.

Proguard.config=proguard-project.txt

You can compile it (how do I compile it?) )

After compiling, you can see in the bin confusing code apk file, is not the file becomes smaller, more cool!

Meanwhile, there are several more files in the bin/proguard folder.



Among them, Mapping.txt is all the confusion of the class and the variable name of the reference,usage.txt is you in the code is useless to the method, are listed for you, is the result of optimization.



Android program added code obfuscation

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.