Fixed Search Book Contents when built under ProGuard.
[zxing.git] / android / build.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
3 Copyright (C) 2008 ZXing authors
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9      http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 -->
17 <project name="BarcodeScanner" default="debug-optimized">
18   <!-- SDK Locations -->
19   <property file="../build.properties"/>
20   <property name="sdk-folder" value="${android-home}"/>
21   <property name="android-tools" value="${sdk-folder}/tools"/>
22
23   <!-- Application Package Name -->
24   <property name="application-package" value="com.google.zxing.client.android" />
25
26   <!-- The intermediates directory -->
27   <!-- Eclipse uses "bin" for its own output, so we do the same. -->
28   <property name="outdir" value="bin" />
29
30   <!-- ************************************************************************************* -->
31   <!-- No user servicable parts below. -->
32
33   <property name="android-framework" value="${android-tools}/lib/framework.aidl" />
34
35   <!-- Input directories -->
36   <property name="resource-dir" value="res" />
37   <property name="asset-dir" value="assets" />
38   <property name="srcdir" value="src" />
39   <condition property="srcdir-ospath"
40              value="${basedir}\${srcdir}"
41              else="${basedir}/${srcdir}" >
42     <os family="windows"/>
43   </condition>
44
45   <property name="external-libs" value="../core" />
46   <condition property="external-libs-ospath"
47              value="${basedir}\${external-libs}"
48              else="${basedir}/${external-libs}" >
49     <os family="windows"/>
50   </condition>
51
52   <!-- Output directories -->
53   <property name="outdir-classes" value="${outdir}/classes" />
54   <condition property="outdir-classes-ospath"
55              value="${basedir}\${outdir-classes}"
56              else="${basedir}/${outdir-classes}" >
57     <os family="windows"/>
58   </condition>
59
60   <!-- Create R.java in the source directory -->
61   <property name="outdir-r" value="src" />
62
63   <!-- Intermediate files -->
64   <property name="dex-file" value="classes.dex" />
65   <property name="intermediate-dex" value="${outdir}/${dex-file}" />
66   <condition property="intermediate-dex-ospath"
67              value="${basedir}\${intermediate-dex}"
68              else="${basedir}/${intermediate-dex}" >
69     <os family="windows"/>
70   </condition>
71
72   <!-- The final package file to generate -->
73   <property name="resources-package" value="${outdir}/${ant.project.name}.ap_" />
74   <condition property="resources-package-ospath"
75              value="${basedir}\${resources-package}"
76              else="${basedir}/${resources-package}" >
77     <os family="windows"/>
78   </condition>
79
80   <property name="out-debug-package" value="${outdir}/${ant.project.name}-debug.apk" />
81   <condition property="out-debug-package-ospath"
82              value="${basedir}\${out-debug-package}"
83              else="${basedir}/${out-debug-package}" >
84     <os family="windows"/>
85   </condition>
86
87   <property name="out-unsigned-package" value="${outdir}/${ant.project.name}-unsigned.apk" />
88   <condition property="out-unsigned-package-ospath"
89              value="${basedir}\${out-unsigned-package}"
90              else="${basedir}/${out-unsigned-package}" >
91     <os family="windows"/>
92   </condition>
93
94   <!-- Tools -->
95   <condition property="aapt" value="${android-tools}/aapt.exe" else="${android-tools}/aapt" >
96     <os family="windows"/>
97   </condition>
98   <condition property="aidl" value="${android-tools}/aidl.exe" else="${android-tools}/aidl" >
99     <os family="windows"/>
100   </condition>
101   <condition property="adb" value="${android-tools}/adb.exe" else="${android-tools}/adb" >
102     <os family="windows"/>
103   </condition>
104   <condition property="dx" value="${android-tools}/dx.bat" else="${android-tools}/dx" >
105     <os family="windows"/>
106   </condition>
107   <condition property="apk-builder" value="${android-tools}/apkbuilder.bat" else="${android-tools}/apkbuilder" >
108     <os family="windows"/>
109   </condition>
110
111   <property name="android-jar" value="${sdk-folder}/android.jar" />
112
113   <!-- Rules -->
114
115   <!-- Create the output directories if they don't exist yet. -->
116   <target name="dirs">
117     <echo>Creating output directories if needed...</echo>
118     <mkdir dir="${outdir}" />
119     <mkdir dir="${outdir-classes}" />
120   </target>
121
122   <!-- Generate the R.java file for this project's resources. -->
123   <target name="resource-src" depends="dirs">
124     <echo>Generating R.java / Manifest.java from the resources...</echo>
125     <exec executable="${aapt}" failonerror="true">
126       <arg value="package" />
127       <arg value="-m" />
128       <arg value="-J" />
129       <arg value="${outdir-r}" />
130       <arg value="-M" />
131       <arg value="AndroidManifest.xml" />
132       <arg value="-S" />
133       <arg value="${resource-dir}" />
134       <arg value="-I" />
135       <arg value="${android-jar}" />
136     </exec>
137   </target>
138
139   <!-- Generate java classes from .aidl files. -->
140   <target name="aidl" depends="dirs">
141     <echo>Compiling aidl files into Java classes...</echo>
142     <apply executable="${aidl}" failonerror="true">
143       <arg value="-p${android-framework}" />
144       <arg value="-I${srcdir}" />
145       <fileset dir="${srcdir}">
146         <include name="**/*.aidl"/>
147       </fileset>
148     </apply>
149   </target>
150
151   <!-- Compile this project's .java files into .class files. -->
152   <target name="compile" depends="clean, dirs, resource-src, aidl">
153     <javac encoding="ascii" target="1.5" debug="true" extdirs=""
154            srcdir="."
155            destdir="${outdir-classes}"
156            bootclasspath="${android-jar}">
157       <classpath>
158         <fileset dir="${external-libs}" includes="*.jar"/>
159       </classpath>
160     </javac>
161   </target>
162
163   <target name="compile-release" depends="clean, dirs, resource-src, aidl">
164     <javac encoding="ascii" target="1.5" debug="off" extdirs=""
165            srcdir="."
166            destdir="${outdir-classes}"
167            bootclasspath="${android-jar}">
168       <classpath>
169         <fileset dir="${external-libs}" includes="*.jar"/>
170       </classpath>
171     </javac>
172   </target>
173
174   <target name="optimize" depends="compile-release">
175     <fail message="Please put proguard.jar in 'bin' under the WTK install directory">
176       <condition>
177         <not>
178           <available file="${WTK-home}/bin/proguard.jar" type="file"/>
179         </not>
180       </condition>
181     </fail>
182     <unzip src="../core/core.jar" dest="${outdir-classes}" overwrite="true"/>
183     <jar jarfile="temp.jar" basedir="${outdir-classes}"/>
184     <delete dir="${outdir-classes}"/>
185     <mkdir dir="${outdir-classes}"/>
186     <java jar="${WTK-home}/bin/proguard.jar" fork="true" failonerror="true">
187       <jvmarg value="-Dmaximum.inlined.code.length=32"/>
188       <arg value="-injars temp.jar"/>
189       <arg value="-outjars temp-optimized.jar"/>
190       <arg value="-libraryjars ${android-jar}"/>
191       <arg value="-keep class com.google.zxing.client.android.*Activity"/>
192       <arg value="-keep class com.google.zxing.client.android.ViewfinderView { public * ; }"/>
193       <arg value="-keep class com.google.zxing.client.android.SearchBookContents* { public * ; }"/>
194       <arg value="-target 5"/>
195       <arg value="-optimizationpasses 7"/>
196       <arg value="-dontshrink"/>
197       <arg value="-dontobfuscate"/>
198       <arg value="-dontskipnonpubliclibraryclasses"/>
199       <arg value="-verbose"/>
200       <arg value="-dump proguard-dump.txt"/>
201     </java>
202     <delete file="temp.jar"/>
203     <unzip src="temp-optimized.jar" dest="${outdir-classes}" overwrite="true"/>
204     <delete file="temp-optimized.jar"/>
205   </target>
206
207   <!-- Convert this project's .class files into .dex files. -->
208   <target name="dex" depends="compile">
209     <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
210     <apply executable="${dx}" failonerror="true" parallel="true">
211       <arg value="--dex" />
212       <arg value="--output=${intermediate-dex-ospath}" />
213       <arg path="${outdir-classes-ospath}" />
214       <fileset dir="${external-libs}" includes="*.jar"/>
215     </apply>
216   </target>
217
218   <target name="dex-optimized" depends="compile-release,optimize">
219     <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
220     <apply executable="${dx}" failonerror="true" parallel="true">
221       <arg value="--dex" />
222       <arg value="--output=${intermediate-dex-ospath}" />
223       <arg path="${outdir-classes-ospath}" />
224       <!-- workaround to appease 'apply'. Not really the right Ant task to use here. -->
225       <fileset dir="." includes="nosuchfile.jar"/>
226     </apply>
227   </target>
228
229   <!-- Put the project's resources into the output package file. -->
230   <target name="package-res-and-assets">
231     <echo>Packaging resources and assets...</echo>
232     <exec executable="${aapt}" failonerror="true">
233       <arg value="package" />
234       <arg value="-f" />
235       <arg value="-M" />
236       <arg value="AndroidManifest.xml" />
237       <arg value="-S" />
238       <arg value="${resource-dir}" />
239       <arg value="-A" />
240       <arg value="${asset-dir}" />
241       <arg value="-I" />
242       <arg value="${android-jar}" />
243       <arg value="-F" />
244       <arg value="${resources-package}" />
245     </exec>
246   </target>
247
248   <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->
249   <target name="package-res-no-assets">
250     <echo>Packaging resources...</echo>
251     <exec executable="${aapt}" failonerror="true">
252       <arg value="package" />
253       <arg value="-f" />
254       <arg value="-M" />
255       <arg value="AndroidManifest.xml" />
256       <arg value="-S" />
257       <arg value="${resource-dir}" />
258       <!-- No assets directory -->
259       <arg value="-I" />
260       <arg value="${android-jar}" />
261       <arg value="-F" />
262       <arg value="${resources-package}" />
263     </exec>
264   </target>
265
266   <!-- Invoke the proper target depending on whether or not
267 an assets directory is present. -->
268   <!-- TODO: find a nicer way to include the "-A ${asset-dir}" argument
269 only when the assets dir exists. -->
270   <target name="package-res">
271     <available file="${asset-dir}" type="dir"
272                property="res-target" value="and-assets" />
273     <property name="res-target" value="no-assets" />
274     <antcall target="package-res-${res-target}" />
275   </target>
276
277   <!-- Package the application and sign it with a debug key.
278       This is the default target when building. It is used for debug. -->
279   <target name="debug" depends="dex, package-res">
280     <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
281     <exec executable="${apk-builder}" failonerror="true">
282       <arg value="${out-debug-package-ospath}" />
283       <arg value="-z" />
284       <arg value="${resources-package-ospath}" />
285       <arg value="-f" />
286       <arg value="${intermediate-dex-ospath}" />
287       <arg value="-rf" />
288       <arg value="${srcdir-ospath}" />
289       <arg value="-rj" />
290       <arg value="${external-libs-ospath}" />
291     </exec>
292   </target>
293
294   <!-- Package the optimized application and sign it with a debug key. -->
295   <target name="debug-optimized" depends="dex-optimized, package-res">
296     <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
297     <exec executable="${apk-builder}" failonerror="true">
298       <arg value="${out-debug-package-ospath}" />
299       <arg value="-z" />
300       <arg value="${resources-package-ospath}" />
301       <arg value="-f" />
302       <arg value="${intermediate-dex-ospath}" />
303       <arg value="-rf" />
304       <arg value="${srcdir-ospath}" />
305       <arg value="-rj" />
306       <arg value="${external-libs-ospath}" />
307     </exec>
308   </target>
309
310   <!-- Package the application without signing it.
311         This allows for the application to be signed later with an official publishing key. -->
312   <target name="release" depends="dex-optimized, package-res">
313     <echo>Packaging ${out-unsigned-package} for release...</echo>
314     <exec executable="${apk-builder}" failonerror="true">
315       <arg value="${out-unsigned-package-ospath}" />
316       <arg value="-u" />
317       <arg value="-z" />
318       <arg value="${resources-package-ospath}" />
319       <arg value="-f" />
320       <arg value="${intermediate-dex-ospath}" />
321       <arg value="-rf" />
322       <arg value="${srcdir-ospath}" />
323     </exec>
324     <echo>It will need to be signed with jarsigner before being published.</echo>
325   </target>
326
327   <!-- Install the package on the default emulator -->
328   <target name="install">
329     <echo>Installing ${out-debug-package} onto default emulator...</echo>
330     <exec executable="${adb}" failonerror="true">
331       <arg value="install" />
332       <arg value="${out-debug-package}" />
333     </exec>
334   </target>
335
336   <target name="reinstall">
337     <echo>Installing ${out-debug-package} onto default emulator...</echo>
338     <exec executable="${adb}" failonerror="true">
339       <arg value="install" />
340       <arg value="-r" />
341       <arg value="${out-debug-package}" />
342     </exec>
343   </target>
344
345   <!-- Uinstall the package from the default emulator -->
346   <target name="uninstall">
347     <echo>Uninstalling ${application-package} from the default emulator...</echo>
348     <exec executable="${adb}" failonerror="true">
349       <arg value="uninstall" />
350       <arg value="${application-package}" />
351     </exec>
352   </target>
353
354   <target name="clean">
355     <delete dir="${outdir}"/>
356     <delete file="proguard-dump.txt"/>
357   </target>
358
359 </project>