After writing the Android program, we usually want to encode code, so as to ensure that our application in the market will not be anti-compilation, and then crack, so at this time need to release the official version of the time, there are some kinds of things can not be confused, such as the implementation of the Serializable interface, Otherwise, there will be an error in deserialization, in which case we can simply add a configuration to the Proguard.cfg (Eclipse) to resolve:
-keepnames class * Implements Java.io.serializable-keepclassmembers class * implements java.io.Serializable {static Final long serialversionuid;private static final java.io.objectstreamfield[] serialpersistentfields;! Static!transient <fields>;p rivate void WriteObject (java.io.ObjectOutputStream);p rivate void ReadObject ( Java.io.ObjectInputStream); Java.lang.Object writereplace (); Java.lang.Object readresolve ();}
The above configuration indicates that all classes that implement the Serializable interface and their members are not confused. Again such as JS calls the Android Javascriptinterface code method, such as individual control layer class need to reflect, individual entity class need JSON to save local, these are to prevent no obvious common characteristics of the class is confused, this time, how do we do it, Add it to proguard.cfg? This causes the Proguard configuration file to be cluttered and requires all members of the team to have an understanding of their syntax. Here's a little trick to share, by adding a common identity to these classes, properties, functions, and then unifying filtering, finally solving the problem 1. Create a new annotation that represents a unified identity Notproguard
Package Cn.trinea.android.common.annotation;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target;/* * * Notproguard, Means not proguard something, like class, method, field<br/>*/@Retention (Retentionpolicy.class) @ Target ({elementtype.type, Elementtype.method, Elementtype.constructor, Elementtype.field}) public @interface Notproguard {}
Notproguard is a compile-time annotation and does not have any impact on run-time performance. You can modify a class, method, constructor, property. Here, there may be some ambiguity about annotations (Annotation), and to learn more about annotations you can click on the link: Java Annotation summary 2. Filter elements that are modified by this annotation in the Proguard configuration file
# Keep annotated by Notproguard-keep @cn. Trinea.android.common.annotation.NotProguard class * {*;} -keep class * {@cn. Trinea.android.common.annotation.NotProguard <fields>;} -keepclassmembers class * {@cn. Trinea.android.common.annotation.NotProguard <methods>;}
Indicates that classes, properties, and methods that are notproguard decorated are not confused. 3. Use Notproguard to make the specified domain non-confusing: (1) The entire class is not confused
@NotProguard public class User {}
(2) Single attribute is not confused
@NotProguard public int id;
(3) Single method does not confuse
@NotProguardpublic Boolean isValid () {...}
This solves the problem that each class needs to be configured in the Proguard configuration file.
* About Confusion *
Confusion is generally effective in Release mode, there are three main functions: (1) Compress, optimize, delete code, (2) to some extent, improve the difficulty of reading after the anti-compilation, (3) by removing the code function to achieve the special role. For example, in the Proguard syntax and common PROGUARD.CFG code introduced in the use of Progurad so that the online version does not print LOG.D and log.v skills, to prevent debugging sensitive information is leaked. Some of the application code may never be confused, although the actual value of the code may not be small, itself may also be open source code together, and the cracked still can be cracked, but the spirit of doing things to professional attitude, or confused it. About Proguard Basic can be consulted: Proguard grammar and common proguard.cfg code Proguard function, use and BUG analysis
Android code confusing part of the class without confusing tricks