Code obfuscation and reflection conflict (a small solution) ~!

Source: Internet
Author: User
Tags reflection

conflict between code obfuscation and reflection

anti -compiling Android code, most people know that the anti-compiler method is also largely known (although it may not always be valid, but at least like my level of people, no way to see the confused Java code, the most hateful is no comment = =).

Only recently discovered that my code has been confusing for a long time, and I didn't know when to accidentally confuse the code for a long time (OK, you can decompile my project, I don't mind, because the code sucks, it sucks).

So the two are confusing code again, and the way to confuse the code is simple:
1. Create a new file under the project named "proguard.cfg", which normally includes the following:

-optimizationpasses 5

-dontusemixedcaseclassnames

-dontskipnonpubliclibraryclasses

-dontpreverify

-verbose

-optimizations!code/simplification/arithmetic,!field/*,!class/merging/*

-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

-keep public class Com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {

Native <methods>;

}

-keepclasseswithmembers class * {

Public <init> (Android.content.Context, Android.util.AttributeSet);

}

-keepclasseswithmembers class * {

Public <init> (Android.content.Context, android.util.AttributeSet, int);

}

-keepclassmembers enum * {

public static **[] values ();

public static * * VALUEOF (java.lang.String);

}

-keep class * Implements Android.os.Parcelable {

public static final Android.os.parcelable$creator *; }



2.Then edit the "project.properties" file under the item and at the end add a sentence:proguard.config=proguard.cfg

OK, done, remember, only when the code will be confused, directly run the project and go to the bin directory to find the APK bag is not confusing.


itSimple things, of course, will not come here to share the ~
I want to talk about the problem I have, that is, when the code is confused with Java reflection, a lot of trouble comes.


============== don't fall asleep, the real problem comes ============

Yes, we will find that Java reflection can no longer find the class, or method, or attribute it is looking for.
Yes, because the code is confusing reason, the original class name, method name, property names changed, and reflect it or according to the original name to reflect, the result can only shoot a program crashes.

SolutionCertainly the method has ~
That is, if the code or class that is related to reflection is not confused, then there is no such problem.

againLook at the proguard.cfg file, there are so many code, it is impossible to use, come, just a whole sentence to see:

-keep public class * extends Android.app.Activity



verySimply, the meaning of this sentence is to retain all classes that inherit the activity without confusion. Come on, now extrapolate:
-keep public class Com.mutou.test.HelloWorld

itSentences are simpler, keep HelloWorld, and do not confuse.

Note that all classes must indicate the full package path (nonsense, this file does not have the import statement, of course, to complete the path, otherwise it would be aware of the class where ~).

Without confusion, when the code for our Android project is compiled against the Decompile tool, the code does not look like a heavenly book, not all the variable names, the method names, the A, B, C, D, and so on.

wasWatch out, there is a more tangled place, must be careful, through the above two ways to prevent confusion of the code, does not mean that the decompile and the source is exactly the same, it's class name and anything like the source code, but the variable name, method name is not necessarily, it may be ABCD or something. I don't know about that either. Anyway, be careful.

Also because of this, in the reflection of the code (NA, don't ask me what is called reflection Ah, I just want to use the time Baidu a bit, now have forgotten the specific usage), can not be used to prevent code confusion in the above two ways.



============== Real theme under the not gorgeous split line ============
that, there's no way.
There is ah ~ Wooden head, there is no way I will not post the ~ ~

Xi hee, look at:
-keepclasseswithmembernames class com.kogame.god.thing.creature.**
{
*;
}

BadCake ~ This is a little complicated, don't worry ~
First look at the name: Keepclasseswithmembernames, as the name suggests, is the retention class and its members ' names.
That curly braces inside that "*;" What is it again? It represents all the members of the class (including variables, methods) of the name will not be confused, all retain the original appearance.

So, the whole code means to keep all the code that inherits the creature class and its variables and methods, without confusing it.

Good La ~ ~
Thank you for watching.

If you do not understand, please go (simpler and more detailed and less complex articles):
Http://www.apkbus.com/android-57338-1-1.html

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.