Use ant obfuscation in Android

Source: Internet
Author: User

After several days, I checked hundreds of websites and finally found a simple ant obfuscation compiling method. Start as follows:

1. For a common project, first add the ant compilation function to it.

Android update project -- name project_name-t 3-p D:/temp/project_name

An ant script named build. xml is automatically generated in the project root directory. The project name above is your project name.

 

2. added the obfuscation function

In the first step, ant can be used for compilation in the command line. In Android, the rule file under the Directory D:/android-sdk-windows/tools/ant/main_rules.xml is automatically called when the command ant debug is entered, so you don't need to write your own ant script. Since it will call the default rule file (the default file does not include the obfuscation function), we want to confuse it and simply modify it on this rule file. Add a target after the target named-dex. The Code is as follows:

 <Target name = "optimize" depends = "compile"> <jar basedir = "$ {out. classes. absolute. dir} "destfile =" temp. jar "> <java jar =" D:/android-sdk-windows/tools/proguard/lib/proguard. jar "fork =" true "failonerror =" true "> <jvmarg value ="-Dmaximum. inlined. code. length = 32 "> <! -- Value = "-libraryjars $ {library-jar}/some_lib_used.jar"> --> </arg> </ arg> </! --> </Arg> </jvmarg> </java> <delete file = "temp. jar "> <delete dir =" $ {out. classes. absolute. dir} "> <mkdir dir =" $ {out. classes. absolute. dir} "> <unzip src =" optimized. jar "mce_src =" optimized. jar "dest =" $ {out. classes. absolute. dir} "> <delete file =" optimized. jar "> </delete> </unzip> </mkdir> </delete> </jar> </target>

Change the path involved in the above Code to the local path. <Arg value = "-ignorewarning"/> This line is added by yourself, meaning ignore warnings.

Then perform a obfuscation dependency in the target named-dex. After adding the dependency, the Code is as follows:

<Target name = "-dex" depends = "compile, optimize,-post-compile,-obfuscate" unless = "do. not. compile "> <if condition =" $ {manifest. hasCode} "> <then> <dex-helper> </then> <else> <echo> hasCode = false. skipping... </echo> </else> </if> </target>

In fact, only optimize is added, which means that this target depends on target optimize.

 

3. Start Compilation

It can be compiled in the command line. After compilation, the class. dex is decompiled. It can be seen that the obfuscation is successful. I don't know why the decompilation won't come back in our project, but it can ensure that obfuscation is successful. I have decompiled other examples of projects after compilation. You can see: (sorry, you cannot upload images within one week of registration)

 

Now add:

 

 

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.