f7760de7a99b5138e224c717203cc50d858a6022
[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   <!--
175   Getting an error like this?
176
177    [apply] UNEXPECTED TOP-LEVEL EXCEPTION:
178    [apply] com.android.dx.cf.code.SimException: local variable type
179    mismatch: attempt to set or access a value of type int using a local
180    variable of type com.google.zxing.qrcode.decoder.Version. This is
181    symptomatic of .class transformation tools that ignore local variable
182    information.
183
184   Build core/ with the 'build-no-debug' target. It's a long story.
185   -->
186
187   <target name="optimize" depends="compile-release">
188     <fail message="Please put proguard.jar in 'bin' under the WTK install directory">
189       <condition>
190         <not>
191           <available file="${WTK-home}/bin/proguard.jar" type="file"/>
192         </not>
193       </condition>
194     </fail>
195     <unzip src="../core/core.jar" dest="${outdir-classes}" overwrite="true"/>
196     <jar jarfile="temp.jar" basedir="${outdir-classes}"/>
197     <delete dir="${outdir-classes}"/>
198     <mkdir dir="${outdir-classes}"/>
199     <java jar="${WTK-home}/bin/proguard.jar" fork="true" failonerror="true">
200       <jvmarg value="-Dmaximum.inlined.code.length=32"/>
201       <arg value="-injars temp.jar"/>
202       <arg value="-outjars temp-optimized.jar"/>
203       <arg value="-libraryjars ${android-jar}"/>
204       <arg value="-keep class com.google.zxing.client.android.*Activity"/>
205       <arg value="-keep class com.google.zxing.client.android.ViewfinderView { public * ; }"/>
206       <arg value="-keep class com.google.zxing.client.android.SearchBookContents* { public * ; }"/>
207       <arg value="-target 5"/>
208       <arg value="-optimizationpasses 7"/>
209       <arg value="-dontshrink"/>
210       <arg value="-dontobfuscate"/>
211       <arg value="-dontskipnonpubliclibraryclasses"/>
212       <arg value="-verbose"/>
213       <arg value="-dump proguard-dump.txt"/>
214     </java>
215     <delete file="temp.jar"/>
216     <unzip src="temp-optimized.jar" dest="${outdir-classes}" overwrite="true"/>
217     <delete file="temp-optimized.jar"/>
218   </target>
219
220   <!-- Convert this project's .class files into .dex files. -->
221   <target name="dex" depends="compile">
222     <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
223     <echo>NOTE: This will fail unless you built core with build-no-debug.</echo>
224     <apply executable="${dx}" failonerror="true" parallel="true">
225       <arg value="--dex" />
226       <arg value="--output=${intermediate-dex-ospath}" />
227       <arg path="${outdir-classes-ospath}" />
228       <fileset dir="${external-libs}" includes="*.jar"/>
229     </apply>
230   </target>
231
232   <target name="dex-optimized" depends="compile-release,optimize">
233     <echo>Converting compiled files and external libraries into ${outdir}/${dex-file}...</echo>
234     <echo>NOTE: This will fail unless you built core with build-no-debug.</echo>      
235     <apply executable="${dx}" failonerror="true" parallel="true">
236       <arg value="--dex" />
237       <arg value="--output=${intermediate-dex-ospath}" />
238       <arg path="${outdir-classes-ospath}" />
239       <!-- workaround to appease 'apply'. Not really the right Ant task to use here. -->
240       <fileset dir="." includes="nosuchfile.jar"/>
241     </apply>
242   </target>
243
244   <!-- Put the project's resources into the output package file. -->
245   <target name="package-res-and-assets">
246     <echo>Packaging resources and assets...</echo>
247     <exec executable="${aapt}" failonerror="true">
248       <arg value="package" />
249       <arg value="-f" />
250       <arg value="-M" />
251       <arg value="AndroidManifest.xml" />
252       <arg value="-S" />
253       <arg value="${resource-dir}" />
254       <arg value="-A" />
255       <arg value="${asset-dir}" />
256       <arg value="-I" />
257       <arg value="${android-jar}" />
258       <arg value="-F" />
259       <arg value="${resources-package}" />
260     </exec>
261   </target>
262
263   <!-- Same as package-res-and-assets, but without "-A ${asset-dir}" -->
264   <target name="package-res-no-assets">
265     <echo>Packaging resources...</echo>
266     <exec executable="${aapt}" failonerror="true">
267       <arg value="package" />
268       <arg value="-f" />
269       <arg value="-M" />
270       <arg value="AndroidManifest.xml" />
271       <arg value="-S" />
272       <arg value="${resource-dir}" />
273       <!-- No assets directory -->
274       <arg value="-I" />
275       <arg value="${android-jar}" />
276       <arg value="-F" />
277       <arg value="${resources-package}" />
278     </exec>
279   </target>
280
281   <!-- Invoke the proper target depending on whether or not an assets directory is present. -->
282   <target name="package-res">
283     <available file="${asset-dir}" type="dir"
284                property="res-target" value="and-assets" />
285     <property name="res-target" value="no-assets" />
286     <antcall target="package-res-${res-target}" />
287   </target>
288
289   <!-- Package the application and sign it with a debug key.
290       This is the default target when building. It is used for debug. -->
291   <target name="debug" depends="dex, package-res">
292     <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
293     <exec executable="${apk-builder}" failonerror="true">
294       <arg value="${out-debug-package-ospath}" />
295       <arg value="-z" />
296       <arg value="${resources-package-ospath}" />
297       <arg value="-f" />
298       <arg value="${intermediate-dex-ospath}" />
299       <arg value="-rf" />
300       <arg value="${srcdir-ospath}" />
301       <arg value="-rj" />
302       <arg value="${external-libs-ospath}" />
303     </exec>
304   </target>
305
306   <!-- Package the optimized application and sign it with a debug key. -->
307   <target name="debug-optimized" depends="dex-optimized, package-res">
308     <echo>Packaging ${out-debug-package}, and signing it with a debug key...</echo>
309     <exec executable="${apk-builder}" failonerror="true">
310       <arg value="${out-debug-package-ospath}" />
311       <arg value="-z" />
312       <arg value="${resources-package-ospath}" />
313       <arg value="-f" />
314       <arg value="${intermediate-dex-ospath}" />
315       <arg value="-rf" />
316       <arg value="${srcdir-ospath}" />
317       <arg value="-rj" />
318       <arg value="${external-libs-ospath}" />
319     </exec>
320   </target>
321
322   <!-- Package the application without signing it.
323         This allows for the application to be signed later with an official publishing key. -->
324   <target name="release" depends="dex-optimized, package-res">
325     <echo>Packaging ${out-unsigned-package} for release...</echo>
326     <exec executable="${apk-builder}" failonerror="true">
327       <arg value="${out-unsigned-package-ospath}" />
328       <arg value="-u" />
329       <arg value="-z" />
330       <arg value="${resources-package-ospath}" />
331       <arg value="-f" />
332       <arg value="${intermediate-dex-ospath}" />
333       <arg value="-rf" />
334       <arg value="${srcdir-ospath}" />
335     </exec>
336     <echo>It will need to be signed with jarsigner before being published.</echo>
337   </target>
338
339   <!-- Install the package on the default emulator -->
340   <target name="install">
341     <echo>Installing ${out-debug-package} onto default emulator...</echo>
342     <exec executable="${adb}" failonerror="true">
343       <arg value="install" />
344       <arg value="${out-debug-package}" />
345     </exec>
346   </target>
347
348   <target name="reinstall">
349     <echo>Installing ${out-debug-package} onto default emulator...</echo>
350     <exec executable="${adb}" failonerror="true">
351       <arg value="install" />
352       <arg value="-r" />
353       <arg value="${out-debug-package}" />
354     </exec>
355   </target>
356
357   <!-- Uinstall the package from the default emulator -->
358   <target name="uninstall">
359     <echo>Uninstalling ${application-package} from the default emulator...</echo>
360     <exec executable="${adb}" failonerror="true">
361       <arg value="uninstall" />
362       <arg value="${application-package}" />
363     </exec>
364   </target>
365
366   <target name="clean">
367     <delete dir="${outdir}"/>
368     <delete file="proguard-dump.txt"/>
369   </target>
370
371 </project>