e850857a09089fecbe6d5e9b63c7dcce7c6af171
[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">
18
19   <!-- Normally the Android build system looks for a local.properties. Since that's only used
20   to find the SDK location, I've removed it and pointed us at the global ZXing build.properties. -->
21   <property file="../build.properties"/>
22   <!-- Parts of the Android build system insist on the name 'sdk-location', so alias it. -->
23   <property name="sdk.dir" value="${android-home}"/>
24
25   <!-- The build.properties file can be created by you and is never touched
26   by the 'android' tool. This is the place to change some of the default property values
27   used by the Ant rules.
28   Here are some properties you may want to change/update:
29
30   application-package
31       the name of your application package as defined in the manifest. Used by the
32       'uninstall' rule.
33   source-folder
34       the name of the source folder. Default is 'src'.
35   out-folder
36       the name of the output folder. Default is 'bin'.
37
38   Properties related to the SDK location or the project target should be updated
39    using the 'android' tool with the 'update' action.
40
41   This file is an integral part of the build system for your application and
42   should be checked in in Version Control Systems.
43
44   -->
45   <property file="build.properties"/>
46
47   <!-- The default.properties file is created and updated by the 'android' tool, as well as ADT.
48   This file is an integral part of the build system for your application and
49   should be checked in in Version Control Systems. -->
50   <property file="default.properties"/>
51
52   <!-- Custom Android task to deal with the project target, and import the proper rules.
53   This requires ant 1.6.0 or above. -->
54   <path id="android.antlibs">
55     <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
56     <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
57     <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
58     <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
59     <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
60   </path>
61
62   <taskdef name="setup"
63            classname="com.android.ant.SetupTask"
64            classpathref="android.antlibs"/>
65
66   <!-- Execute the Android Setup task that will setup some properties specific to the target,
67        and import the rules files.
68        To customize the rules, copy/paste them below the task, and disable import by setting
69        the import attribute to false:
70           <setup import="false" />
71
72        This will ensure that the properties are setup correctly but that your customized
73        targets are used.
74   -->
75   <setup import="false" />
76
77   <!-- Custom tasks -->
78   <taskdef name="aaptexec"
79            classname="com.android.ant.AaptExecLoopTask"
80            classpathref="android.antlibs"/>
81
82   <taskdef name="apkbuilder"
83            classname="com.android.ant.ApkBuilderTask"
84            classpathref="android.antlibs"/>
85
86   <!-- Properties -->
87
88   <property name="android-tools" value="${sdk.dir}/tools" />
89
90   <!-- Input directories -->
91   <property name="source-folder" value="src" />
92   <property name="gen-folder" value="gen" />
93   <property name="resource-folder" value="res" />
94   <property name="asset-folder" value="assets" />
95   <property name="source-location" value="${basedir}/${source-folder}" />
96
97   <!-- folder for the 3rd party java libraries -->
98   <!--<property name="external-libs-folder" value="../core" />-->
99
100   <!-- Output directories -->
101   <property name="gen-folder" value="gen" />
102   <property name="out-folder" value="bin" />
103   <property name="out-classes" value="${out-folder}/classes" />
104   <property name="out-classes-location" value="${basedir}/${out-classes}"/>
105   <!-- out folders for a parent project if this project is an instrumentation project -->
106   <property name="main-out-folder" value="../${out-folder}" />
107   <property name="main-out-classes" value="${main-out-folder}/classes"/>
108
109   <!-- Intermediate files -->
110   <property name="dex-file" value="classes.dex" />
111   <property name="intermediate-dex" value="${out-folder}/${dex-file}" />
112   <!-- dx does not properly support incorrect / or \ based on the platform
113   and Ant cannot convert them because the parameter is not a valid path.
114   Because of this we have to compute different paths depending on the platform. -->
115   <condition property="intermediate-dex-location"
116              value="${basedir}\${intermediate-dex}"
117              else="${basedir}/${intermediate-dex}" >
118     <os family="windows"/>
119   </condition>
120
121   <!-- The final package file to generate -->
122   <property name="out-debug-package" value="${out-folder}/${ant.project.name}-debug.apk"/>
123
124   <!-- Tools -->
125   <condition property="exe" value=".exe" else=""><os family="windows"/></condition>
126   <property name="adb" value="${android-tools}/adb${exe}"/>
127
128   <!-- rules -->
129
130   <!-- Create the output directories if they don't exist yet. All builds do a clean first
131   to prevent stale resources and to make ProGuard happy. -->
132   <target name="dirs" depends="clean">
133     <echo>Creating output directories if needed...</echo>
134     <mkdir dir="${resource-folder}" />
135     <mkdir dir="${external-libs-folder}" />
136     <mkdir dir="${gen-folder}" />
137     <mkdir dir="${out-folder}" />
138     <mkdir dir="${out-classes}" />
139   </target>
140
141   <!-- Generate the R.java file for this project's resources. -->
142   <target name="resource-src" depends="dirs">
143     <echo>Generating R.java / Manifest.java from the resources...</echo>
144     <exec executable="${aapt}" failonerror="true">
145       <arg value="package" />
146       <arg value="-m" />
147       <arg value="-J" />
148       <arg path="${gen-folder}" />
149       <arg value="-M" />
150       <arg path="AndroidManifest.xml" />
151       <arg value="-S" />
152       <arg path="${resource-folder}" />
153       <arg value="-I" />
154       <arg path="${android.jar}" />
155     </exec>
156   </target>
157
158   <!-- Generate java classes from .aidl files. -->
159   <target name="aidl" depends="dirs">
160     <echo>Compiling aidl files into Java classes...</echo>
161     <apply executable="${aidl}" failonerror="true">
162       <arg value="-p${android-aidl}" />
163       <arg value="-I${source-folder}" />
164       <arg value="-o${gen-folder}" />
165       <fileset dir="${source-folder}">
166         <include name="**/*.aidl"/>
167       </fileset>
168     </apply>
169   </target>
170
171   <!-- Compile this project's .java files into .class files. -->
172   <target name="compile" depends="resource-src, aidl">
173     <javac encoding="ascii" target="1.5" debug="false" extdirs=""
174            destdir="${out-classes}"
175            bootclasspathref="android.target.classpath">
176       <src path="${source-folder}" />
177       <src path="${gen-folder}" />
178       <classpath>
179         <fileset dir="${external-libs-folder}" includes="*.jar"/>
180         <!-- yeah, want to not use this mechanism above -->
181         <pathelement path="../core/core.jar"/>
182         <pathelement path="${main-out-classes}"/>
183       </classpath>
184     </javac>
185
186     <unzip src="../core/core.jar" dest="${out-classes}" overwrite="true"/>
187
188     <antcall target="optimize"/>
189   </target>
190
191   <target name="optimize" unless="no-optimize">
192     <mkdir dir="optimized"/>
193     <property name="libraryjars.path" refid="android.target.classpath"/>
194     <java jar="${proguard-jar}" fork="true" failonerror="true">
195       <jvmarg value="-Dmaximum.inlined.code.length=48"/>
196       <arg value="-injars ${out-classes}"/>
197       <arg value="-outjars optimized"/>
198       <arg value="-libraryjars ${libraryjars.path}"/>
199       <arg value="-keep class com.google.zxing.client.android.*Activity"/>
200       <arg value="-keep class com.google.zxing.client.android.ViewfinderView { public * ; }"/>
201       <arg value="-keep class com.google.zxing.client.android.book.SearchBookContents* { public * ; }"/>
202       <arg value="-target 5"/>
203       <arg value="-optimizationpasses 5"/>
204       <arg value="-optimizations !field/*,!class/merging/*"/> <!-- works around dex VerifyError -->
205       <arg value="-dontshrink"/>
206       <arg value="-dontobfuscate"/>
207       <arg value="-dontskipnonpubliclibraryclasses"/>
208       <arg value="-verbose"/>
209       <arg value="-dump proguard-dump.txt"/>
210     </java>
211     <delete dir="${out-classes}"/>
212     <move file="optimized" tofile="${out-classes}"/>
213   </target>
214
215   <!-- Convert this project's .class files into .dex files. -->
216   <target name="dex" depends="compile">
217     <echo>Converting compiled files and external libraries into ${out-folder}/${dex-file}...</echo>
218     <apply executable="${dx}" failonerror="true" parallel="true">
219       <arg value="-JXmx256M"/>
220       <arg value="--dex" />
221       <arg value="--output=${intermediate-dex-location}" />
222       <arg path="${out-classes-location}" />
223       <fileset dir="${external-libs-folder}" includes="*.jar"/>
224     </apply>
225   </target>
226
227   <!-- Put the project's resources into the output package file
228   This actually can create multiple resource package in case
229   Some custom apk with specific configuration have been
230   declared in default.properties.
231   -->
232   <target name="package-resources">
233     <echo>Packaging resources</echo>
234     <aaptexec executable="${aapt}"
235               command="package"
236               manifest="AndroidManifest.xml"
237               resources="${resource-folder}"
238               assets="${asset-folder}"
239               androidjar="${android.jar}"
240               outfolder="${out-folder}"
241               basename="${ant.project.name}" />
242   </target>
243
244   <!--
245   Getting an error like this?
246
247    [apply] UNEXPECTED TOP-LEVEL EXCEPTION:
248    [apply] com.android.dx.cf.code.SimException: local variable type
249    mismatch: attempt to set or access a value of type int using a local
250    variable of type com.google.zxing.qrcode.decoder.Version. This is
251    symptomatic of .class transformation tools that ignore local variable
252    information.
253
254   Build core/ with the 'build-no-debug' target. It's a long story.
255   -->
256
257   <!-- Package the application and sign it with a debug key.
258   This is the default target when building. It is used for debug. -->
259   <target name="debug" depends="dex, package-resources">
260     <apkbuilder
261         outfolder="${out-folder}"
262         basename="${ant.project.name}"
263         signed="true"
264         verbose="false">
265       <file path="${intermediate-dex}" />
266       <sourcefolder path="${source-folder}" />
267       <jarfolder path="${external-libs-folder}" />
268     </apkbuilder>
269     <copy file="${out-folder}/BarcodeScanner-debug.apk" tofile="${out-folder}/temp.apk" overwrite="true"/>
270     <exec executable="${android-tools}/zipalign">
271       <arg value="-f"/>
272       <arg value="-v"/>
273       <arg value="4"/>
274       <arg value="${out-folder}/temp.apk"/>
275       <arg value="${out-folder}/BarcodeScanner-debug.apk"/>
276     </exec>
277   </target>
278
279   <!-- Package the application without signing it.
280   This allows for the application to be signed later with an official publishing key. -->
281   <target name="release" depends="dex, package-resources">
282     <apkbuilder
283         outfolder="${out-folder}"
284         basename="${ant.project.name}"
285         signed="false"
286         verbose="false">
287       <file path="${intermediate-dex}" />
288       <sourcefolder path="${source-folder}" />
289       <jarfolder path="${external-libs-folder}" />
290     </apkbuilder>
291     <echo>All generated packages need to be signed with jarsigner before they are published.</echo>
292     <echo>Also run zipalign -f -v 4 BarcodeScanner.apk BarcodeScanner-aligned.apk after signing</echo>
293   </target>
294
295   <!-- Install (or reinstall) the package on the default emulator -->
296   <target name="install" depends="debug">
297     <echo>Installing ${out-debug-package} onto default emulator...</echo>
298     <exec executable="${adb}" failonerror="true">
299       <arg value="install" />
300       <arg value="-r" />
301       <arg path="${out-debug-package}" />
302     </exec>
303   </target>
304
305   <!-- Uinstall the package from the default emulator -->
306   <target name="uninstall">
307     <echo>Uninstalling ${application-package} from the default emulator...</echo>
308     <exec executable="${adb}" failonerror="true">
309       <arg value="uninstall" />
310       <arg value="${application-package}" />
311     </exec>
312   </target>
313
314   <target name="help">
315     <echo>Android Ant Build. Available targets:</echo>
316     <echo>   help:      Displays this help.</echo>
317     <echo>   debug:     Builds the application and sign it with a debug key.</echo>
318     <echo>   release:   Builds the application. The generated apk file must be</echo>
319     <echo>              signed before it is published.</echo>
320     <echo>   install:   Installs the debug package onto a running emulator or</echo>
321     <echo>              device. This can only be used if the application has </echo>
322     <echo>              not yet been installed.</echo>
323     <echo>   reinstall: Installs the debug package on a running emulator or</echo>
324     <echo>              device that already has the application.</echo>
325     <echo>              The signatures must match.</echo>
326     <echo>   uninstall: uninstall the application from a running emulator or</echo>
327     <echo>              device.</echo>
328   </target>
329
330   <target name="clean">
331     <delete dir="${out-folder}"/>
332     <delete dir="${gen-folder}"/>
333   </target>
334 </project>