Android – Obfuscated code

androidobfuscationproguard

I was asked to crate a simple app for android. The first one in fact that I'll be paid for, so I really don't want to screw it up :). One of the requirements was that the code must be obfuscated.

I learned the general idea of obfuscating, but I don't want to make any silly mistakes.

What precisely do I have to do to make the code obfuscated? Does exporting it as release do the job, or some other steps are required? Any remarks are also appreciated.

PS. I'm using Eclipse if it matters.

EDIT

From the article suggested in the anwsers:

To enable ProGuard so that it runs as part of an Ant or Eclipse build,
set the proguard.config property in the
/project.properties file. The path can be an absolute
path or a path relative to the project's root.

If you left the proguard.cfg file in its default location (the
project's root directory), you can specify its location like this:

proguard.config=proguard.cfg

I indeed have the project.properties file in my project's dir. But I don't have the proguard.cfg file. Instead I have the proguard-project.txt file. I guess it's a replacement.

project.properties:

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 edit
"ant.properties", and override values to adapt the script to your
project structure.

To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):

proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

Project target.
target=android-7

Originally everything but the last line is commented out.

In proguard-project.txt everything is commented out.

I guess I'm lost here, so I'd be very thankful If somebody could tell me step by step what I am supposed to do and also how to check if it actually works.

Best Answer

The article on the official dev site is out of date. Starting with SDK Tools r17 they changed the way the ProGuard configuration works. Basically they split what used to be proguard.cfg into two files (and also simply made them .txt files): proguard-android.txt and proguard-project.txt. proguard-android.txt resides in your {SDK dir}\tools\proguard directory and proguard-project.txt is in each of your project directories.

They split the file so that general configuration/flags that the Android team deems pretty standard will be in the proguard-android.txt file and can be updated (basically silently) with each SDK Tools revision. And then any developer/project specific flags, the developer can add to the proguard-project.txt file.

This is why you noticed everything in your proguard-project.txt file was commented out, because in general most people don't have any project specific needs, they just want to run ProGuard with standard settings and be done with it.

For reference (a very good explanation): http://tools.android.com/recent/proguardimprovements

Step-by-Step (day by day :P)

(Using Eclipse IDE) To enable ProGuard and it's shrinking and obfuscation using only the default settings:

  • First update your SDK Tools to newest (r19 at time of writing)
  • In project.properties uncomment the line that says:

proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

If you created the project before r17 then you might not see the same things. If that's the case I suggest you create a new dummy project (after updating SDK Tools) and just copy over the proguard-project.txt and project.properties files.

That's it! ProGuard will now be enabled and it will run, but only on release builds (when you use Android Tools and export a signed/unsigned apk).

When you make a release build you'll notice that a new ProGuard folder will be created in your project folder. Within this folder you find four files with stuff like what classes/methods were kept or removed. Also note that when you obfuscate, you'll also obfuscate your stacktraces in the event of errors.

Edit: (9/4/12) Just wanted to update this answer to reflect changes in Tools r20.

The Android team has now added one additional proguard configuration file. It is proguard-android-optimize.txt and it also resides in the {SDK dir}\tools\proguard directory. This file is the same as the original proguard-android.txt config file, except it has proguard optimization turned on.

This additional file was added for convenience for those who wanted to turn on optimizations (maybe so they can do stuff like strip out Log calls, which requires optimizations be turned on). Now you can simply point to this file instead in your project.properties file like so:

proguard.config=${sdk.dir}\tools\proguard\proguard-android-optimize.txt:proguard-project.txt

As before, don't try to alter any of the proguard-android... config files. Any project specific flags should go in the proguard-project.txt file in your project folder.

Related Topic