aboutsummaryrefslogtreecommitdiffstats
path: root/files/ant/build.xml
blob: 1f85f5e609c38bc70af3f617b69f89a0b13e3a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
<?xml version="1.0" encoding="UTF-8"?>
<project name="android_rules" default="debug">

    <!--
        This build file is imported by the project build file. It contains
        all the targets and tasks necessary to build Android projects, be they
        regular projects, library projects, or test projects.

        At the beginning of the file is a list of properties that can be overridden
        by adding them to your ant.properties (properties are immutable, so their
        first definition sticks and is never changed).

        Follows:
        - custom task definitions,
        - more properties (do not override those unless the whole build system is modified).
        - macros used throughout the build,
        - base build targets,
        - debug-specific build targets,
        - release-specific build targets,
        - instrument-specific build targets,
        - test project-specific build targets,
        - install targets,
        - help target
    -->

    <!-- ******************************************************* -->
    <!-- **************** Overridable Properties *************** -->
    <!-- ******************************************************* -->

    <!-- You can override these values in your build.xml or ant.properties.
         Overriding any other properties may result in broken build. -->

    <!-- Tells adb which device to target. You can change this from the command line
         by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
         the emulator. -->
    <property name="adb.device.arg" value="" />

    <!-- fileset exclude patterns (space separated) to prevent
         files inside src/ from being packaged. -->
    <property name="android.package.excludes" value="" />

    <!-- set some properties used for filtering/override. If those weren't defined
         before, then this will create them with empty values, which are then ignored
         by the custom tasks receiving them. -->
    <property name="version.code" value="" />
    <property name="version.name" value="" />
    <property name="aapt.resource.filter" value="" />
    <!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
         Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

         Overall patterns syntax is:
           [!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...

         - The first character flag ! avoids printing a warning.
         - Pattern can have the flag "<dir>" to match only directories
           or "<file>" to match only files. Default is to match both.
         - Match is not case-sensitive.
    -->
    <property name="aapt.ignore.assets" value="" />

    <!-- compilation options -->
    <property name="java.encoding" value="UTF-8" />
    <property name="java.target" value="1.5" />
    <property name="java.source" value="1.5" />
    <property name="java.compilerargs" value="" />

    <!-- Renderscript options -->
    <property name="renderscript.debug.opt.level" value="O0" />
    <property name="renderscript.release.opt.level" value="O3" />

    <!-- manifest merger default value -->
    <property name="manifestmerger.enabled" value="false" />

    <!-- instrumentation options -->
    <property name="emma.filter" value="" />

    <!-- Verbosity -->
    <property name="verbose" value="false" />

    <!-- ******************************************************* -->
    <!-- ********************* Custom Tasks ******************** -->
    <!-- ******************************************************* -->

    <!-- jar file from where the tasks are loaded -->
    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
    </path>

    <!-- Custom tasks -->
    <taskdef resource="anttasks.properties" classpathref="android.antlibs" />

    <!-- Emma configuration -->
    <property name="emma.dir" value="${sdk.dir}/tools/lib" />
    <path id="emma.lib">
        <pathelement location="${emma.dir}/emma.jar" />
        <pathelement location="${emma.dir}/emma_ant.jar" />
    </path>
    <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
    <!-- End of emma configuration -->


    <!-- ******************************************************* -->
    <!-- ******************* Other Properties ****************** -->
    <!-- ******************************************************* -->
    <!-- overriding these properties may break the build
         unless the whole file is updated -->

    <!-- Input directories -->
    <property name="source.dir" value="src" />
    <property name="source.absolute.dir" location="${source.dir}" />
    <property name="gen.absolute.dir" location="gen" />
    <property name="resource.absolute.dir" location="res" />
    <property name="asset.dir" value="assets" />
    <property name="asset.absolute.dir" location="${asset.dir}" />
    <property name="jar.libs.dir" value="libs" />
    <property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
    <property name="native.libs.absolute.dir" location="libs" />

    <property name="manifest.file" value="AndroidManifest.xml" />
    <property name="manifest.abs.file" location="${manifest.file}" />

    <!-- Output directories -->
    <property name="out.dir" value="bin" />
    <property name="out.absolute.dir" location="${out.dir}" />
    <property name="out.classes.absolute.dir" location="${out.dir}/classes" />
    <property name="out.res.absolute.dir" location="${out.dir}/res" />
    <property name="out.manifest.abs.file" location="${out.dir}/AndroidManifest.xml" />

    <!-- tools location -->
    <property name="android.tools.dir" location="${sdk.dir}/tools" />
    <property name="android.platform.tools.dir" location="${sdk.dir}/platform-tools" />
    <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
    <condition property="bat" value=".bat" else=""><os family="windows" /></condition>
    <property name="adb" location="${android.platform.tools.dir}/adb${exe}" />
    <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
    <property name="aidl" location="${android.platform.tools.dir}/aidl${exe}" />
    <property name="aapt" location="${android.platform.tools.dir}/aapt${exe}" />
    <property name="dx" location="${android.platform.tools.dir}/dx${bat}" />
    <property name="renderscript" location="${android.platform.tools.dir}/llvm-rs-cc${exe}"/>

    <!-- Renderscript include Path -->
    <path id="android.renderscript.include.path">
        <pathelement location="${android.platform.tools.dir}/renderscript/include" />
        <pathelement location="${android.platform.tools.dir}/renderscript/clang-include" />
    </path>

    <!-- Intermediate files -->
    <property name="dex.file.name" value="classes.dex" />
    <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />
    <property name="resource.package.file.name" value="${ant.project.name}.ap_" />

    <!-- Build property file -->
    <property name="out.build.prop.file" location="${out.absolute.dir}/build.prop" />


    <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'
         The property 'verbosity' is not user configurable and depends exclusively on 'verbose'
         value.-->
    <condition property="verbosity" value="verbose" else="quiet">
        <istrue value="${verbose}" />
    </condition>

    <!-- properties for signing in release mode -->
    <condition property="has.keystore">
        <and>
            <isset property="key.store" />
            <length string="${key.store}" when="greater" length="0" />
            <isset property="key.alias" />
        </and>
    </condition>
    <condition property="has.password">
        <and>
            <isset property="has.keystore" />
            <isset property="key.store.password" />
            <isset property="key.alias.password" />
        </and>
    </condition>

    <!-- properties for packaging -->
    <property name="build.packaging.nocrunch" value="true" />

    <!-- whether we need to fork javac.
         This is only needed on Windows when running Java < 7 -->
    <condition else="false" property="need.javac.fork">
        <and>
            <matches pattern="1\.[56]" string="${java.specification.version}"/>
            <not>
                <os family="unix"/>
            </not>
        </and>
    </condition>

    <!-- ******************************************************* -->
    <!-- ************************ Macros *********************** -->
    <!-- ******************************************************* -->

    <!-- macro to do a task on if project.is.library is false.
         elseText attribute is displayed otherwise -->
    <macrodef name="do-only-if-not-library">
        <attribute name="elseText" />
        <element name="task-to-do" implicit="yes" />
        <sequential>
        <if condition="${project.is.library}">
            <else>
                <task-to-do />
            </else>
            <then>
                <echo level="info">@{elseText}</echo>
            </then>
        </if>
        </sequential>
    </macrodef>

    <!-- macro to do a task on if manifest.hasCode is true.
         elseText attribute is displayed otherwise -->
    <macrodef name="do-only-if-manifest-hasCode">
        <attribute name="elseText" default=""/>
        <element name="task-to-do" implicit="yes" />
        <sequential>
        <if condition="${manifest.hasCode}">
            <then>
                <task-to-do />
            </then>
            <else>
                <if>
                    <condition>
                        <length string="@{elseText}" trim="true" when="greater" length="0" />
                    </condition>
                    <then>
                        <echo level="info">@{elseText}</echo>
                    </then>
                </if>
            </else>
        </if>
        </sequential>
    </macrodef>


    <!-- Configurable macro, which allows to pass as parameters output directory,
         output dex filename and external libraries to dex (optional) -->
    <macrodef name="dex-helper">
        <element name="external-libs" optional="yes" />
        <attribute name="nolocals" default="false" />
        <sequential>
            <!-- sets the primary input for dex. If a pre-dex task sets it to
                 something else this has no effect -->
            <property name="out.dex.input.absolute.dir" value="${out.classes.absolute.dir}" />

            <!-- set the secondary dx input: the project (and library) jar files
                 If a pre-dex task sets it to something else this has no effect -->
            <if>
                <condition>
                    <isreference refid="out.dex.jar.input.ref" />
                </condition>
                <else>
                    <path id="out.dex.jar.input.ref">
                        <path refid="project.all.jars.path" />
                    </path>
                </else>
            </if>

            <dex executable="${dx}"
                    output="${intermediate.dex.file}"
                    nolocals="@{nolocals}"
                    verbose="${verbose}">
                <path path="${out.dex.input.absolute.dir}"/>
                <path refid="out.dex.jar.input.ref" />
                <external-libs />
            </dex>
        </sequential>
    </macrodef>

    <!-- This is macro that enable passing variable list of external jar files to ApkBuilder
         Example of use:
         <package-helper>
             <extra-jars>
                <jarfolder path="my_jars" />
                <jarfile path="foo/bar.jar" />
                <jarfolder path="your_jars" />
             </extra-jars>
         </package-helper> -->
    <macrodef name="package-helper">
        <element name="extra-jars" optional="yes" />
        <sequential>
            <apkbuilder
                    outfolder="${out.absolute.dir}"
                    resourcefile="${resource.package.file.name}"
                    apkfilepath="${out.packaged.file}"
                    debugpackaging="${build.is.packaging.debug}"
                    debugsigning="${build.is.signing.debug}"
                    verbose="${verbose}"
                    hascode="${manifest.hasCode}"
                    previousBuildType="${build.last.is.packaging.debug}/${build.last.is.signing.debug}"
                    buildType="${build.is.packaging.debug}/${build.is.signing.debug}">
                <dex path="${intermediate.dex.file}"/>
                <sourcefolder path="${source.absolute.dir}"/>
                <jarfile refid="project.all.jars.path" />
                <nativefolder path="${native.libs.absolute.dir}" />
                <nativefolder refid="project.library.native.folder.path" />
                <extra-jars/>
            </apkbuilder>
        </sequential>
    </macrodef>

    <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
         debug, -debug-with-emma and release.-->
    <macrodef name="zipalign-helper">
        <attribute name="in.package" />
        <attribute name="out.package" />
        <sequential>
            <zipalign
                    executable="${zipalign}"
                    input="@{in.package}"
                    output="@{out.package}"
                    verbose="${verbose}" />
        </sequential>
    </macrodef>

    <macrodef name="run-tests-helper">
        <attribute name="emma.enabled" default="false" />
        <element name="extra-instrument-args" optional="yes" />
        <sequential>
            <echo level="info">Running tests ...</echo>
            <exec executable="${adb}" failonerror="true">
                <arg line="${adb.device.arg}" />
                <arg value="shell" />
                <arg value="am" />
                <arg value="instrument" />
                <arg value="-w" />
                <arg value="-e" />
                <arg value="coverage" />
                <arg value="@{emma.enabled}" />
                <extra-instrument-args />
                <arg value="${project.app.package}/${test.runner}" />
            </exec>
        </sequential>
    </macrodef>

    <macrodef name="record-build-key">
        <attribute name="key" default="false" />
        <attribute name="value" default="false" />
        <sequential>
            <propertyfile file="${out.build.prop.file}" comment="Last build type">
                <entry key="@{key}" value="@{value}"/>
            </propertyfile>
        </sequential>
    </macrodef>

    <macrodef name="record-build-info">
        <sequential>
            <record-build-key key="build.last.target" value="${build.target}" />
            <record-build-key key="build.last.is.instrumented" value="${build.is.instrumented}" />
            <record-build-key key="build.last.is.packaging.debug" value="${build.is.packaging.debug}" />
            <record-build-key key="build.last.is.signing.debug" value="${build.is.signing.debug}" />
        </sequential>
    </macrodef>

    <macrodef name="uninstall-helper">
        <attribute name="app.package" default="false" />
        <sequential>
            <echo level="info">Uninstalling @{app.package} from the default emulator or device...</echo>
            <exec executable="${adb}" failonerror="true">
                <arg line="${adb.device.arg}" />
                <arg value="uninstall" />
                <arg value="@{app.package}" />
            </exec>
        </sequential>
    </macrodef>

    <!-- ******************************************************* -->
    <!-- ******************** Build Targets ******************** -->
    <!-- ******************************************************* -->

    <!-- Basic Ant + SDK check -->
    <target name="-check-env">
        <checkenv />
    </target>

    <!-- target to disable building dependencies -->
    <target name="nodeps">
        <property name="dont.do.deps" value="true" />
    </target>

    <!-- generic setup -->
    <target name="-setup" depends="-check-env">
        <echo level="info">Project Name: ${ant.project.name}</echo>
        <gettype projectTypeOut="project.type" />

        <!-- sets a few boolean based on project.type
             to make the if task easier -->
        <condition property="project.is.library" value="true" else="false">
            <equals arg1="${project.type}" arg2="library" />
        </condition>
        <condition property="project.is.test" value="true" else="false">
            <equals arg1="${project.type}" arg2="test" />
        </condition>
        <condition property="project.is.testapp" value="true" else="false">
            <equals arg1="${project.type}" arg2="test-app" />
        </condition>

        <!-- If a test project, resolve absolute path to tested project. -->
        <if condition="${project.is.test}">
            <then>
                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
            </then>
        </if>

        <!-- If the "debug" build type changed, clear out the compiled code.
             This is to make sure the new BuildConfig.DEBUG value is picked up
             as javac can't deal with this type of change in its dependency computation. -->
        <if>
            <condition>
                <and>
                    <length string="${build.last.is.packaging.debug}" trim="true" when="greater" length="0" />
                    <not><equals
                            arg1="${build.is.packaging.debug}"
                            arg2="${build.last.is.packaging.debug}" /></not>
                </and>
            </condition>
            <then>
                <echo level="info">Switching between debug and non debug build: Deleting previous compilation output...</echo>
                <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
            </then>
            <else>
                <!-- Else, we may still need to clean the code, for another reason.
                     special case for instrumented: if the previous build was
                     instrumented but not this one, clear out the compiled code -->
                <if>
                    <condition>
                        <and>
                            <istrue value="${build.last.is.instrumented}" />
                            <isfalse value="${build.is.instrumented}" />
                        </and>
                    </condition>
                    <then>
                        <echo level="info">Switching from instrumented to non-instrumented build: Deleting previous compilation output...</echo>
                        <delete dir="${out.classes.absolute.dir}" verbose="${verbose}" />
                    </then>
                </if>
            </else>
        </if>


        <!-- get the project manifest package -->
        <xpath input="${manifest.abs.file}"
                expression="/manifest/@package" output="project.app.package" />

    </target>

    <!-- empty default pre-clean target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-pre-clean"/>

    <!-- clean target -->
    <target name="clean" depends="-setup, -pre-clean"
            description="Removes output files created by other targets.">
        <delete dir="${out.absolute.dir}" verbose="${verbose}" />
        <delete dir="${gen.absolute.dir}" verbose="${verbose}" />

        <!-- if we know about a tested project or libraries, we clean them too. -->
        <if condition="${project.is.test}">
            <then>
                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />
                <subant failonerror="true">
                    <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
                    <target name="clean" />
                </subant>
            </then>
        </if>

        <!-- get all the libraries -->
        <if>
            <condition><not><isset property="dont.do.deps" /></not></condition>
            <then>
                <getlibs libraryFolderPathOut="project.library.folder.path" />
                <if>
                    <condition>
                        <isreference refid="project.library.folder.path" />
                    </condition>
                    <then>
                        <!-- clean the libraries with nodeps since we already
                             know about all the libraries even the indirect one -->
                        <subant
                                buildpathref="project.library.folder.path"
                                antfile="build.xml"
                                failonerror="true">
                            <target name="nodeps" />
                            <target name="clean" />
                        </subant>
                    </then>
                </if>
            </then>
        </if>
    </target>

    <!-- Pre build setup -->
    <target name="-build-setup" depends="-setup">

        <!-- read the previous build mode -->
        <property file="${out.build.prop.file}" />
        <!-- if empty the props won't be set, meaning it's a new build.
             To force a build, set the prop to empty values. -->
        <property name="build.last.target" value="" />
        <property name="build.last.is.instrumented" value="" />
        <property name="build.last.is.packaging.debug" value="" />
        <property name="build.last.is.signing.debug" value="" />

        <echo level="info">Resolving Build Target for ${ant.project.name}...</echo>
        <!-- load project properties, resolve Android target, library dependencies
             and set some properties with the results.
             All property names are passed as parameters ending in -Out -->
        <gettarget
                androidJarFileOut="project.target.android.jar"
                androidAidlFileOut="project.target.framework.aidl"
                bootClassPathOut="project.target.class.path"
                targetApiOut="project.target.apilevel"
                minSdkVersionOut="project.minSdkVersion" />

        <!-- Value of the hasCode attribute (Application node) extracted from manifest file -->
        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:hasCode"
                    output="manifest.hasCode" default="true"/>

        <echo level="info">----------</echo>
        <echo level="info">Creating output directories if needed...</echo>
        <mkdir dir="${resource.absolute.dir}" />
        <mkdir dir="${jar.libs.absolute.dir}" />
        <mkdir dir="${out.absolute.dir}" />
        <mkdir dir="${out.res.absolute.dir}" />
        <do-only-if-manifest-hasCode>
            <mkdir dir="${gen.absolute.dir}" />
            <mkdir dir="${out.classes.absolute.dir}" />
        </do-only-if-manifest-hasCode>

        <echo level="info">----------</echo>
        <echo level="info">Resolving Dependencies for ${ant.project.name}...</echo>
        <dependency
                libraryFolderPathOut="project.library.folder.path"
                libraryPackagesOut="project.library.packages"
                libraryManifestFilePathOut="project.library.manifest.file.path"
                libraryResFolderPathOut="project.library.res.folder.path"
                libraryNativeFolderPathOut="project.library.native.folder.path"
                jarLibraryPathOut="project.all.jars.path"
                targetApi="${project.target.apilevel}"
                verbose="${verbose}" />

        <!-- compile the libraries if any -->
        <if>
            <condition>
                <and>
                    <isreference refid="project.library.folder.path" />
                    <not><isset property="dont.do.deps" /></not>
                </and>
            </condition>
            <then>
                <!-- figure out which target must be used to build the library projects.
                     If emma is enabled, then use 'instrument' otherwise, use 'debug' -->
                <condition property="project.libraries.target" value="instrument" else="${build.target}">
                    <istrue value="${build.is.instrumented}" />
                </condition>

                <echo level="info">----------</echo>
                <echo level="info">Building Libraries with '${project.libraries.target}'...</echo>

                <!-- no need to build the deps as we have already
                     the full list of libraries -->
                <subant failonerror="true"
                        buildpathref="project.library.folder.path"
                        antfile="build.xml">
                    <target name="nodeps" />
                    <target name="${project.libraries.target}" />
                </subant>
            </then>
        </if>

        <!-- compile the main project if this is a test project -->
        <if condition="${project.is.test}">
            <then>
                <!-- figure out which target must be used to build the tested project.
                     If emma is enabled, then use 'instrument' otherwise, use 'debug' -->
                <condition property="tested.project.target" value="instrument" else="debug">
                    <isset property="emma.enabled" />
                </condition>

                <echo level="info">----------</echo>
                <echo level="info">Building tested project at ${tested.project.absolute.dir} with '${tested.project.target}'...</echo>
                <subant target="${tested.project.target}" failonerror="true">
                    <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
                </subant>

                <!-- get the tested project full classpath to be able to build
                     the test project -->
                <testedprojectclasspath
                        projectLocation="${tested.project.absolute.dir}"
                        projectClassPathOut="tested.project.classpath"/>
            </then>
            <else>
                <!-- no tested project, make an empty Path object so that javac doesn't
                     complain -->
                <path id="tested.project.classpath" />
            </else>
        </if>
    </target>

    <!-- empty default pre-build target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-pre-build"/>

    <!-- Code Generation: compile resources (aapt -> R.java), aidl, renderscript -->
    <target name="-code-gen">
        <!-- always merge manifest -->
        <mergemanifest
                appManifest="${manifest.abs.file}"
                outManifest="${out.manifest.abs.file}"
                enabled="${manifestmerger.enabled}">
            <library refid="project.library.manifest.file.path" />
        </mergemanifest>

        <do-only-if-manifest-hasCode
                elseText="hasCode = false. Skipping aidl/renderscript/R.java">
            <echo level="info">Handling aidl files...</echo>
            <aidl executable="${aidl}" framework="${project.target.framework.aidl}"
                    genFolder="${gen.absolute.dir}">
                <source path="${source.absolute.dir}"/>
            </aidl>

            <!-- renderscript generates resources so it must be called before aapt -->
            <echo level="info">----------</echo>
            <echo level="info">Handling RenderScript files...</echo>
            <renderscript executable="${renderscript}"
                    includePath="${android.renderscript.include.path}"
                    genFolder="${gen.absolute.dir}"
                    resFolder="${out.res.absolute.dir}/raw"
                    targetApi="${project.minSdkVersion}"
                    optLevel="${renderscript.opt.level}"
                    buildType="${build.is.packaging.debug}"
                    previousBuildType="${build.last.is.packaging.debug}">
                <source path="${source.absolute.dir}"/>
            </renderscript>

            <echo level="info">----------</echo>
            <echo level="info">Handling Resources...</echo>
            <aapt executable="${aapt}"
                    command="package"
                    verbose="${verbose}"
                    manifest="${out.manifest.abs.file}"
                    androidjar="${project.target.android.jar}"
                    rfolder="${gen.absolute.dir}"
                    nonConstantId="${android.library}"
                    libraryResFolderPathRefid="project.library.res.folder.path"
                    libraryPackagesRefid="project.library.packages"
                    ignoreAssets="${aapt.ignore.assets}">
                <res path="${out.res.absolute.dir}" />
                <res path="${resource.absolute.dir}" />
            </aapt>

            <echo level="info">----------</echo>
            <echo level="info">Handling BuildConfig class...</echo>
            <buildconfig
                    genFolder="${gen.absolute.dir}"
                    package="${project.app.package}"
                    buildType="${build.is.packaging.debug}"
                    previousBuildType="${build.last.is.packaging.debug}"/>

        </do-only-if-manifest-hasCode>
    </target>

    <!-- empty default pre-compile target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-pre-compile"/>

    <!-- Compiles this project's .java files into .class files. -->
    <target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
        <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
            <!-- merge the project's own classpath and the tested project's classpath -->
            <path id="project.javac.classpath">
                <path refid="project.all.jars.path" />
                <path refid="tested.project.classpath" />
            </path>
            <javac encoding="${java.encoding}"
                    source="${java.source}" target="${java.target}"
                    debug="true" extdirs="" includeantruntime="false"
                    destdir="${out.classes.absolute.dir}"
                    bootclasspathref="project.target.class.path"
                    verbose="${verbose}"
                    classpathref="project.javac.classpath"
                    fork="${need.javac.fork}">
                <src path="${source.absolute.dir}" />
                <src path="${gen.absolute.dir}" />
                <compilerarg line="${java.compilerargs}" />
            </javac>

            <!-- if the project is instrumented, intrument the classes -->
            <if condition="${build.is.instrumented}">
                <then>
                    <echo level="info">Instrumenting classes from ${out.absolute.dir}/classes...</echo>

                    <!-- build the filter to remove R, Manifest, BuildConfig -->
                    <getemmafilter
                            appPackage="${project.app.package}"
                            libraryPackagesRefId="project.library.packages"
                            filterOut="emma.default.filter"/>

                    <!-- It only instruments class files, not any external libs -->
                    <emma enabled="true">
                        <instr verbosity="${verbosity}"
                               mode="overwrite"
                               instrpath="${out.absolute.dir}/classes"
                               outdir="${out.absolute.dir}/classes">
                            <filter excludes="${emma.default.filter}" />
                            <filter value="${emma.filter}" />
                        </instr>
                    </emma>
                </then>
            </if>

            <!-- if the project is a library then we generate a jar file -->
            <if condition="${project.is.library}">
                <then>
                    <echo level="info">Creating library output jar file...</echo>
                    <property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
                    <if>
                        <condition>
                            <length string="${android.package.excludes}" trim="true" when="greater" length="0" />
                        </condition>
                        <then>
                            <echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo>
                        </then>
                    </if>

                    <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" />

                    <jar destfile="${out.library.jar.file}">
                        <fileset dir="${out.classes.absolute.dir}"
                                includes="**/*.class"
                                excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/Manifest.class ${project.app.package.path}/Manifest$*.class ${project.app.package.path}/BuildConfig.class"/>
                        <fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
                    </jar>
                </then>
            </if>

        </do-only-if-manifest-hasCode>
    </target>

    <!-- empty default post-compile target. Create a similar target in
         your build.xml and it'll be called instead of this one. -->
    <target name="-post-compile"/>

    <!-- Obfuscate target
        This is only active in release builds when proguard.config is defined
        in default.properties.

        To replace Proguard with a different obfuscation engine:
        Override the following targets in your build.xml, before the call to <setup>
            -release-obfuscation-check
                Check whether obfuscation should happen, and put the result in a property.
            -debug-obfuscation-check
                Obfuscation should not happen. Set the same property to false.
            -obfuscate
                check if the property set in -debug/release-obfuscation-check is set to true.
                If true:
                    Perform obfuscation
                    Set property out.dex.input.absolute.dir to be the output of the obfuscation
    -->
    <target name="-obfuscate">
        <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" />
                <!-- input for dex will be proguard's output -->
                <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.
                     Each path must be quoted if it contains spaces.
                -->
                <pathconvert property="project.target.classpath.value" refid="project.target.class.path">
                    <firstmatchmapper>
                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
                        <identitymapper/>
                    </firstmatchmapper>
                </pathconvert>

                <!-- 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.all.classes.path">
                    <pathelement location="${preobfuscate.jar.file}" />
                    <path refid="project.all.jars.path" />
                </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.
                     Each path must be quoted if it contains spaces.
                -->
                <pathconvert property="project.all.classes.value" refid="project.all.classes.path">
                    <firstmatchmapper>
                        <regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
                        <identitymapper/>
                    </firstmatchmapper>
                </pathconvert>

                <!-- Turn the path property ${proguard.config} from an A:B:C property
                     into a series of includes: -include A -include B -include C
                     suitable for processing by the ProGuard task. Note - this does
                     not include the leading '-include "' or the closing '"'; those
                     are added under the <proguard> call below.
                -->
                <path id="proguard.configpath">
                    <pathelement path="${proguard.config}"/>
                </path>
                <pathconvert pathsep='" -include "' property="proguard.configcmd" refid="proguard.configpath"/>

                <mkdir   dir="${obfuscate.absolute.dir}" />
                <delete file="${preobfuscate.jar.file}"/>
                <delete file="${obfuscated.jar.file}"/>
                <jar basedir="${out.classes.absolute.dir}"
                    destfile="${preobfuscate.jar.file}" />
                <proguard>
                    -include      "${proguard.configcmd}"
                    -injars       ${project.all.classes.value}
                    -outjars      "${obfuscated.jar.file}"
                    -libraryjars  ${project.target.classpath.value}
                    -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>
        </if>
    </target>

    <!-- Converts this project's .class files into .dex files -->
    <target name="-dex" depends="-compile, -post-compile, -obfuscate">
        <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
            <!-- only convert to dalvik bytecode is *not* a library -->
            <do-only-if-not-library elseText="Library project: do not convert bytecode..." >
                <!-- special case for instrumented builds: need to use no-locals and need
                     to pass in the emma jar. -->
                <if condition="${build.is.instrumented}">
                    <then>
                        <dex-helper nolocals="true">
                            <external-libs>
                                <fileset file="${emma.dir}/emma_device.jar" />
                            </external-libs>
                        </dex-helper>
                    </then>
                    <else>
                        <dex-helper />
                    </else>
                </if>
            </do-only-if-not-library>
        </do-only-if-manifest-hasCode>
    </target>

<!-- Updates the pre-processed PNG cache -->
    <target name="-crunch">
        <exec executable="${aapt}" taskName="crunch">
            <arg value="crunch" />
            <arg value="-v" />
            <arg value="-S" />
            <arg path="${resource.absolute.dir}" />
            <arg value="-C" />
            <arg path="${out.res.absolute.dir}" />
        </exec>
    </target>

    <!-- Puts the project's resources into the output package file
         This actually can create multiple resource package in case
         Some custom apk with specific configuration have been
         declared in default.properties.
         -->
    <target name="-package-resources" depends="-crunch">
        <!-- only package resources if *not* a library project -->
        <do-only-if-not-library elseText="Library project: do not package resources..." >
            <aapt executable="${aapt}"
                    command="package"
                    versioncode="${version.code}"
                    versionname="${version.name}"
                    debug="${build.is.packaging.debug}"
                    manifest="${out.manifest.abs.file}"
                    assets="${asset.absolute.dir}"
                    androidjar="${project.target.android.jar}"
                    apkfolder="${out.absolute.dir}"
                    nocrunch="${build.packaging.nocrunch}"
                    resourcefilename="${resource.package.file.name}"
                    resourcefilter="${aapt.resource.filter}"
                    libraryResFolderPathRefid="project.library.res.folder.path"
                    libraryPackagesRefid="project.library.packages"
                    previousBuildType="${build.last.target}"
                    buildType="${build.target}"
                    ignoreAssets="${aapt.ignore.assets}">
                <res path="${out.res.absolute.dir}" />
                <res path="${resource.absolute.dir}" />
                <!-- <nocompress /> forces no compression on any files in assets or res/raw -->
                <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
            </aapt>
        </do-only-if-not-library>
    </target>

    <!-- Packages the application. -->
    <target name="-package" depends="-dex, -package-resources">
        <!-- only package apk if *not* a library project -->
        <do-only-if-not-library elseText="Library project: do not package apk..." >
            <if condition="${build.is.instrumented}">
                <then>
                    <package-helper>
                        <extra-jars>
                            <!-- Injected from external file -->
                            <jarfile path="${emma.dir}/emma_device.jar" />
                        </extra-jars>
                    </package-helper>
                </then>
                <else>
                    <package-helper />
                </else>
            </if>
        </do-only-if-not-library>
    </target>

    <target name="-post-package" />
    <target name="-post-build" />

    <target name="-set-mode-check">
        <fail if="build.is.mode.set"
                message="Cannot run two different modes at the same time. If you are running more than one debug/release/instrument type targets, call them from different Ant calls." />
    </target>

    <!-- ******************************************************* -->
    <!-- **************** Debug specific targets *************** -->
    <!-- ******************************************************* -->

    <target name="-set-debug-files" depends="-set-mode-check">

        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
        <property name="build.is.mode.set" value="true" />
    </target>


    <target name="-set-debug-mode" depends="-setup">
        <!-- record the current build target -->
        <property name="build.target" value="debug" />

        <if>
            <condition>
                <and>
                    <istrue value="${project.is.testapp}" />
                    <istrue value="${emma.enabled}" />
                </and>
            </condition>
            <then>
                <property name="build.is.instrumented" value="true" />
            </then>
            <else>
                <property name="build.is.instrumented" value="false" />
            </else>
        </if>

        <!-- whether the build is a debug build. always set. -->
        <property name="build.is.packaging.debug" value="true" />

        <!-- signing mode: debug -->
        <property name="build.is.signing.debug" value="true" />

        <!-- Renderscript optimization level: none -->
        <property name="renderscript.opt.level" value="${renderscript.debug.opt.level}" />

    </target>

    <target name="-debug-obfuscation-check">
        <!-- proguard is never enabled in debug mode -->
        <property name="proguard.enabled" value="false"/>
    </target>

    <!-- Builds debug output package -->
    <target name="-do-debug" depends="-set-debug-mode, -debug-obfuscation-check, -package, -post-package">
        <!-- only create apk if *not* a library project -->
        <do-only-if-not-library elseText="Library project: do not create apk..." >
            <sequential>
                <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />
                <echo level="info">Debug Package: ${out.final.file}</echo>
            </sequential>
        </do-only-if-not-library>
        <record-build-info />
    </target>

    <!-- Builds debug output package -->
    <target name="debug" depends="-set-debug-files, -do-debug, -post-build"
                description="Builds the application and signs it with a debug key.">
    </target>


    <!-- ******************************************************* -->
    <!-- *************** Release specific targets ************** -->
    <!-- ******************************************************* -->

    <!-- called through target 'release'. Only executed if the keystore and
         key alias are known but not their password. -->
    <target name="-release-prompt-for-password" if="has.keystore" unless="has.password">
        <!-- Gets passwords -->
        <input
                message="Please enter keystore password (store:${key.store}):"
                addproperty="key.store.password" />
        <input
                message="Please enter password for alias '${key.alias}':"
                addproperty="key.alias.password" />
    </target>

    <!-- called through target 'release'. Only executed if there's no
         keystore/key alias set -->
    <target name="-release-nosign" unless="has.keystore">
        <!-- no release builds for library project -->
        <do-only-if-not-library elseText="" >
            <sequential>
                <echo level="info">No key.store and key.alias properties found in build.properties.</echo>
                <echo level="info">Please sign ${out.packaged.file} manually</echo>
                <echo level="info">and run zipalign from the Android SDK tools.</echo>
            </sequential>
        </do-only-if-not-library>
        <record-build-info />
    </target>

    <target name="-release-obfuscation-check">
        <echo level="info">proguard.config is ${proguard.config}</echo>
        <condition property="proguard.enabled" value="true" else="false">
            <and>
                <isset property="build.is.mode.release" />
                <isset property="proguard.config" />
            </and>
        </condition>
        <if condition="${proguard.enabled}">
            <then>
                <echo level="info">Proguard.config is enabled</echo>
                <!-- Secondary dx input (jar files) is empty since all the
                     jar files will be in the obfuscated jar -->
                <path id="out.dex.jar.input.ref" />
            </then>
        </if>
    </target>

    <target name="-set-release-mode" depends="-set-mode-check">
        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-release-unsigned.apk" />
        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-release.apk" />
        <property name="build.is.mode.set" value="true" />

        <!-- record the current build target -->
        <property name="build.target" value="release" />

        <property name="build.is.instrumented" value="false" />

        <!-- release mode is only valid if the manifest does not explicitly
             set debuggable to true. default is false. -->
        <xpath input="${manifest.abs.file}" expression="/manifest/application/@android:debuggable"
                output="build.is.packaging.debug" default="false"/>

        <!-- signing mode: release -->
        <property name="build.is.signing.debug" value="false" />

        <!-- Renderscript optimization level: aggressive -->
        <property name="renderscript.opt.level" value="${renderscript.release.opt.level}" />

        <if condition="${build.is.packaging.debug}">
            <then>
                <echo>*************************************************</echo>
                <echo>****  Android Manifest has debuggable=true   ****</echo>
                <echo>**** Doing DEBUG packaging with RELEASE keys ****</echo>
                <echo>*************************************************</echo>
            </then>
            <else>
                <!-- property only set in release mode.
                     Useful for if/unless attributes in target node
                     when using Ant before 1.8 -->
                <property name="build.is.mode.release" value="true"/>
            </else>
        </if>
    </target>

    <target name="-release-sign" if="has.keystore" >
        <!-- only create apk if *not* a library project -->
        <do-only-if-not-library elseText="Library project: do not create apk..." >
            <sequential>
                <property name="out.unaligned.file" location="${out.absolute.dir}/${ant.project.name}-release-unaligned.apk" />

                <!-- Signs the APK -->
                <echo level="info">Signing final apk...</echo>
                <signapk
                        input="${out.packaged.file}"
                        output="${out.unaligned.file}"
                        keystore="${key.store}"
                        storepass="${key.store.password}"
                        alias="${key.alias}"
                        keypass="${key.alias.password}"/>

                <!-- Zip aligns the APK -->
                <zipalign-helper
                        in.package="${out.unaligned.file}"
                        out.package="${out.final.file}" />
                <echo level="info">Release Package: ${out.final.file}</echo>
            </sequential>
        </do-only-if-not-library>
        <record-build-info />
    </target>

    <!-- This runs -package-release and -release-nosign first and then runs
         only if release-sign is true (set in -release-check,
         called by -release-no-sign)-->
    <target name="release"
                depends="-set-release-mode, -release-obfuscation-check, -package, -post-package, -release-prompt-for-password, -release-nosign, -release-sign, -post-build"
                description="Builds the application in release mode.">
    </target>

    <!-- ******************************************************* -->
    <!-- ************ Instrumented specific targets ************ -->
    <!-- ******************************************************* -->

    <!-- These targets are specific for the project under test when it
         gets compiled by the test projects in a way that will make it
         support emma code coverage -->

    <target name="-set-instrumented-mode" depends="-set-mode-check">
        <property name="out.packaged.file" location="${out.absolute.dir}/${ant.project.name}-instrumented-unaligned.apk" />
        <property name="out.final.file" location="${out.absolute.dir}/${ant.project.name}-instrumented.apk" />
        <property name="build.is.mode.set" value="true" />

        <!-- whether the build is an instrumented build. -->
        <property name="build.is.instrumented" value="true" />
    </target>

    <!-- Builds instrumented output package -->
    <target name="instrument" depends="-set-instrumented-mode, -do-debug"
                description="Builds an instrumented packaged.">
        <!-- only create apk if *not* a library project -->
        <do-only-if-not-library elseText="Library project: do not create apk..." >
            <sequential>
                <zipalign-helper in.package="${out.packaged.file}" out.package="${out.final.file}" />
                <echo level="info">Instrumented Package: ${out.final.file}</echo>
            </sequential>
        </do-only-if-not-library>
        <record-build-info />
    </target>

    <!-- ******************************************************* -->
    <!-- ************ Test project specific targets ************ -->
    <!-- ******************************************************* -->

    <!-- enable code coverage -->
    <target name="emma">
        <property name="emma.enabled" value="true" />
    </target>

    <!-- fails if the project is not a test project -->
    <target name="-test-project-check" depends="-setup">
        <if>
            <condition>
                <and>
                    <isfalse value="${project.is.test}" />
                    <isfalse value="${project.is.testapp}" />
                </and>
            </condition>
            <then>
                <fail message="Project is not a test project." />
            </then>
        </if>
    </target>

    <target name="test" depends="-test-project-check"
                description="Runs tests from the package defined in test.package property">
        <property name="test.runner" value="android.test.InstrumentationTestRunner" />

        <if condition="${project.is.test}">
        <then>
            <property name="tested.project.absolute.dir" location="${tested.project.dir}" />

            <!-- Application package of the tested project extracted from its manifest file -->
            <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
                    expression="/manifest/@package" output="tested.project.app.package" />
        </then>
        <else>
            <!-- this is a test app, the tested package is the app's own package -->
            <property name="tested.project.app.package" value="${project.app.package}" />
        </else>
        </if>

        <property name="emma.dump.file"
                value="/data/data/${tested.project.app.package}/coverage.ec" />

        <if condition="${emma.enabled}">
            <then>
                <echo>WARNING: Code Coverage is currently only supported on the emulator and rooted devices.</echo>
                <run-tests-helper emma.enabled="true">
                    <extra-instrument-args>
                        <arg value="-e" />
                           <arg value="coverageFile" />
                           <arg value="${emma.dump.file}" />
                    </extra-instrument-args>
                </run-tests-helper>
                <echo level="info">Downloading coverage file into project directory...</echo>
                <exec executable="${adb}" failonerror="true">
                    <arg line="${adb.device.arg}" />
                    <arg value="pull" />
                    <arg value="${emma.dump.file}" />
                    <arg value="coverage.ec" />
                </exec>
                <echo level="info">Extracting coverage report...</echo>
                <emma>
                    <report sourcepath="${tested.project.absolute.dir}/${source.dir}"
                                      verbosity="${verbosity}">
                        <!-- TODO: report.dir or something like should be introduced if necessary -->
                        <infileset dir="${out.absolute.dir}">
                            <include name="coverage.ec" />
                            <include name="coverage.em" />
                        </infileset>
                        <!-- TODO: reports in other, indicated by user formats -->
                        <html outfile="coverage.html" />
                   </report>
                </emma>
                <echo level="info">Cleaning up temporary files...</echo>
                <delete file="${out.absolute.dir}/coverage.ec" />
                <delete file="${out.absolute.dir}/coverage.em" />
                <echo level="info">Saving the report file in ${basedir}/coverage/coverage.html</echo>
            </then>
            <else>
                <run-tests-helper />
            </else>
        </if>
    </target>


    <!-- ******************************************************* -->
    <!-- ********** Install/uninstall specific targets ********* -->
    <!-- ******************************************************* -->

    <target name="install"
                description="Installs the newly build package. Must be used in conjunction with a build target
                            (debug/release/instrument). If the application was previously installed, the application
                            is reinstalled if the signature matches." >
        <!-- only do install if *not* a library project -->
        <do-only-if-not-library elseText="Library project: nothing to install!" >
            <if>
                <condition>
                    <isset property="out.final.file" />
                </condition>
                <then>
                    <if>
                        <condition>
                            <resourceexists>
                                <file file="${out.final.file}"/>
                            </resourceexists>
                        </condition>
                        <then>
                            <echo level="info">Installing ${out.final.file} onto default emulator or device...</echo>
                            <exec executable="${adb}" failonerror="true">
                                <arg line="${adb.device.arg}" />
                                <arg value="install" />
                                <arg value="-r" />
                                <arg path="${out.final.file}" />
                            </exec>

                            <!-- now install the tested project if applicable -->
                            <!-- can't use project.is.test since the setup target might not have run -->
                            <if>
                                <condition>
                                    <and>
                                        <isset property="tested.project.dir" />
                                        <not>
                                            <isset property="dont.do.deps" />
                                        </not>
                                    </and>
                                </condition>
                                <then>
                                    <property name="tested.project.absolute.dir" location="${tested.project.dir}" />

                                    <!-- figure out which tested package to install based on emma.enabled -->
                                    <condition property="tested.project.install.target" value="installi" else="installd">
                                        <isset property="emma.enabled" />
                                    </condition>
                                    <subant target="${tested.project.install.target}" failonerror="true">
                                        <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
                                    </subant>
                                </then>
                            </if>
                        </then>
                        <else>
                            <fail message="File ${out.final.file} does not exist." />
                        </else>
                    </if>
                </then>
                <else>
                    <echo>Install file not specified.</echo>
                    <echo></echo>
                    <echo>'ant install' now requires the build target to be specified as well.</echo>
                    <echo></echo>
                    <echo></echo>
                    <echo>    ant debug install</echo>
                    <echo>    ant release install</echo>
                    <echo>    ant instrument install</echo>
                    <echo>This will build the given package and install it.</echo>
                    <echo></echo>
                    <echo>Alternatively, you can use</echo>
                    <echo>    ant installd</echo>
                    <echo>    ant installr</echo>
                    <echo>    ant installi</echo>
                    <echo>    ant installt</echo>
                    <echo>to only install an existing package (this will not rebuild the package.)</echo>
                    <fail />
                </else>
            </if>
        </do-only-if-not-library>
    </target>

    <target name="installd" depends="-set-debug-files, install"
            description="Installs (only) the debug package." />
    <target name="installr" depends="-set-release-mode, install"
            description="Installs (only) the release package." />
    <target name="installi" depends="-set-instrumented-mode, install"
            description="Installs (only) the instrumented package." />
    <target name="installt" depends="-test-project-check, installd"
            description="Installs (only) the test and tested packages." />


    <!-- Uninstalls the package from the default emulator/device -->
    <target name="uninstall" depends="-setup"
                description="Uninstalls the application from a running emulator or device.">
        <if>
            <condition>
                <isset property="project.app.package" />
            </condition>
            <then>
                <uninstall-helper app.package="${project.app.package}" />
            </then>
            <else>
                <fail message="Could not find application package in manifest. Cannot run 'adb uninstall'." />
            </else>
        </if>

        <!-- Now uninstall the tested project, if applicable -->
        <if>
            <condition>
                <and>
                    <istrue value="${project.is.test}" />
                    <not>
                        <isset property="dont.do.deps" />
                    </not>
                </and>
            </condition>
            <then>
                <property name="tested.project.absolute.dir" location="${tested.project.dir}" />

                <!-- Application package of the tested project extracted from its manifest file -->
                <xpath input="${tested.project.absolute.dir}/AndroidManifest.xml"
                    expression="/manifest/@package" output="tested.project.app.package" />
                <if>
                    <condition>
                        <isset property="tested.project.app.package" />
                    </condition>
                    <then>
                        <uninstall-helper app.package="${tested.project.app.package}" />
                    </then>
                    <else>
                        <fail message="Could not find tested application package in manifest. Cannot run 'adb uninstall'." />
                    </else>
                </if>
            </then>
        </if>

    </target>


    <!-- ******************************************************* -->
    <!-- ************************* Help ************************ -->
    <!-- ******************************************************* -->

    <target name="help">
        <!-- displays starts at col 13
              |13                                                              80| -->
        <echo>Android Ant Build. Available targets:</echo>
        <echo>   help:      Displays this help.</echo>
        <echo>   clean:     Removes output files created by other targets.</echo>
        <echo>              The 'all' target can be used to clean dependencies</echo>
        <echo>              (tested projects and libraries)at the same time</echo>
        <echo>              using: 'ant all clean'</echo>
        <echo>   debug:     Builds the application and signs it with a debug key.</echo>
        <echo>              The 'nodeps' target can be used to only build the</echo>
        <echo>              current project and ignore the libraries using:</echo>
        <echo>              'ant nodeps debug'</echo>
        <echo>   release:   Builds the application. The generated apk file must be</echo>
        <echo>              signed before it is published.</echo>
        <echo>              The 'nodeps' target can be used to only build the</echo>
        <echo>              current project and ignore the libraries using:</echo>
        <echo>              'ant nodeps release'</echo>
        <echo>   instrument:Builds an instrumented package and signs it with a</echo>
        <echo>              debug key.</echo>
        <echo>   test:      Runs the tests. Project must be a test project and</echo>
        <echo>              must have been built. Typical usage would be:</echo>
        <echo>                  ant [emma] debug install test</echo>
        <echo>   emma:      Transiently enables code coverage for subsequent</echo>
        <echo>              targets.</echo>
        <echo>   install:   Installs the newly build package. Must either be used</echo>
        <echo>              in conjunction with a build target (debug/release/</echo>
        <echo>              instrument) or with the proper suffix indicating</echo>
        <echo>              which package to install (see below).</echo>
        <echo>              If the application was previously installed, the</echo>
        <echo>              application is reinstalled if the signature matches.</echo>
        <echo>   installd:  Installs (only) the debug package.</echo>
        <echo>   installr:  Installs (only) the release package.</echo>
        <echo>   installi:  Installs (only) the instrumented package.</echo>
        <echo>   installt:  Installs (only) the test and tested packages (unless</echo>
        <echo>              nodeps is used as well.</echo>
        <echo>   uninstall: Uninstalls the application from a running emulator or</echo>
        <echo>              device. Also uninstall tested package if applicable</echo>
        <echo>              unless 'nodeps' is used as well.</echo>
    </target>
</project>