d2f7b74c54711d9c1995f360b4d64eb33a74f125
[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-location" 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-location}/tools/lib/anttasks.jar" />
56     <pathelement path="${sdk-location}/tools/lib/sdklib.jar" />
57     <pathelement path="${sdk-location}/tools/lib/androidprefs.jar" />
58     <pathelement path="${sdk-location}/tools/lib/apkbuilder.jar" />
59     <pathelement path="${sdk-location}/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-location}/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   <!-- folder for the native libraries -->
101   <property name="native-libs-folder" value="libs" />
102
103   <!-- Output directories -->
104   <property name="gen-folder" value="gen" />
105   <property name="out-folder" value="bin" />
106   <property name="out-classes" value="${out-folder}/classes" />
107   <property name="out-classes-location" value="${basedir}/${out-classes}"/>
108   <!-- out folders for a parent project if this project is an instrumentation project -->
109   <property name="main-out-folder" value="../${out-folder}" />
110   <property name="main-out-classes" value="${main-out-folder}/classes"/>
111
112   <!-- Intermediate files -->
113   <property name="dex-file" value="classes.dex" />
114   <property name="intermediate-dex" value="${out-folder}/${dex-file}" />
115   <!-- dx does not properly support incorrect / or \ based on the platform
116   and Ant cannot convert them because the parameter is not a valid path.
117   Because of this we have to compute different paths depending on the platform. -->
118   <condition property="intermediate-dex-location"
119              value="${basedir}\${intermediate-dex}"
120              else="${basedir}/${intermediate-dex}" >
121     <os family="windows"/>
122   </condition>
123
124   <!-- The final package file to generate -->
125   <property name="out-debug-package" value="${out-folder}/${ant.project.name}-debug.apk"/>
126
127   <!-- Tools -->
128   <condition property="exe" value=".exe" else=""><os family="windows"/></condition>
129   <property name="adb" value="${android-tools}/adb${exe}"/>
130
131   <!-- rules -->
132
133   <!-- Create the output directories if they don't exist yet. All builds do a clean first
134   to prevent stale resources and to make ProGuard happy. -->
135   <target name="dirs" depends="clean">
136     <echo>Creating output directories if needed...</echo>
137     <mkdir dir="${resource-folder}" />
138     <mkdir dir="${external-libs-folder}" />
139     <mkdir dir="${gen-folder}" />
140     <mkdir dir="${out-folder}" />
141     <mkdir dir="${out-classes}" />
142   </target>
143
144   <!-- Generate the R.java file for this project's resources. -->
145   <target name="resource-src" depends="dirs">
146     <echo>Generating R.java / Manifest.java from the resources...</echo>
147     <exec executable="${aapt}" failonerror="true">
148       <arg value="package" />
149       <arg value="-m" />
150       <arg value="-J" />
151       <arg path="${gen-folder}" />
152       <arg value="-M" />
153       <arg path="AndroidManifest.xml" />
154       <arg value="-S" />
155       <arg path="${resource-folder}" />
156       <arg value="-I" />
157       <arg path="${android-jar}" />
158     </exec>
159   </target>
160
161   <!-- Generate java classes from .aidl files. -->
162   <target name="aidl" depends="dirs">
163     <echo>Compiling aidl files into Java classes...</echo>
164     <apply executable="${aidl}" failonerror="true">
165       <arg value="-p${android-aidl}" />
166       <arg value="-I${source-folder}" />
167       <arg value="-o${gen-folder}" />
168       <fileset dir="${source-folder}">
169         <include name="**/*.aidl"/>
170       </fileset>
171     </apply>
172   </target>
173
174   <!-- Compile this project's .java files into .class files. -->
175   <target name="compile" depends="resource-src, aidl">
176     <javac encoding="ascii" target="1.5" debug="true" extdirs=""
177            destdir="${out-classes}"
178            bootclasspathref="android.target.classpath">
179       <src path="${source-folder}" />
180       <src path="${gen-folder}" />
181       <classpath>
182         <fileset dir="${external-libs-folder}" includes="*.jar"/>
183         <!-- yeah, want to not use this mechanism above -->
184         <pathelement path="../core/core.jar"/>
185         <pathelement path="${main-out-classes}"/>
186       </classpath>
187     </javac>
188
189     <unzip src="../core/core.jar" dest="${out-classes}" overwrite="true"/>
190
191     <antcall target="optimize"/>
192   </target>
193
194   <target name="optimize" unless="no-optimize">
195     <mkdir dir="optimized"/>
196     <property name="libraryjars.path" refid="android.target.classpath"/>
197     <java jar="${proguard-jar}" fork="true" failonerror="true">
198       <jvmarg value="-Dmaximum.inlined.code.length=48"/>
199       <arg value="-injars ${out-classes}"/>
200       <arg value="-outjars optimized"/>
201       <arg value="-libraryjars ${libraryjars.path}"/>
202       <arg value="-keep class com.google.zxing.client.android.*Activity"/>
203       <arg value="-keep class com.google.zxing.client.android.ViewfinderView { public * ; }"/>
204       <arg value="-keep class com.google.zxing.client.android.book.SearchBookContents* { public * ; }"/>
205       <!-- This works around some strange Android/ProGuard problem verifying MaskUtil -->
206       <arg value="-keep class com.google.zxing.qrcode.encoder.MaskUtil { public * ; }"/>
207       <arg value="-target 5"/>
208       <arg value="-optimizationpasses 4"/>
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 dir="${out-classes}"/>
216     <move file="optimized" tofile="${out-classes}"/>
217   </target>
218
219   <!-- Convert this project's .class files into .dex files. -->
220   <target name="dex" depends="compile">
221     <echo>Converting compiled files and external libraries into ${out-folder}/${dex-file}...</echo>
222     <apply executable="${dx}" failonerror="true" parallel="true">
223       <arg value="-JXmx256M"/>
224       <arg value="--dex" />
225       <arg value="--output=${intermediate-dex-location}" />
226       <arg path="${out-classes-location}" />
227       <fileset dir="${external-libs-folder}" includes="*.jar"/>
228     </apply>
229   </target>
230
231   <!-- Put the project's resources into the output package file
232   This actually can create multiple resource package in case
233   Some custom apk with specific configuration have been
234   declared in default.properties.
235   -->
236   <target name="package-resources">
237     <echo>Packaging resources</echo>
238     <aaptexec executable="${aapt}"
239               command="package"
240               manifest="AndroidManifest.xml"
241               resources="${resource-folder}"
242               assets="${asset-folder}"
243               androidjar="${android-jar}"
244               outfolder="${out-folder}"
245               basename="${ant.project.name}" />
246   </target>
247
248   <!--
249   Getting an error like this?
250
251    [apply] UNEXPECTED TOP-LEVEL EXCEPTION:
252    [apply] com.android.dx.cf.code.SimException: local variable type
253    mismatch: attempt to set or access a value of type int using a local
254    variable of type com.google.zxing.qrcode.decoder.Version. This is
255    symptomatic of .class transformation tools that ignore local variable
256    information.
257
258   Build core/ with the 'build-no-debug' target. It's a long story.
259   -->
260
261   <!-- Package the application and sign it with a debug key.
262   This is the default target when building. It is used for debug. -->
263   <target name="debug" depends="dex, package-resources">
264     <apkbuilder
265         outfolder="${out-folder}"
266         basename="${ant.project.name}"
267         signed="true"
268         verbose="false">
269       <file path="${intermediate-dex}" />
270       <sourcefolder path="${source-folder}" />
271       <jarfolder path="${external-libs-folder}" />
272       <nativefolder path="${native-libs-folder}" />
273     </apkbuilder>
274     <copy file="${out-folder}/BarcodeScanner-debug.apk" tofile="${out-folder}/temp.apk" overwrite="true"/>
275     <exec executable="${android-tools}/zipalign">
276       <arg value="-f"/>
277       <arg value="-v"/>
278       <arg value="4"/>
279       <arg value="${out-folder}/temp.apk"/>      
280       <arg value="${out-folder}/BarcodeScanner-debug.apk"/>
281     </exec>
282   </target>
283
284   <!-- Package the application without signing it.
285   This allows for the application to be signed later with an official publishing key. -->
286   <target name="release" depends="dex, package-resources">
287     <apkbuilder
288         outfolder="${out-folder}"
289         basename="${ant.project.name}"
290         signed="false"
291         verbose="false">
292       <file path="${intermediate-dex}" />
293       <sourcefolder path="${source-folder}" />
294       <jarfolder path="${external-libs-folder}" />
295       <nativefolder path="${native-libs-folder}" />
296     </apkbuilder>
297     <echo>All generated packages need to be signed with jarsigner before they are published.</echo>
298     <echo>Also run zipalign -f -v 4 BarcodeScanner.apk BarcodeScanner-aligned.apk after signing</echo>
299   </target>
300
301   <!-- Install the package on the default emulator -->
302   <target name="install" depends="debug">
303     <echo>Installing ${out-debug-package} onto default emulator...</echo>
304     <exec executable="${adb}" failonerror="true">
305       <arg value="install" />
306       <arg path="${out-debug-package}" />
307     </exec>
308   </target>
309
310   <target name="reinstall" depends="debug">
311     <echo>Installing ${out-debug-package} onto default emulator...</echo>
312     <exec executable="${adb}" failonerror="true">
313       <arg value="install" />
314       <arg value="-r" />
315       <arg path="${out-debug-package}" />
316     </exec>
317   </target>
318
319   <!-- Uinstall the package from the default emulator -->
320   <target name="uninstall">
321     <echo>Uninstalling ${application-package} from the default emulator...</echo>
322     <exec executable="${adb}" failonerror="true">
323       <arg value="uninstall" />
324       <arg value="${application-package}" />
325     </exec>
326   </target>
327
328   <target name="help">
329     <echo>Android Ant Build. Available targets:</echo>
330     <echo>   help:      Displays this help.</echo>
331     <echo>   debug:     Builds the application and sign it with a debug key.</echo>
332     <echo>   release:   Builds the application. The generated apk file must be</echo>
333     <echo>              signed before it is published.</echo>
334     <echo>   install:   Installs the debug package onto a running emulator or</echo>
335     <echo>              device. This can only be used if the application has </echo>
336     <echo>              not yet been installed.</echo>
337     <echo>   reinstall: Installs the debug package on a running emulator or</echo>
338     <echo>              device that already has the application.</echo>
339     <echo>              The signatures must match.</echo>
340     <echo>   uninstall: uninstall the application from a running emulator or</echo>
341     <echo>              device.</echo>
342   </target>
343
344   <target name="clean">
345     <delete dir="${out-folder}"/>
346     <delete dir="${gen-folder}"/>
347   </target>
348 </project>