am e3d92a20: am 983dd1a9: am b569ec3c: am d76aa14c: am d5885842: Merge "docs: proguard help update" into lmp-docs
* commit 'e3d92a205b5c1bfde3ceff1fe4ca9a62cc5cb4fc': docs: proguard help update
This commit is contained in:
@@ -59,8 +59,7 @@ android {
|
|||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
minifyEnabled true
|
minifyEnabled true
|
||||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), \
|
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||||
'proguard-rules.txt'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,8 +186,7 @@ android {
|
|||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
minifyEnabled true
|
minifyEnabled true
|
||||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), \
|
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||||
'proguard-rules.txt'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,7 +195,7 @@ android {
|
|||||||
|
|
||||||
<p><code>getDefaultProguardFile('proguard-android.txt')</code> obtains the default ProGuard
|
<p><code>getDefaultProguardFile('proguard-android.txt')</code> obtains the default ProGuard
|
||||||
settings from the Android SDK installation. Android Studio adds the module-specific rules file
|
settings from the Android SDK installation. Android Studio adds the module-specific rules file
|
||||||
<code>proguard-rules.txt</code> at the root of the module, where you can add custom ProGuard
|
<code>proguard-rules.pro</code> at the root of the module, where you can add custom ProGuard
|
||||||
rules.</p>
|
rules.</p>
|
||||||
|
|
||||||
<h3 id="configureSigning">Configure signing settings</h3>
|
<h3 id="configureSigning">Configure signing settings</h3>
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ parent.link=index.html
|
|||||||
<h2>In this document</h2>
|
<h2>In this document</h2>
|
||||||
|
|
||||||
<ol>
|
<ol>
|
||||||
<li><a href="#enabling">Enabling ProGuard</a></li>
|
<li><a href="#enabling-gradle">Enabling ProGuard (Gradle Builds)</a></li>
|
||||||
|
|
||||||
|
<li><a href="#enabling">Enabling ProGuard (Ant Builds)</a></li>
|
||||||
|
|
||||||
<li><a href="#configuring">Configuring ProGuard</a></li>
|
<li><a href="#configuring">Configuring ProGuard</a></li>
|
||||||
|
|
||||||
@@ -37,7 +39,10 @@ parent.link=index.html
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and
|
|
||||||
|
|
||||||
|
<p>The <a href="http://proguard.sourceforge.net">ProGuard</a> tool shrinks, optimizes, and
|
||||||
|
obfuscates your code by removing unused code and
|
||||||
renaming classes, fields, and methods with semantically obscure names. The result is a smaller
|
renaming classes, fields, and methods with semantically obscure names. The result is a smaller
|
||||||
sized <code>.apk</code> file that is more difficult to reverse engineer. Because ProGuard makes your
|
sized <code>.apk</code> file that is more difficult to reverse engineer. Because ProGuard makes your
|
||||||
application harder to reverse engineer, it is important that you use it
|
application harder to reverse engineer, it is important that you use it
|
||||||
@@ -52,37 +57,95 @@ parent.link=index.html
|
|||||||
<p>This document describes how to enable and configure ProGuard as well as use the
|
<p>This document describes how to enable and configure ProGuard as well as use the
|
||||||
<code>retrace</code> tool to decode obfuscated stack traces.</p>
|
<code>retrace</code> tool to decode obfuscated stack traces.</p>
|
||||||
|
|
||||||
<h2 id="enabling">Enabling ProGuard</h2>
|
|
||||||
|
<h2 id="enabling-gradle">Enabling ProGuard (Gradle Builds)</h2>
|
||||||
|
<p>When you create a project in Android Studio or with the Gradle build system, the
|
||||||
|
<code>minifyEnabled</code> property in the <code>build.gradle</code> file enables and disables
|
||||||
|
ProGuard for release builds. The <code>minifyEnabled</code> property is part of the
|
||||||
|
<code>buildTypes</code> <code>release</code> block that controls the settings applied to
|
||||||
|
release builds. Set the <code>minifyEnabled</code> property to <code>true</code> to enable
|
||||||
|
ProGuard, as shown in this example. </p>
|
||||||
|
|
||||||
|
<pre class="no-pretty-print">
|
||||||
|
android {
|
||||||
|
...
|
||||||
|
|
||||||
|
buildTypes {
|
||||||
|
release {
|
||||||
|
minifyEnabled true
|
||||||
|
proguardFiles getDefaultProguardFile('proguard-android.txt'),
|
||||||
|
'proguard-rules.pro'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>The <code>getDefaultProguardFile('proguard-android.txt')</code> method obtains the default
|
||||||
|
ProGuard settings from the Android SDK <code>tools/proguard/</code> folder. The
|
||||||
|
<code>proguard-android-optimize.txt</code> file is also available in this Android SDK
|
||||||
|
folder with the same rules but with optimizations enabled. ProGuard optimizations perform
|
||||||
|
analysis at the bytecode level, inside and across methods to help make your app smaller and run
|
||||||
|
faster. Android Studio adds the <code>proguard-rules.pro</code> file at the root of the module,
|
||||||
|
so you can also easily add custom ProGuard rules specific to the current module. </p>
|
||||||
|
|
||||||
|
<p>You can also add ProGuard files to the <code>getDefaultProguardFile</code>
|
||||||
|
directive for all release builds or as part of the <code>productFlavor</code> settings in the
|
||||||
|
<code>build.gradle</code> file to customize the settings applied to build variants. This example
|
||||||
|
adds the <code>proguard-rules-new.pro</code> to the <code>proguardFiles</code>
|
||||||
|
directive and the <code>other-rules.pro</code> file to the <code>flavor2</code> product flavor. </p>
|
||||||
|
|
||||||
|
<pre class="no-pretty-print">
|
||||||
|
android {
|
||||||
|
...
|
||||||
|
|
||||||
|
buildTypes {
|
||||||
|
release {
|
||||||
|
minifyEnabled true
|
||||||
|
proguardFiles getDefaultProguardFile('proguard-android.txt'),
|
||||||
|
'proguard-rules.pro', 'proguard-rules-new.pro'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
productFlavors {
|
||||||
|
flavor1 {
|
||||||
|
}
|
||||||
|
flavor2 {
|
||||||
|
proguardFile 'other-rules.pro'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<h2 id="enabling">Enabling ProGuard (Ant Builds)</h2>
|
||||||
|
|
||||||
<p>When you create an Android project, a <code>proguard.cfg</code> file is automatically
|
<p>When you create an Android project, a <code>proguard.cfg</code> file is automatically
|
||||||
generated in the root directory of the project. This file defines how ProGuard optimizes and
|
generated in the root directory of the project. This file defines how ProGuard optimizes and
|
||||||
obfuscates your code, so it is very important that you understand how to customize it for your
|
obfuscates your code, so it is very important that you understand how to customize it for your
|
||||||
needs. The default configuration file only covers general cases, so you most likely have to edit
|
needs. The default configuration file only covers general cases, so you most likely have to edit
|
||||||
it for your own needs. See the following section about <a href="#configuring">Configuring ProGuard</a> for information on
|
it for your own needs. See the following section about <a href="#configuring">Configuring
|
||||||
customizing the ProGuard configuration file.</p>
|
ProGuard</a> for information on customizing the ProGuard configuration file.</p>
|
||||||
|
|
||||||
<p>To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the
|
<p>To enable ProGuard so that it runs as part of an Ant or Eclipse build, set the
|
||||||
<code>proguard.config</code> property in the <code><project_root>/project.properties</code>
|
<code>proguard.config</code> property in the <code><project_root>/project.properties</code>
|
||||||
file. The path can be an absolute path or a path relative to the project's root.</p>
|
file. The path can be an absolute path or a path relative to the project's root.</p>
|
||||||
|
|
||||||
<p class="note"><strong>Note:</strong> When using Android Studio, you must add Proguard
|
<p>If you left the <code>proguard.cfg</code> file in its default location (the project's root
|
||||||
to your <code>gradle.build</code> file's build types. For more information, see the
|
directory), you can specify its location like this:</p>
|
||||||
<a href="http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard"
|
|
||||||
>Gradle Plugin User Guide</a>.
|
|
||||||
|
|
||||||
<p>If you left the <code>proguard.cfg</code> file in its default location (the project's root directory),
|
|
||||||
you can specify its location like this:</p>
|
|
||||||
<pre class="no-pretty-print">
|
<pre class="no-pretty-print">
|
||||||
proguard.config=proguard.cfg
|
proguard.config=proguard.cfg
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
You can also move the the file to anywhere you want, and specify the absolute path to it:
|
You can also move the the file to anywhere you want, and specify the absolute path to it:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre class="no-pretty-print">
|
<pre class="no-pretty-print">
|
||||||
proguard.config=/path/to/proguard.cfg
|
proguard.config=/path/to/proguard.cfg
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
<p>When you build your application in release mode, either by running <code>ant release</code> or
|
<p>When you build your application in release mode, either by running <code>ant release</code> or
|
||||||
by using the <em>Export Wizard</em> in Eclipse, the build system automatically checks to see if
|
by using the <em>Export Wizard</em> in Eclipse, the build system automatically checks to see if
|
||||||
the <code>proguard.config</code> property is set. If it is, ProGuard automatically processes
|
the <code>proguard.config</code> property is set. If it is, ProGuard automatically processes
|
||||||
@@ -126,9 +189,9 @@ proguard.config=/path/to/proguard.cfg
|
|||||||
|
|
||||||
<h2 id="configuring">Configuring ProGuard</h2>
|
<h2 id="configuring">Configuring ProGuard</h2>
|
||||||
|
|
||||||
<p>For some situations, the default configurations in the <code>proguard.cfg</code> file will
|
<p>For some situations, the default configurations in the ProGuard configuration file will
|
||||||
suffice. However, many situations are hard for ProGuard to analyze correctly and it might remove code
|
suffice. However, many situations are hard for ProGuard to analyze correctly and it might remove
|
||||||
that it thinks is not used, but your application actually needs. Some examples include:</p>
|
code that it thinks is not used, but your application actually needs. Some examples include:</p>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>a class that is referenced only in the <code>AndroidManifest.xml</code> file</li>
|
<li>a class that is referenced only in the <code>AndroidManifest.xml</code> file</li>
|
||||||
@@ -138,12 +201,12 @@ proguard.config=/path/to/proguard.cfg
|
|||||||
<li>dynamically referenced fields and methods</li>
|
<li>dynamically referenced fields and methods</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p>The default <code>proguard.cfg</code> file tries to cover general cases, but you might
|
<p>The default ProGuard configuration file tries to cover general cases, but you might
|
||||||
encounter exceptions such as <code>ClassNotFoundException</code>, which happens when ProGuard
|
encounter exceptions such as <code>ClassNotFoundException</code>, which happens when ProGuard
|
||||||
strips away an entire class that your application calls.</p>
|
strips away an entire class that your application calls.</p>
|
||||||
|
|
||||||
<p>You can fix errors when ProGuard strips away your code by adding a <code>-keep</code> line in
|
<p>You can fix errors when ProGuard strips away your code by adding a <code>-keep</code> line in
|
||||||
the <code>proguard.cfg</code> file. For example:</p>
|
the ProGuard configuration file. For example:</p>
|
||||||
<pre>
|
<pre>
|
||||||
-keep public class <MyClass>
|
-keep public class <MyClass>
|
||||||
</pre>
|
</pre>
|
||||||
@@ -162,12 +225,12 @@ proguard.config=/path/to/proguard.cfg
|
|||||||
|
|
||||||
<p>When your obfuscated code outputs a stack trace, the method names are obfuscated, which makes
|
<p>When your obfuscated code outputs a stack trace, the method names are obfuscated, which makes
|
||||||
debugging hard, if not impossible. Fortunately, whenever ProGuard runs, it outputs a
|
debugging hard, if not impossible. Fortunately, whenever ProGuard runs, it outputs a
|
||||||
<code><project_root>/bin/proguard/mapping.txt</code> file, which shows you the original
|
<code>mapping.txt</code> file, which shows you the original class, method, and field names
|
||||||
class, method, and field names mapped to their obfuscated names.</p>
|
mapped to their obfuscated names.</p>
|
||||||
|
|
||||||
<p>The <code>retrace.bat</code> script on Windows or the <code>retrace.sh</code> script on Linux
|
<p>The <code>retrace.bat</code> script on Windows or the <code>retrace.sh</code> script on Linux
|
||||||
or Mac OS X can convert an obfuscated stack trace to a readable one. It is located in the
|
or Mac OS X can convert an obfuscated stack trace to a readable one. It is located
|
||||||
<code><sdk_root>/tools/proguard/</code> directory. The syntax for executing the
|
in the <code><sdk_root>/tools/proguard/</code> directory. The syntax for executing the
|
||||||
<code>retrace</code> tool is:</p>
|
<code>retrace</code> tool is:</p>
|
||||||
<pre>retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]</pre>
|
<pre>retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]</pre>
|
||||||
<p>For example:</p>
|
<p>For example:</p>
|
||||||
@@ -183,7 +246,9 @@ proguard.config=/path/to/proguard.cfg
|
|||||||
By retaining a copy of the <code>mapping.txt</code> file for each release build,
|
By retaining a copy of the <code>mapping.txt</code> file for each release build,
|
||||||
you ensure that you can debug a problem if a user encounters a bug and submits an obfuscated stack trace.
|
you ensure that you can debug a problem if a user encounters a bug and submits an obfuscated stack trace.
|
||||||
A project's <code>mapping.txt</code> file is overwritten every time you do a release build, so you must be
|
A project's <code>mapping.txt</code> file is overwritten every time you do a release build, so you must be
|
||||||
careful about saving the versions that you need.</p>
|
careful about saving the versions that you need. For Eclipse, this file is stored in
|
||||||
|
<code><project_root>/bin/proguard/</code>. For Android Studio, this file is stored in
|
||||||
|
the app <code>build/outs/</code> folder. </p>
|
||||||
|
|
||||||
<p>For example, say you publish an application and continue developing new features of
|
<p>For example, say you publish an application and continue developing new features of
|
||||||
the application for a new version. You then do a release build using ProGuard soon after. The
|
the application for a new version. You then do a release build using ProGuard soon after. The
|
||||||
@@ -193,6 +258,6 @@ proguard.config=/path/to/proguard.cfg
|
|||||||
on the user's device is gone. There are other situations where your <code>mapping.txt</code> file can be overwritten, so
|
on the user's device is gone. There are other situations where your <code>mapping.txt</code> file can be overwritten, so
|
||||||
ensure that you save a copy for every release that you anticipate you have to debug.</p>
|
ensure that you save a copy for every release that you anticipate you have to debug.</p>
|
||||||
|
|
||||||
<p>How you save the <code>mapping.txt</code> file is your decision. For example, you can rename them to
|
<p>How you save the <code>mapping.txt</code> files is your decision. For example, you can rename
|
||||||
include a version or build number, or you can version control them along with your source
|
the files to include a version or build number, or you can version control them along with your
|
||||||
code.</p>
|
source code.</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user