diff options
author | Xavier Ducrohet <xav@android.com> | 2010-09-16 18:53:44 -0700 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2010-09-24 16:33:38 -0700 |
commit | a66ca8ad997527af0e7a07ba851da0a6d379fad0 (patch) | |
tree | 0827f8d51c3d55ecf4abc0f2b824feafdf211a4f /files | |
parent | 03dec99d1bc5fc418e58235c5e6dd0543b41413e (diff) | |
download | sdk-a66ca8ad997527af0e7a07ba851da0a6d379fad0.zip sdk-a66ca8ad997527af0e7a07ba851da0a6d379fad0.tar.gz sdk-a66ca8ad997527af0e7a07ba851da0a6d379fad0.tar.bz2 |
Use proguard for release builds through Ant.
This is only activated if default.properties contains
a property "proguard.config" with the name of a proguard
config file.
Some clean-up in the Ant tasks and in the name
of the properties used by the rules and the custom
tasks to make them clearer.
Added a new test app with a project using a jar file
as well as a library using a jar file.
Change-Id: Ia8f4d873025993d454c0a484e61d47ae679ea79c
Diffstat (limited to 'files')
-rw-r--r-- | files/ant/main_rules.xml | 100 |
1 files changed, 84 insertions, 16 deletions
diff --git a/files/ant/main_rules.xml b/files/ant/main_rules.xml index 7059f3a..7ce46e4 100644 --- a/files/ant/main_rules.xml +++ b/files/ant/main_rules.xml @@ -60,8 +60,14 @@ <property name="asset.absolute.dir" location="${asset.dir}" /> <!-- Directory for the third party java libraries --> - <property name="external.libs.dir" value="libs" /> - <property name="external.libs.absolute.dir" location="${external.libs.dir}" /> + <property name="jar.libs.dir" value="libs" /> + <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" /> + <!-- create a path with all the jar files, from the main project and the + libraries --> + <path id="jar.libs.ref"> + <fileset dir="${jar.libs.absolute.dir}" includes="*.jar" /> + <path refid="project.libraries.jars" /> + </path> <!-- Directory for the native libraries --> <property name="native.libs.dir" value="libs" /> @@ -72,7 +78,6 @@ <property name="out.absolute.dir" location="${out.dir}" /> <property name="out.classes.dir" value="${out.absolute.dir}/classes" /> <property name="out.classes.absolute.dir" location="${out.classes.dir}" /> - <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" /> <!-- Intermediate files --> <property name="dex.file.name" value="classes.dex" /> @@ -179,8 +184,7 @@ <extra-parameters /> <arg line="${verbose.option}" /> <arg path="${out.dex.input.absolute.dir}" /> - <fileset dir="${external.libs.absolute.dir}" includes="*.jar" /> - <path refid="android.libraries.jars" /> + <path refid="jar.libs.ref" /> <external-libs /> </apply> </sequential> @@ -209,11 +213,11 @@ hascode="${manifest.hasCode}"> <dex path="${intermediate.dex.file}"/> <sourcefolder path="${source.absolute.dir}"/> - <sourcefolder refid="android.libraries.src"/> - <jarfolder path="${external.libs.absolute.dir}" /> - <jarfolder refid="android.libraries.libs" /> + <sourcefolder refid="project.libraries.src"/> + <jarfolder path="${jar.libs.absolute.dir}" /> + <jarfolder refid="project.libraries.libs" /> <nativefolder path="${native.libs.absolute.dir}" /> - <nativefolder refid="android.libraries.libs" /> + <nativefolder refid="project.libraries.libs" /> <extra-jars/> </apkbuilder> </sequential> @@ -256,7 +260,7 @@ <target name="-dirs"> <echo>Creating output directories if needed...</echo> <mkdir dir="${resource.absolute.dir}" /> - <mkdir dir="${external.libs.absolute.dir}" /> + <mkdir dir="${jar.libs.absolute.dir}" /> <mkdir dir="${out.absolute.dir}" /> <if condition="${manifest.hasCode}"> <then> @@ -298,7 +302,7 @@ <aidl executable="${aidl}" framework="${android.aidl}" genFolder="${gen.absolute.dir}"> <source path="${source.absolute.dir}"/> - <source refid="android.libraries.src"/> + <source refid="project.libraries.src"/> </aidl> </then> <else> @@ -325,7 +329,7 @@ </condition> <condition property="extensible.libs.classpath" value="${tested.project.absolute.dir}/libs" - else="./libs"> + else="${jar.libs.dir}"> <isset property="tested.project.absolute.dir" /> </condition> <javac encoding="${java.encoding}" @@ -335,12 +339,11 @@ bootclasspathref="android.target.classpath" verbose="${verbose}" classpath="${extensible.classpath}" - classpathref="android.libraries.jars"> + classpathref="jar.libs.ref"> <src path="${source.absolute.dir}" /> <src path="${gen.absolute.dir}" /> - <src refid="android.libraries.src" /> + <src refid="project.libraries.src" /> <classpath> - <fileset dir="${external.libs.absolute.dir}" includes="*.jar" /> <fileset dir="${extensible.libs.classpath}" includes="*.jar" /> </classpath> </javac> @@ -355,8 +358,57 @@ your build.xml and it'll be called instead of this one. --> <target name="-post-compile"/> + <target name="-pre-dex" unless="do.not.compile"> + <if condition="${proguard.enabled}"> + <then> + <property name="obfuscate.absolute.dir" location="${out.absolute.dir}/proguard" /> + <property name="preobfuscate.jar.file" value="${obfuscate.absolute.dir}/original.jar" /> + <property name="obfuscated.jar.file" value="${obfuscate.absolute.dir}/obfuscated.jar" /> + <property name="out.dex.input.absolute.dir" value="${obfuscated.jar.file}" /> + + <!-- Add Proguard Tasks --> + <property name="proguard.jar" location="${android.tools.dir}/proguard/lib/proguard.jar" /> + <taskdef name="proguard" classname="proguard.ant.ProGuardTask" classpath="${proguard.jar}" /> + + <!-- Set the android classpath Path object into a single property. It'll be + all the jar files separated by a platform path-separator. + --> + <property name="android.libraryjars" refid="android.target.classpath"/> + <!-- Build a path object with all the jar files that must be obfuscated. + This include the project compiled source code and any 3rd party jar + files. --> + <path id="project.jars.ref"> + <pathelement location="${preobfuscate.jar.file}" /> + <path refid="jar.libs.ref" /> + </path> + <!-- Set the project jar files Path object into a single property. It'll be + all the jar files separated by a platform path-separator. + --> + <property name="project.jars" refid="project.jars.ref" /> + + <mkdir dir="${obfuscate.absolute.dir}" /> + <delete file="${preobfuscate.jar.file}"/> + <delete file="${obfuscated.jar.file}"/> + <jar basedir="${out.classes.dir}" destfile="${preobfuscate.jar.file}" /> + <proguard> + @${proguard.config} + -injars ${project.jars} + -outjars ${obfuscated.jar.file} + -libraryjars ${android.libraryjars} + -dump ${obfuscate.absolute.dir}/dump.txt + -printseeds ${obfuscate.absolute.dir}/seeds.txt + -printusage ${obfuscate.absolute.dir}/usage.txt + -printmapping ${obfuscate.absolute.dir}/mapping.txt + </proguard> + </then> + <else> + <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" /> + </else> + </if> + </target> + <!-- Converts this project's .class files into .dex files --> - <target name="-dex" depends="compile, -post-compile" + <target name="-dex" depends="compile, -post-compile, -pre-dex" unless="do.not.compile"> <if condition="${manifest.hasCode}"> <then> @@ -417,6 +469,10 @@ <!-- whether the build is a debug build. always set. --> <property name="build.debug" value="true" /> + + <!-- dex input file: in debug mode, no proguard, input + is always the out class folder --> + <property name="proguard.enabled" value="false"/> </target> <!-- Builds debug output package, provided all the necessary files are already dexed --> @@ -455,6 +511,18 @@ <!-- whether the build is a debug build. always set. --> <property name="build.debug" value="false" /> + + <!-- dex input file: in release mode, it's dependent on whether + proguard is used. --> + <if isset="proguard.config"> + <then> + <property name="proguard.enabled" value="true"/> + </then> + <else> + <property name="proguard.enabled" value="false"/> + <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" /> + </else> + </if> </target> <!-- This runs -package-release and -release-nosign first and then runs |