Android APK adds code obfuscation for Programs

Source: Internet
Author: User

In SDK 2.3, we can see that a proguard folder is added under android_sdk _ path/tools/. Google places proguard Technology in the android SDK, it can be implemented through normal CompilationCodeConfused.

After Version 2.3, no matter which SDK version is used for development, two files, default. properties and proguard. cfg, will be created in a new project. Http://www.my400800.cn)

 

Download proguard4.6.zip

 

The default. properties code is as follows:

 
# This file is automatically generated by Android tools. # do not modify this file -- your changes will be erased! # This file must be checked in version control systems. # to customize properties used by the ant build system use, # "build. properties ", and override values to adapt the script to your # project structure. # project target.tar get = Android-9

By default, the proguard. cfg script also has obfuscation code:

-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 COM. android. vending. licensing. ilicensingservice-keepclasseswithmembernames class * {native <Methods >;}-keepclasseswithmembernames class * {Public <init> (Android. content. context, android. util. attributeset);}-keepclasseswithmembernames 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 *;}

From the script, you can see:

    • Obfuscation retains the basic components inherited from activity, service, application, broadcastreceiver, contentprovider, and COM. Android. Vending. Licensing. ilicensingservice.
    • All native variable names and class names are retained, and constructors and enumerations with fixed parameter formats are set for all classes.

For more information, see examples and comments in <proguard_path>/examples.

Use code obfuscation

According to the official documentation:

 
To enable proguard so that it runs as part of an ant or eclipse build, set the proguard. config property in the <project_root>/default. properties file. the path can be an absolute path or a path relative to the project's root.

Modify the default. properties file and add the following sentence:

 
Proguard. Config = proguard. cfg

The following figure shows the result after adding:

Proguard. Config = proguard. cfg # This file is automatically generated by Android tools. # do not modify this file -- your changes will be erased! # This file must be checked in version control systems. # to customize properties used by the ant build system use, # "build. properties ", and override values to adapt the script to your # project structure. # project target.tar get = android-9proguard.config = proguard. cfg

Then, the signature can be compiled to generate an APK that is obfuscated by code.

 

 

If a warning occurs during compilation, you can add-ignorewarnings in the proguard. cfg file to ignore the warning item for obfuscation.

 

 

Eclipse + ADT for Android applicationsProgramCode obfuscation and signatureIllustration Start the code obfuscation function

In the later versions of Android tools and ADT, the project contains proguard. the CFG code obfuscated the configuration file, but this configuration is not started by default. add the specified configuration file in properties:

# Project target.tar get = Android-3 Proguard. Config = proguard. cfg Then press F5 to refresh the current project. At this time, eclipse detects file changes and re-compiles! Generate a signature and publish the APK 1. Right-click the project in the Eclipse project. In the pop-up window, select Android tool-generate a signature application package:

2. Select the android project to be packaged (note: the current project is automatically selected here ):

3. If the private key file already exists, select the private key file and enter the password. If there is no private key file, see steps 6th and 7 to create the private key file:

4. Enter the private key alias and password:

5. Select the location of the APK storage and complete the settings to start generating:

6. if there is no private key file, create a private key file (Note: it is best to select a new location for the location of the private key file to keep it in mind, in addition, it is best to back up the private key file to other places to avoid loss, because the application needs to update the same private key file ):

7. Enter the information required for the private key file and create it (Note: The password here is used for the key alias, which is different from the keystore File above. For details, see Steps 3 and 4. In addition, the following names and developer information do not need to be fully filled in. The dialog will prompt you ):

The generated APK is smaller than the debug version! If you find that the project has not become smaller, make sure that the project is re-compiled! However, code obfuscation is generally effective. Basically, you can see the original statement!

 

 

If an error occurs when running the above Code, download proguard4.6.rar and decompress it. copy the files in the lib directory under the directory to the lib directory under the developer directory of androidsdk]

 

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.