if (Config.LOGGING) print(“The data is: “ + data);
The above statement is completely removed when Config.LOGGING is false.
textView.setTextColor(Color.RED, 10, 20);
in order to set the 10th to the 20th characters red. I'll show you a method of making this easy to do; not just with colors, but with all sorts of styles.<b>
, <i>
, and <u>
for bold, italics, and underlining, respectively. For example,TextView
, along with many other Android classes which use formatted text, don't just use simple Strings
. They use CharSequences
. Get this: a CharSequence
is more abstract than a String
; a String
is a sub-class of CharSequence
. A CharSequence
defines a series of characters, such as a regular string, but it could also define a series of characters with formatting, such as a SpannableString
. Internally, what we will do to change the middle of a TextView
's text is to add spans to its text. More precisely, we will add CharacterStyles
to the TextView's CharSequence
(text), which is a SpannableString
."##"
. The tokens will be removed in the returned result.TextView
has its text set as "Hello, ##world##!"
and you want world
to be in red, call setSpanBetweenTokens("Hello ##world##!", "##", new ForegroundColorSpan(0xFFFF0000));
will return the text Hello, world!
with world
in red. Note that you can send multiple spans as parameters to this method.MovementMethod
to the TextView
. You can investigate this further if you wish, or you can just see the sample code to make it work below (and it is also in the sample):TextViews
and a Button
. The first TextView
shows how you can set a TextView
's text just by using HTML tags in strings.xml
. The second TextView
demonstrates how to change text dynamically, using the above utility method. When the button is clicked, it will first set some text red using a ForegroundColorSpan
. Second, it will set some text bold and italics using a StyleSpan
. Third, it will make a generic link by setting some text to blue, underlining it (UnderlineSpan
), and then creating an onClick
method which executes some custom code using a ClickableSpan
. The final click demonstrates both a ForegroundColorSpan
and a RelativeSizeSpan
.if (Config.LOGGING) { TestClass test = new TestClass(); Log.d(TAG, "[onCreate] testClass=" + test); }
Config.LOGGING
to false
, so it doesn't execute. The problem is, this code is still in your application. It makes it bigger, and may cause potential security issues by including code which should never be seen by a snooping hacker. x * 2
with x << 1
. The positive effects of these optimizations will depend on your code and on the virtual machine on which the code is executed. Simple virtual machines may benefit more than advanced virtual machines with sophisticated JIT compilers. At the very least, your bytecode may become a bit smaller. .java
files) into Java bytecode (i.e. .class
files). Then, a tool in the Android SDK turns the Java bytecode into Dalvik bytecode (i.e. .dex
files). Finally, all of the resources and code are packaged into a single ZIP file, which is an .APK
file. Since ProGuard works with Java bytecode, we want to run ProGuard on the class files that are created by the Java compiler, before the build process converts the Java bytecode into Dalvik bytecode. This isn't possible with the regular Eclipse method of creating Android packages (at least, not that I know of), but it's a cinch if you use Ant to build your application. It doesn't take long to create an Ant build script to build your existing Android application. See the instructions on my blog post here. Or, you can just download the sample at the end of this blog. proguard/
. For example, in the latest version as of this writing (4.5.1 distribution), I copied lib/proguard.jar
from the distribution ZIP file into my source tree as proguard/proguard.jar
. Now, we add the script to the Ant build file, build.xml
. <!-- ================================================= -->
<!-- Obfuscation with ProGuard -->
<!-- ================================================= -->
<property name="proguard-dir" value="proguard"/>
<property name="unoptimized" value="${proguard-dir}/unoptimized.jar"/>
<property name="optimized" value="${proguard-dir}/optimized.jar"/>
<target name="optimize" unless="nooptimize">
<jar basedir="${out.classes.dir}" destfile="${unoptimized}"/>
<java jar="${proguard-dir}/proguard.jar" fork="true" failonerror="true">
<jvmarg value="-Dmaximum.inlined.code.length=16"/>
<arg value="@${proguard-dir}/config.txt"/>
<arg value="-injars ${unoptimized}"/>
<arg value="-outjars ${optimized}"/>
<arg value="-libraryjars ${android.jar}"/>
</java>
<!-- Delete source pre-optimized jar -->
<!--delete file="${unoptimized}"/-->
<!-- Unzip target optimization jar to original output, and delete optimized.jar -->
<delete dir="${out.classes.dir}"/>
<mkdir dir="${out.classes.dir}"/>
<unzip src="${proguard-dir}/optimized.jar" dest="${out.classes.dir}"/>
<!-- Delete optimized jar (now unzipped into bin directory) -->
<delete file="optimized.jar"/>
</target>
optimize
Ant target between the Java compiler and dex compiler, we change the dex target as so:<!-- Converts this project's .class files into .dex files -->
<target name="-dex" depends="compile,optimize">
-post-compile
target, and add this:<target name="-post-compile">
<antcall target="optimize"/>
</target>
proguard/config.txt
, which is referenced in the above Ant script. The following is taken from the ProGuard manual, although -libraryjars
, -injars
, and -outjars
is passed in via the Ant build script instead of here. -target 1.6
-optimizationpasses 2
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
# The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle.
-optimizations !code/simplification/arithmetic
-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 * extends View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
# Also keep - Enumerations. Keep the special static
# methods that are required in enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-verbose
and -printusage unused.txt
. You may remove these if you don't like the extra output cluttering your build process. ant release
from the command line, you will see the optimizer run. Here is the output from the test project, included below, when the build property config.logging
is true
: >ant release
...
[java] Shrinking...
[java] Printing usage to [blog\obfuscation\proguard\unused.txt]...
[java] Removing unused program classes and class elements...
[java] Original number of program classes: 8
[java] Final number of program classes: 2
-printusage unused.txt
, we can see what was removed from our code: com.androidengineer.obfu.Obfuscation:
private static final java.lang.String TAG
com.androidengineer.obfu.R
com.androidengineer.obfu.R$attr
com.androidengineer.obfu.R$drawable
com.androidengineer.obfu.R$layout
com.androidengineer.obfu.R$string
com.androidengineer.obfu.TestClass:
private static final java.lang.String TAG
proguard/unoptimized.jar
and proguard/optimized.jar
. We can see that it removed many classes which were just placeholders for constants, and it removed the string TAG
variables used by our logging code by replacing the references with the actual string constants. config.logging
to false
, we get an even further reduction in size. The best part about it is, it removes all of our debugging code.>ant release
...
[java] Original number of program classes: 8
[java] Final number of program classes: 1
TestClass
. Because it is only used when Config.LOGGING
is true
, it is completely removed from the final build during the obfuscation process. So feel free to leave all of the debugging code you want in your source, because it can be removed during the build. proguard/unoptimized.jar
is 4,959 bytes and proguard/optimized.jar
is 646 bytes. But on an application I work with, which has over a thousand classes, I've seen a literal 50% reduction of code size. Well worth the trouble of setting this build up, in my opinion. ClassNotFoundException
when running your application which has been obfuscated with ProGuard. In this case, you need to edit the config.txt
file to tell ProGuard to keep the class in question. For example, # Keep classes which are not directly referenced by code, but referenced by layout files.
-keep,allowshrinking class com.androidengineer.MyClass
{
*** (...);
}
View
class in an Android layout file, such as MyButton extends Button
, but the class is not referenced in regular code. More information can be found in the ProGuard documentation. $[android-jar}
, to ${android.jar}
. This caused the builds to break. The solution is to define them both if they do not exist: <!-- In newer platforms, the build setup tasks were updated to begin using ${android.jar} instead of ${android-jar}. This makes them both compatible. -->
<target name="target-new-vars" unless="android-jar">
<property name="android-jar" value="${android.jar}"/>
</target>
<!-- Be sure to call target-new-vars before anything else. -->
<target name="config" depends="target-new-vars,clean">
The sample project file below has been updated.
<!-- extension targets. Uncomment the ones where you want to
do custom work in between standard targets -->
<!--
<target name="-pre-build">
</target>
<target name="-pre-compile">
</target>
[This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override
${out.dex.input.absolute.dir}]
<target name="-post-compile">
</target>
-->
-post-compile
Ant target, and add our obfuscation Ant target to it.<target name="-post-compile">
<antcall target="optimize"/>
</target>
proguard/config.txt
file.-keep class com.android.vending.licensing.ILicensingService
The sample application is a simple Hello World application, but it includes the custom build script and ProGuard library as described in this tutorial. First, you must run "android update project -p .
" from the command line in the project's directory to let the tools set the SDK path in local.properties
. Then you can turn on and off logging by changing the value of config.logging
in build.properties
. Finally, run ant release
to build the application, which will create the obfuscated and signed .apk
file. If you have any trouble, you may want to review the previous blog post about setting up Ant builds.
Project source code - obfuscation.zip (600 Kb)
Build file for Android API level 8 and above:build.xml (4.52 Kb)
build.xml
. Build steps in Ant are called tasks, which are defined by targets in the build file. When you build your Android application with the default build script, you would type ant release
at the command line. In this case, Ant looks for the default filename build.xml
, and release
is the target which it builds. The release
target builds the application ready for release (as opposed to for debugging). Another example would be ant clean
, which cleans the project binaries.android update project --path .
>android update project --path .
Updated local.properties
Added file C:\dev\blog\antbuild\build.xml
android
command is not found, then you need to update your path to include the Android tools. On Windows, you can use something like set path=%PATH%;C:\dev\android-sdk-windows\tools
(substituting your actual Android installation directory), or even better add it to your path persistently by updating the environment variables through your system properties.build.xml
. You can test your setup by typing ant
at the command prompt, and you should receive something similar to the following boilerplate help:ant
command is not found, then you need to update your path. Like above, on Windows use set path=%PATH%;C:\dev\apache-ant-1.8.1\bin
(substituting your actual Ant installation directory), or even better update your environment variables.ant release
at the command prompt, which will build the project, placing the unsigned .apk file inside of the bin/
directory.-release-nosign:
which says to sign the apk manually and to run zipalign. We'll get to these steps later in the signing section below.android create project --name YourProjectName --path C:\dev\YourProject --target android-3 --package com.company.testproject --activity MainActivity
android list target
and you should see something like:id: 1 or "android-3"
Name: Android 1.5
Type: Platform
API level: 3
Revision: 4
Skins: HVGA (default), HVGA-L, HVGA-P, QVGA-L, QVGA-P
1
or android-3
as the target ID. In the sample project, I chose android-4
, which corresponds to Android 1.6.ant
at the command line. See the above section for further instructions.build.xml
, in Eclipse, you will see an error on the second line of the file at this line: <project name="MainActivity" default="help">
. The problem with this line is that it is saying that the default Ant target is "help", but the actual Ant targets used in the build file are imported from another location, which the Eclipse editor does not recognize. The import is done at the line <taskdef name="setup"
, which imports Ant files from the Android SDK.build.xml
is not needed. There are two solutions. You can remove default="help"
from the file, which will remove the error in Eclipse. If you do this, and type ant
at a command prompt without any targets (as opposed to "ant release
"), you won't get the default help. Or, you can copy the imported Ant files directly into your code, which is exactly what you must do if you would like to customize your build. If you follow this tutorial, you won't have to worry about this error. See the Customizing the build section for more information.ant debug
) For actual applications delivered to the Android Marketplace, you need to sign them with a real key. It is useful to put this step into the build process. On top of the ease of automating the process, it allows you to build your application in one step. (One-step builds are a Good IdeaTM)build.properties
in your project's base directory (in the same directory as build.xml
and the other properties files), if it does not already exist. Add the following lines:key.store=keystore
key.alias=www.androidengineer.com
keystore
is the name of your keystore file and change the value of key.alias
to your keystore's alias. Now when you run ant release
, you will be prompted for your passwords, and the build will automatically sign and zipalign your package.build.properties
as well, which will solve the issue:key.store.password=password
key.alias.password=password
build.properties
file, but not the passwords, you can create a separate properties file which could only be allowed on certain machines but not checked in to version control. For example, you could create a secure.properties
file which goes on the build machine, but not checked in to version control so all developers wouldn't have access to it; import the extra properties file by adding <property file="secure.properties" />
to build.xml
. Finally, you could always build the APKs unsigned with ant release
by not adding any information to the properties files. The package built using this method will need to be signed and aligned.ant
on the command line, such as release
, clean
, etc. To customize the build further, we need to copy the imported targets into our own build file.build.xml
, you can see the instructions for how to customize your build steps:The rules file is imported from
<SDK>/platforms/<target_platform>/templates/android_rules.xml
To customize some build steps for your project:
- copy the content of the main node <project> from android_rules.xml
- paste it in this build.xml below the <setup /> task.
- disable the import by changing the setup task below to <setup import="false" />
android_rules.xml
file in your Android SDK. For example, mine is located at C:\dev\android-sdk-windows\platforms\android-4\templates
. There, copy almost the entire file, excluding the project node (copy below <project name="MainActivity">
to above </project>
), and paste it in your build.xml
file. Also, change the line <setup />
to <setup import="false"/>
.LOGGING
flag be set from your build. That way, you can be sure that when you create your release package, all of the code you used for debugging won't be included. For example, you may have debugging log statements like this:Config.LOGGING
flag is in your build properties. Add the following to build.properties
:# Turn on or off logging.
config.logging=true
filterset
with the copy
task. What we can do is create a Java template file which has tokens such as @CONFIG.LOGGING@
and copy it to our source directory, replacing the tokens with whatever the build properties values are. For example, in the sample application I have a file called Config.java
in the config/
directory.config/Config.java
is notthe actual file used when compiling the project. The file src/com/yourpackage/Config.java
, which is the copied file destination, is what will be used as a source file.@CONFIG.LOGGING
with the value of the property config.logging
, which is true
. I will create an Ant target called config
which will copy the above template to the source directory. This will be called before the compile
target. <!-- Copy Config.java to our source tree, replacing custom tokens with
values in build.properties. The configuration depends on "clean" because otherwise the build system will not detect changes in the configuration. --> <target name="config">
<property name="config-target-path" value="${source.dir}/com/androidengineer/antbuild"/>
<!-- Copy the configuration file, replacing tokens in the file. -->
<copy file="config/Config.java" todir="${config-target-path}"
overwrite="true" encoding="utf-8">
<filterset>
<filter token="CONFIG.LOGGING" value="${config.logging}"/>
</filterset>
</copy>
<!-- Now set it to read-only, as we don't want people accidentally
editing the wrong one. NOTE: This step is unnecessary, but I do
it so the developers remember that this is not the original file. -->
<chmod file="${config-target-path}/Config.java" perm="-w"/>
<attrib file="${config-target-path}/Config.java" readonly="true"/>
</target>
compile
target, we simply add config
to the dependency of compile
: <target name="compile" depends="config, -resource-src, -aidl".
We also make the config
target call clean
, because otherwise the build system will not detect changes in the configuration, and may not recompile the proper classes.build.properties
.local.properties
file which is generated by the Android build tools. This is noted in the file itself; it sets paths based on the local machine. Do check in the default.properties
file, which is used by the Android tools, and build.properties
, which is the file which you edit to customize your project's build.Config.java
in the source directory, nor anything else is configured by the build. I don't want local changes to propagate to other developers, so I only check in the original template file in the config/
directory.VERSION_2.0
". That way we are certain of what properties the application was built with, and we can reproduce the application exactly as it was released, if we later need to.android create project
, or android update project
in your project base directory if it already exists.key.store
and key.alias
to build.properties
if you want to include the signing step in your build.key.store.password
and key.alias.password
to build.properties
if you want to include the keystore passwords, to make the build run without any further input needed.<SDK>/platforms/<target_platform>/templates/android_rules.xml
to your local build.xml
and change <setup />
to <setup import="false"/>
.ant release
to build your project. It will create the package in bin/
.Config.java
which is configurable by the build. First, you must run "android update project -p .
" from the command line in the project's directory to let the tools set the SDK path in local.properties
. Then you can turn on and off logging by changing the value of config.logging
in build.properties
. Finally, run ant release
to build the application, which will create the signed bin/MainActivity-release.apk
file ready to be released.