How to deal with constants in Android code obfuscation

Source: Internet
Author: User

recently has been studying code obfuscation, which found a problem, we define some common constants after the code is confused, after the anti-compilation, even after the full display in front of my eyes, such as the service request address, unexpectedly in the form of a full path is displayed in the confusion of the code, more than their definition of the complete, If so, does code obfuscation make sense? At that time very depressed, but another thought, constants must not be confused, if the constant is confused, 1 hybrid 2, other places to quote will inevitably error ah, then how to do? Want to confuse, and don't want to make the more important constant blatant display? There must be some way, but you may not want to think. We can compare the original project with the extracted project structure after the confusion, you know what to do:

See the difference, right, the difference is in the values folder, so what can you think of? We can define the relevant constants in the String.xml resource file under the Values folder, which can be referenced in the code with GetString (R.STRING.XXX), so that there is no more constant content in the plaintext form in the post-compilation code.

There may be some children's shoes do not understand exactly what to do, now bloggers say the approximate method:

We typically define a common class in the project, such as: Const.java, which contains a number of frequently used constants (including service addresses) such as:

public static final String server_ip = "192.168.0.1";

This kind of writing, after the anti-compilation is very easy to see, according to my method, it should be this:

public static String server_ip = "";

Then define a value for SERVER_IP in String.xml:

<string name= "Server_ip" >192.168.8.2</string>

The constant CONST.SERVER_IP is then assigned in the OnCreate method of the application in its own project:

Const.server_ip=getstring (R.STRING.SERVER_IP);


In this way, the anti-compiled code will not see your service address clear text, if you have other better ways, you can exchange learning together.

About the method of anti-compilation, has been explained in the previous article, interested can be studied under.




How to deal with constants in Android code obfuscation

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.