• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=ProGuard
2@jd:body
3
4 <div id="qv-wrapper">
5    <div id="qv">
6      <h2>In this document</h2>
7
8      <ol>
9        <li><a href="#enabling">Enabling ProGuard</a></li>
10
11        <li><a href="#configuring">Configuring ProGuard</a></li>
12
13        <li>
14          <a href="#decoding">Decoding Obfuscated Stack Traces</a>
15
16          <ol>
17            <li><a href="#considerations">Debugging considerations for published
18            applications</a></li>
19          </ol>
20        </li>
21      </ol>
22
23      <h2>See also</h2>
24
25      <ol>
26        <li><a href="http://proguard.sourceforge.net/manual/introduction.html">ProGuard
27        Manual</a></li>
28
29        <li><a href="http://proguard.sourceforge.net/manual/retrace/introduction.html">ProGuard
30        ReTrace Manual</a></li>
31      </ol>
32    </div>
33  </div>
34
35  <p>The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and
36  renaming classes, fields, and methods with semantically obscure names. The result is a smaller
37  sized <code>.apk</code> file that is more difficult to reverse engineer. Because ProGuard makes your
38  application harder to reverse engineer, it is important that you use it
39  when your application utilizes features that are sensitive to security like when you are
40  <a href="{@docRoot}guide/publishing/licensing.html">Licensing Your Applications</a>.</p>
41
42  <p>ProGuard is integrated into the Android build system, so you do not have to invoke it
43  manually. ProGuard runs only when you build your application in release mode, so you do not
44  have to deal with obfuscated code when you build your application in debug mode.
45  Having ProGuard run is completely optional, but highly recommended.</p>
46
47  <p>This document describes how to enable and configure ProGuard as well as use the
48  <code>retrace</code> tool to decode obfuscated stack traces.</p>
49
50  <h2 id="enabling">Enabling ProGuard</h2>
51
52  <p>When you create an Android project, a <code>proguard.cfg</code> file is automatically
53  generated in the root directory of the project. This file defines how ProGuard optimizes and
54  obfuscates your code, so it is very important that you understand how to customize it for your
55  needs. The default configuration file only covers general cases, so you most likely have to edit
56  it for your own needs. See the following section about <a href="#configuring">Configuring ProGuard</a> for information on
57  customizing the ProGuard configuration file.</p>
58
59  <p>To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the
60  <code>proguard.config</code> property in the <code>&lt;project_root&gt;/default.properties</code>
61  file. The path can be an absolute path or a path relative to the project's root.</p>
62<p>If you left the <code>proguard.cfg</code> file in its default location (the project's root directory),
63you can specify its location like this:</p>
64<pre class="no-pretty-print">
65proguard.config=proguard.cfg
66</pre>
67<p>
68You can also move the the file to anywhere you want, and specify the absolute path to it:
69</p>
70<pre class="no-pretty-print">
71proguard.config=/path/to/proguard.cfg
72</pre>
73
74
75  <p>When you build your application in release mode, either by running <code>ant release</code> or
76  by using the <em>Export Wizard</em> in Eclipse, the build system automatically checks to see if
77  the <code>proguard.config</code> property is set. If it is, ProGuard automatically processes
78  the application's bytecode before packaging everything into an <code>.apk</code> file. Building in debug mode
79  does not invoke ProGuard, because it makes debugging more cumbersome.</p>
80
81  <p>ProGuard outputs the following files after it runs:</p>
82
83  <dl>
84    <dt><code>dump.txt</code></dt>
85    <dd>Describes the internal structure of all the class files in the <code>.apk</code> file</dd>
86
87    <dt><code>mapping.txt</code></dt>
88    <dd>Lists the mapping between the original and obfuscated class, method, and field names.
89    This file is important when you receive a bug report from a release build, because it
90    translates the obfuscated stack trace back to the original class, method, and member names.
91    See <a href="#decoding">Decoding Obfuscated Stack Traces</a> for more information.</dd>
92
93    <dt><code>seeds.txt</code></dt>
94    <dd>Lists the classes and members that are not obfuscated</dd>
95
96    <dt><code>usage.txt</code></dt>
97    <dd>Lists the code that was stripped from the <code>.apk</code></dd>
98  </ul>
99
100  <p>These files are located in the following directories:</p>
101
102  <ul>
103    <li><code>&lt;project_root&gt;/bin/proguard</code> if you are using Ant.</li>
104
105    <li><code>&lt;project_root&gt;/proguard</code> if you are using Eclipse.</li>
106  </ul>
107
108
109  <p class="caution"><strong>Caution:</strong> Every time you run a build in release mode, these files are
110  overwritten with the latest files generated by ProGuard. Save a copy of them each time you release your
111  application in order to de-obfuscate bug reports from your release builds.
112  For more information on why saving these files is important, see
113  <a href="#considerations">Debugging considerations for published applications</a>.
114  </p>
115
116  <h2 id="configuring">Configuring ProGuard</h2>
117
118  <p>For some situations, the default configurations in the <code>proguard.cfg</code> file will
119  suffice. However, many situations are hard for ProGuard to analyze correctly and it might remove code
120  that it thinks is not used, but your application actually needs. Some examples include:</p>
121
122  <ul>
123    <li>a class that is referenced only in the <code>AndroidManifest.xml</code> file</li>
124
125    <li>a method called from JNI</li>
126
127    <li>dynamically referenced fields and methods</li>
128  </ul>
129
130  <p>The default <code>proguard.cfg</code> file tries to cover general cases, but you might
131  encounter exceptions such as <code>ClassNotFoundException</code>, which happens when ProGuard
132  strips away an entire class that your application calls.</p>
133
134  <p>You can fix errors when ProGuard strips away your code by adding a <code>-keep</code> line in
135  the <code>proguard.cfg</code> file. For example:</p>
136  <pre>
137-keep public class &lt;MyClass&gt;
138</pre>
139
140  <p>There are many options and considerations when using the <code>-keep</code> option, so it is
141  highly recommended that you read the <a href="http://proguard.sourceforge.net/manual/introduction.html">ProGuard
142  Manual</a> for more information about customizing your configuration file. The <a href=
143  "http://proguard.sourceforge.net/manual/usage.html#keepoverview">Overview of Keep options</a> and
144  <a href="http://proguard.sourceforge.net/index.html#/manual/examples.html">Examples section</a>
145  are particularly helpful. The <a href=
146  "http://proguard.sourceforge.net/manual/troubleshooting.html">Troubleshooting</a> section of the
147  ProGuard Manual outlines other common problems you might encounter when your code gets stripped
148  away.</p>
149
150  <h2 id="decoding">Decoding Obfuscated Stack Traces</h2>
151
152  <p>When your obfuscated code outputs a stack trace, the method names are obfuscated, which makes
153  debugging hard, if not impossible. Fortunately, whenever ProGuard runs, it outputs a
154  <code>&lt;project_root&gt;/bin/proguard/mapping.txt</code> file, which shows you the original
155  class, method, and field names mapped to their obfuscated names.</p>
156
157  <p>The <code>retrace.bat</code> script on Windows or the <code>retrace.sh</code> script on Linux
158  or Mac OS X can convert an obfuscated stack trace to a readable one. It is located in the
159  <code>&lt;sdk_root&gt;/tools/proguard/</code> directory. The syntax for executing the
160  <code>retrace</code> tool is:</p>
161  <pre>retrace.bat|retrace.sh [-verbose] mapping.txt [&lt;stacktrace_file&gt;]</pre>
162  <p>For example:</p>
163
164  <pre>retrace.bat -verbose mapping.txt obfuscated_trace.txt</pre>
165
166  <p>If you do not specify a value for <em>&lt;stacktrace_file&gt;</em>, the <code>retrace</code> tool reads
167  from standard input.</p>
168
169  <h3 id="considerations">Debugging considerations for published applications</h3>
170
171  <p>Save the <code>mapping.txt</code> file for every release that you publish to your users.
172  By retaining a copy of the <code>mapping.txt</code> file for each release build,
173  you ensure that you can debug a problem if a user encounters a bug and submits an obfuscated stack trace.
174  A project's <code>mapping.txt</code> file is overwritten every time you do a release build, so you must be
175  careful about saving the versions that you need.</p>
176
177  <p>For example, say you publish an application and continue developing new features of
178  the application for a new version. You then do a release build using ProGuard soon after. The
179  build overwrites the previous <code>mapping.txt</code> file. A user submits a bug report
180  containing a stack trace from the application that is currently published. You no longer have a way
181  of debugging the user's stack trace, because the <code>mapping.txt</code> file associated with the version
182  on the user's device is gone. There are other situations where your <code>mapping.txt</code> file can be overwritten, so
183  ensure that you save a copy for every release that you anticipate you have to debug.</p>
184
185  <p>How you save the <code>mapping.txt</code> file is your decision. For example, you can rename them to
186  include a version or build number, or you can version control them along with your source
187  code.</p>