android使用ant编译打包

You need to download ant, proguard and configure them first.

Then you have to genarate a build.xml file use the following command:

$android update project --path

path is the full path of your android project, eg:~/code/workspace/ENotes

now you can see there's a build.xml file under ~/code/workspace/ENotes.

Then you have to configure your build.xml file. Including sign, optimize, align.

Here's my example build.xml file.

   1 <?xml version="1.0" encoding="UTF-8"?>
   2 
   3 <project name="ENotes" default="help">
   4 
   5 
   6 
   7     <!-- The local.properties file is created and updated by the 'android' tool.
   8 
   9          It contains the path to the SDK. It should *NOT* be checked in in Version
  10 
  11          Control Systems. -->
  12 
  13     <property file="local.properties" />
  14 
  15 
  16 
  17     <!-- The build.properties file can be created by you and is never touched
  18 
  19          by the 'android' tool. This is the place to change some of the default property values
  20 
  21          used by the Ant rules.
  22 
  23          Here are some properties you may want to change/update:
  24 
  25 
  26 
  27          application.package
  28 
  29              the name of your application package as defined in the manifest. Used by the
  30 
  31              'uninstall' rule.
  32 
  33          source.dir
  34 
  35              the name of the source directory. Default is 'src'.
  36 
  37          out.dir
  38 
  39              the name of the output directory. Default is 'bin'.
  40 
  41 
  42 
  43          Properties related to the SDK location or the project target should be updated
  44 
  45           using the 'android' tool with the 'update' action.
  46 
  47 
  48 
  49          This file is an integral part of the build system for your application and
  50 
  51          should be checked in in Version Control Systems.
  52 
  53 
  54 
  55          -->
  56 
  57     <property file="build.properties" />
  58 
  59 
  60 
  61     <!-- The default.properties file is created and updated by the 'android' tool, as well
  62 
  63          as ADT.
  64 
  65          This file is an integral part of the build system for your application and
  66 
  67          should be checked in in Version Control Systems. -->
  68 
  69     <property file="default.properties" />
  70 
  71 
  72 
  73     <!-- Custom Android task to deal with the project target, and import the proper rules.
  74 
  75          This requires ant 1.6.0 or above. -->
  76 
  77     <path id="android.antlibs">
  78 
  79         <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
  80 
  81         <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
  82 
  83         <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
  84 
  85         <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
  86 
  87         <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
  88 
  89     </path>
  90 
  91 
  92 
  93     <taskdef name="setup"
  94 
  95         classname="com.android.ant.SetupTask"
  96 
  97         classpathref="android.antlibs" />
  98 
  99 
 100 
 101     <!-- Execute the Android Setup task that will setup some properties specific to the target,
 102 
 103          and import the build rules files.
 104 
 105 
 106 
 107          The rules file is imported from
 108 
 109             <SDK>/platforms/<target_platform>/templates/android_rules.xml
 110 
 111 
 112 
 113          To customize some build steps for your project:
 114 
 115          - copy the content of the main node <project> from android_rules.xml
 116 
 117          - paste it in this build.xml below the <setup /> task.
 118 
 119          - disable the import by changing the setup task below to <setup import="false" />
 120 
 121 
 122 
 123          This will ensure that the properties are setup correctly but that your customized
 124 
 125          build steps are used.
 126 
 127     -->
 128 
 129     <setup import="false" />
 130 
 131 
 132 
 133       <!-- Custom tasks -->
 134 
 135     <taskdef name="aaptexec"
 136 
 137         classname="com.android.ant.AaptExecLoopTask"
 138 
 139         classpathref="android.antlibs" />
 140 
 141 
 142 
 143     <taskdef name="apkbuilder"
 144 
 145         classname="com.android.ant.ApkBuilderTask"
 146 
 147         classpathref="android.antlibs" />
 148 
 149 
 150 
 151     <taskdef name="xpath"
 152 
 153         classname="com.android.ant.XPathTask"
 154 
 155         classpathref="android.antlibs" />
 156 
 157 
 158 
 159     <!-- Properties -->
 160 
 161 
 162 
 163     <!-- Tells adb which device to target. You can change this from the command line
 164 
 165          by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for
 166 
 167          the emulator. -->
 168 
 169     <property name="adb.device.arg" value="" />
 170 
 171 
 172 
 173     <property name="android.tools.dir" location="${sdk.dir}/tools" />
 174 
 175     <!-- Name of the application package extracted from manifest file -->
 176 
 177     <xpath input="AndroidManifest.xml" expression="/manifest/@package"
 178 
 179                 output="manifest.package" />
 180 
 181 
 182 
 183     <!-- Input directories -->
 184 
 185     <property name="source.dir" value="src" />
 186 
 187     <property name="source.absolute.dir" location="${source.dir}" />
 188 
 189     <property name="gen.dir" value="gen" />
 190 
 191     <property name="gen.absolute.dir" location="${gen.dir}" />
 192 
 193     <property name="resource.dir" value="res" />
 194 
 195     <property name="resource.absolute.dir" location="${resource.dir}" />
 196 
 197     <property name="asset.dir" value="assets" />
 198 
 199     <property name="asset.absolute.dir" location="${asset.dir}" />
 200 
 201 
 202 
 203     <!-- Directory for the third party java libraries -->
 204 
 205     <property name="external.libs.dir" value="libs" />
 206 
 207     <property name="external.libs.absolute.dir" location="${external.libs.dir}" />
 208 
 209 
 210 
 211     <!-- Directory for the native libraries -->
 212 
 213     <property name="native.libs.dir" value="libs" />
 214 
 215     <property name="native.libs.absolute.dir" location="${native.libs.dir}" />
 216 
 217 
 218 
 219     <!-- Output directories -->
 220 
 221     <property name="out.dir" value="bin" />
 222 
 223     <property name="out.absolute.dir" location="${out.dir}" />
 224 
 225     <property name="out.classes.dir" value="${out.absolute.dir}" />
 226 
 227     <property name="out.classes.absolute.dir" location="${out.classes.dir}" />
 228 
 229 
 230 
 231     <!-- Intermediate files -->
 232 
 233     <property name="dex.file.name" value="classes.dex" />
 234 
 235     <property name="intermediate.dex.file" location="${out.absolute.dir}/${dex.file.name}" />
 236 
 237 
 238 
 239     <!-- The final package file to generate -->
 240 
 241     <property name="out.debug.unaligned.package"
 242 
 243                   location="${out.absolute.dir}/${ant.project.name}-debug-unaligned.apk" />
 244 
 245     <property name="out.debug.package"
 246 
 247                   location="${out.absolute.dir}/${ant.project.name}-debug.apk" />
 248 
 249     <property name="out.unsigned.package"
 250 
 251                   location="${out.absolute.dir}/${ant.project.name}-unsigned.apk" />
 252 
 253     <property name="out.unaligned.package"
 254 
 255                   location="${out.absolute.dir}/${ant.project.name}-unaligned.apk" />
 256 
 257     <property name="out.release.package"
 258 
 259                   location="${out.absolute.dir}/${ant.project.name}-release.apk" />
 260 
 261 
 262 
 263     <!-- Verbosity -->
 264 
 265     <property name="verbose" value="false" />
 266 
 267     <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false'
 268 
 269          The property 'verbosity' is not user configurable and depends exclusively on 'verbose'
 270 
 271          value.-->
 272 
 273     <condition property="verbosity" value="verbose" else="quiet">
 274 
 275         <istrue value="${verbose}" />
 276 
 277     </condition>
 278 
 279     <!-- This is needed to switch verbosity of zipalign and aapt. Depends exclusively on 'verbose'
 280 
 281          -->
 282 
 283     <condition property="v.option" value="-v" else="">
 284 
 285         <istrue value="${verbose}" />
 286 
 287     </condition>
 288 
 289     <!-- This is needed to switch verbosity of dx. Depends exclusively on 'verbose' -->
 290 
 291     <condition property="verbose.option" value="--verbose" else="">
 292 
 293         <istrue value="${verbose}" />
 294 
 295     </condition>
 296 
 297 
 298 
 299     <!-- Tools -->
 300 
 301     <condition property="exe" value=".exe" else=""><os family="windows" /></condition>
 302 
 303     <property name="adb" location="${android.tools.dir}/adb${exe}" />
 304 
 305     <property name="zipalign" location="${android.tools.dir}/zipalign${exe}" />
 306 
 307 
 308 
 309     <!-- Emma configuration -->
 310 
 311     <property name="emma.dir" value="${sdk.dir}/tools/lib" />
 312 
 313         <path id="emma.lib">
 314 
 315             <pathelement location="${emma.dir}/emma.jar" />
 316 
 317             <pathelement location="${emma.dir}/emma_ant.jar" />
 318 
 319         </path>
 320 
 321     <taskdef resource="emma_ant.properties" classpathref="emma.lib" />
 322 
 323     <!-- End of emma configuration -->
 324 
 325 
 326 
 327     <!-- Macros -->
 328 
 329 
 330 
 331     <!-- Configurable macro, which allows to pass as parameters output directory,
 332 
 333          output dex filename and external libraries to dex (optional) -->
 334 
 335     <macrodef name="dex-helper">
 336 
 337        <element name="external-libs" optional="yes" />
 338 
 339        <element name="extra-parameters" optional="yes" />
 340 
 341        <sequential>
 342 
 343          <echo>Converting compiled files and external libraries into ${intermediate.dex.file}...
 344 
 345          </echo>
 346 
 347          <apply executable="${dx}" failonerror="true" parallel="true">
 348 
 349              <arg value="--dex" />
 350 
 351              <arg value="--output=${intermediate.dex.file}" />
 352 
 353              <extra-parameters />
 354 
 355              <arg line="${verbose.option}" />
 356 
 357              <arg path="${out.classes.absolute.dir}" />
 358 
 359              <fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
 360 
 361              <external-libs />
 362 
 363          </apply>
 364 
 365        </sequential>
 366 
 367     </macrodef>
 368 
 369 
 370 
 371     <!-- This is macro that enable passing variable list of external jar files to ApkBuilder
 372 
 373          Example of use:
 374 
 375          <package-helper>
 376 
 377              <extra-jars>
 378 
 379                 <jarfolder path="my_jars" />
 380 
 381                 <jarfile path="foo/bar.jar" />
 382 
 383                 <jarfolder path="your_jars" />
 384 
 385              </extra-jars>
 386 
 387          </package-helper> -->
 388 
 389     <macrodef name="package-helper">
 390 
 391         <attribute name="sign.package" />
 392 
 393         <element name="extra-jars" optional="yes" />
 394 
 395         <sequential>
 396 
 397             <apkbuilder
 398 
 399                     outfolder="${out.absolute.dir}"
 400 
 401                     basename="${ant.project.name}"
 402 
 403                     signed="@{sign.package}"
 404 
 405                     verbose="${verbose}">
 406 
 407                 <file path="${intermediate.dex.file}" />
 408 
 409                 <sourcefolder path="${source.absolute.dir}" />
 410 
 411                 <nativefolder path="${native.libs.absolute.dir}" />
 412 
 413                 <jarfolder path="${external.libs.absolute.dir}" />
 414 
 415                 <extra-jars/>
 416 
 417             </apkbuilder>
 418 
 419         </sequential>
 420 
 421     </macrodef>
 422 
 423 
 424 
 425     <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets
 426 
 427          debug, -debug-with-emma and release.-->
 428 
 429     <macrodef name="zipalign-helper">
 430 
 431         <attribute name="in.package" />
 432 
 433         <attribute name="out.package" />
 434 
 435         <sequential>
 436 
 437             <echo>Running zip align on final apk...</echo>
 438 
 439             <exec executable="${zipalign}" failonerror="true">
 440 
 441                 <arg line="${v.option}" />
 442 
 443                 <arg value="-f" />
 444 
 445                 <arg value="4" />
 446 
 447                 <arg path="@{in.package}" />
 448 
 449                 <arg path="@{out.package}" />
 450 
 451             </exec>
 452 
 453         </sequential>
 454 
 455     </macrodef>
 456 
 457 
 458 
 459     <!-- This is macro used only for sharing code among two targets, -install and
 460 
 461          -install-with-emma which do exactly the same but differ in dependencies -->
 462 
 463     <macrodef name="install-helper">
 464 
 465         <sequential>
 466 
 467             <echo>Installing ${out.debug.package} onto default emulator or device...</echo>
 468 
 469             <exec executable="${adb}" failonerror="true">
 470 
 471                 <arg line="${adb.device.arg}" />
 472 
 473                 <arg value="install" />
 474 
 475                 <arg value="-r" />
 476 
 477                 <arg path="${out.debug.package}" />
 478 
 479             </exec>
 480 
 481         </sequential>
 482 
 483     </macrodef>
 484 
 485 
 486 
 487     <!-- Rules -->
 488 
 489 
 490 
 491     <!-- Creates the output directories if they don't exist yet. -->
 492 
 493     <target name="-dirs">
 494 
 495         <echo>Creating output directories if needed...</echo>
 496 
 497         <mkdir dir="${resource.absolute.dir}" />
 498 
 499         <mkdir dir="${external.libs.absolute.dir}" />
 500 
 501         <mkdir dir="${gen.absolute.dir}" />
 502 
 503         <mkdir dir="${out.absolute.dir}" />
 504 
 505         <mkdir dir="${out.classes.absolute.dir}" />
 506 
 507     </target>
 508 
 509     
 510 
 511     <property name="proguard-home" value="D:/proguard4.6/lib"/>
 512 
 513 
 514 
 515      <target name="optimize" depends="compile">
 516 
 517      <echo>optimize files...</echo>
 518 
 519     
 520 
 521          <jar basedir="${out.classes.absolute.dir}" destfile="temp.jar"/>
 522 
 523                    <java jar="${proguard-home}/proguard.jar" fork="true" failonerror="true">
 524 
 525                          <jvmarg value="-Dmaximum.inlined.code.length=32"/>
 526 
 527                          <arg value="-injars temp.jar"/>
 528 
 529                          <arg value="-libraryjars ${android-jar}"/>
 530 
 531                          <arg value="-outjars optimized.jar"/>
 532 
 533                          
 534 
 535                          <!-- <arg value="-libraryjars ${library-jar}/some_lib_used.jar"/> -->
 536 
 537                          <arg value="-dontpreverify"/>
 538 
 539                          <arg value="-dontoptimize"/>
 540 
 541                        
 542 
 543                          <arg value="-dontusemixedcaseclassnames"/>
 544 
 545                          <arg value="-repackageclasses ''"/>
 546 
 547                          <arg value="-allowaccessmodification"/>
 548 
 549                          
 550 
 551                          
 552 
 553                          <arg value="-keep public class * extends android.app.Activity"/>
 554 
 555                                                     <arg value="-keep public class * extends android.app.Application"/>
 556 
 557                                                     <arg value="-keep public class * extends android.app.Service"/>
 558 
 559                                                     <arg value="-keep public class * extends android.content.BroadcastReceiver"/>
 560 
 561                                                     <arg value="-keep public class * extends android.content.ContentProvider"/>
 562 
 563                                                     <arg value="-keep public class * extends android.app.backup.BackupAgentHelper"/>
 564 
 565                                                     <arg value="-keep public class * extends android.preference.Preference"/>
 566 
 567                                                     <arg value="-keep public class com.android.vending.licensing.ILicensingService"/>
 568 
 569                                                     
 570 
 571                                             
 572 
 573                          
 574 
 575                                          <!-- <arg value="-keep public class com.just2me.obfapp.receiver.*"/> -->
 576 
 577                                                  <arg value="-optimizationpasses 5"/>
 578 
 579                         <arg value="-verbose"/>
 580 
 581                          <arg value="-dontskipnonpubliclibraryclasses"/>
 582 
 583                          <arg value="-dontskipnonpubliclibraryclassmembers"/>
 584 
 585                  </java>
 586 
 587                  <delete file="temp.jar"/>
 588 
 589                  <delete dir="${out.classes.absolute.dir}"/>
 590 
 591                  <mkdir dir="${out.classes.absolute.dir}"/>
 592 
 593                  <unzip src="optimized.jar" dest="${out.classes.absolute.dir}"/>
 594 
 595                  <delete file="optimized.jar"/>
 596 
 597      </target>
 598 
 599     
 600 
 601     <!-- Generate the version number to strings.xml -->
 602 
 603     <target name="gen-ver-num" depends="-dirs">
 604 
 605         <echo>Generating SVN version number to resource string...</echo>
 606 
 607         <!--<exec executable="./genVersionNum.sh" failonerror="true"></exec> -->
 608 
 609         <exec executable="bash"> <arg value="genVersionNum.sh"/></exec>
 610 
 611     </target>
 612 
 613 
 614 
 615     <!-- Generates the R.java file for this project's resources. -->
 616 
 617     <target name="-resource-src" depends="-dirs">
 618 
 619         <echo>Generating R.java / Manifest.java from the resources...</echo>
 620 
 621         <exec executable="${aapt}" failonerror="true">
 622 
 623             <arg value="package" />
 624 
 625             <arg line="${v.option}" />
 626 
 627             <arg value="-m" />
 628 
 629             <arg value="-J" />
 630 
 631             <arg path="${gen.absolute.dir}" />
 632 
 633             <arg value="-M" />
 634 
 635             <arg path="AndroidManifest.xml" />
 636 
 637             <arg value="-S" />
 638 
 639             <arg path="${resource.absolute.dir}" />
 640 
 641             <arg value="-I" />
 642 
 643             <arg path="${android.jar}" />
 644 
 645         </exec>
 646 
 647     </target>
 648 
 649 
 650 
 651     <!-- Generates java classes from .aidl files. -->
 652 
 653     <target name="-aidl" depends="-dirs">
 654 
 655         <echo>Compiling aidl files into Java classes...</echo>
 656 
 657         <apply executable="${aidl}" failonerror="true">
 658 
 659             <arg value="-p${android.aidl}" />
 660 
 661             <arg value="-I${source.absolute.dir}" />
 662 
 663             <arg value="-o${gen.absolute.dir}" />
 664 
 665             <fileset dir="${source.absolute.dir}">
 666 
 667                 <include name="**/*.aidl" />
 668 
 669             </fileset>
 670 
 671         </apply>
 672 
 673     </target>
 674 
 675 
 676 
 677     <path id="classpathref">
 678 
 679         <fileset dir="./lib/">
 680 
 681             <include name="*.jar" />
 682 
 683         </fileset>
 684 
 685     </path>
 686 
 687 
 688 
 689     <!-- Compiles this project's .java files into .class files. -->
 690 
 691     <target name="compile" depends="-resource-src, -aidl"
 692 
 693                 description="Compiles project's .java files into .class files">
 694 
 695         <!-- If android rules are used for a test project, its classpath should include
 696 
 697              tested project's location -->
 698 
 699         <condition property="extensible.classpath"
 700 
 701                            value="${tested.project.absolute.dir}/bin" else=".">
 702 
 703             <isset property="tested.project.absolute.dir" />
 704 
 705         </condition>
 706 
 707         <javac encoding="ascii" target="1.5" debug="true" extdirs=""
 708 
 709                         
 710 
 711                 destdir="${out.classes.absolute.dir}"
 712 
 713                 bootclasspathref="android.target.classpath"
 714 
 715                
 716 
 717                 >
 718 
 719             <src path="${source.absolute.dir}" />
 720 
 721             <src path="${gen.absolute.dir}" />
 722 
 723             <classpath>
 724 
 725                 <fileset dir="${external.libs.absolute.dir}" includes="*.jar" />
 726 
 727             </classpath>
 728 
 729         </javac>
 730 
 731     </target>
 732 
 733 
 734 
 735     <!-- Converts this project's .class files into .dex files -->
 736 
 737     <target name="-dex" depends="compile,optimize">
 738 
 739         <dex-helper />
 740 
 741     </target>
 742 
 743 
 744 
 745     <!-- Puts the project's resources into the output package file
 746 
 747          This actually can create multiple resource package in case
 748 
 749          Some custom apk with specific configuration have been
 750 
 751          declared in default.properties.
 752 
 753          -->
 754 
 755     <target name="-package-resources">
 756 
 757         <echo>Packaging resources</echo>
 758 
 759         <aaptexec executable="${aapt}"
 760 
 761                 command="package"
 762 
 763                 manifest="AndroidManifest.xml"
 764 
 765                 resources="${resource.absolute.dir}"
 766 
 767                 assets="${asset.absolute.dir}"
 768 
 769                 androidjar="${android.jar}"
 770 
 771                 outfolder="${out.absolute.dir}"
 772 
 773                 basename="${ant.project.name}" />
 774 
 775     </target>
 776 
 777 
 778 
 779     <!-- Packages the application and sign it with a debug key. -->
 780 
 781     <target name="-package-debug-sign" depends="-dex, -package-resources">
 782 
 783         <package-helper sign.package="true" />
 784 
 785     </target>
 786 
 787 
 788 
 789     <!-- Packages the application without signing it. -->
 790 
 791     <target name="-package-no-sign" depends="-dex, -package-resources">
 792 
 793         <package-helper sign.package="false" />
 794 
 795     </target>
 796 
 797 
 798 
 799     <target name="-compile-tested-if-test" if="tested.project.dir" unless="do.not.compile.again">
 800 
 801        <subant target="compile">
 802 
 803             <fileset dir="${tested.project.absolute.dir}" includes="build.xml" />
 804 
 805        </subant>
 806 
 807     </target>
 808 
 809 
 810 
 811     <!-- Builds debug output package, provided all the necessary files are already dexed -->
 812 
 813     <target name="debug" depends="-compile-tested-if-test, -package-debug-sign"
 814 
 815                 description="Builds the application and signs it with a debug key.">
 816 
 817         <zipalign-helper in.package="${out.debug.unaligned.package}"
 818 
 819                                    out.package="${out.debug.package}" />
 820 
 821         <echo>Debug Package: ${out.debug.package}</echo>
 822 
 823     </target>
 824 
 825 
 826 
 827     <target name="-release-check">
 828 
 829         <condition property="release.sign">
 830 
 831             <and>
 832 
 833                 <isset property="key.store" />
 834 
 835                 <isset property="key.alias" />
 836 
 837             </and>
 838 
 839         </condition>
 840 
 841     </target>
 842 
 843 
 844 
 845     <target name="-release-nosign" depends="-release-check" unless="release.sign">
 846 
 847         <echo>No key.store and key.alias properties found in build.properties.</echo>
 848 
 849         <echo>Please sign ${out.unsigned.package} manually</echo>
 850 
 851         <echo>and run zipalign from the Android SDK tools.</echo>
 852 
 853     </target>
 854 
 855 
 856 
 857         <target name="signapk">
 858 
 859         <echo>Signing apk...</echo>
 860 
 861         <signjar
 862 
 863                 jar="${out.unsigned.package}"
 864 
 865                 signedjar="${out.unaligned.package}"
 866 
 867                 keystore="${key.store}"
 868 
 869                 storepass="${key.store.password}"
 870 
 871                 alias="${key.alias}"
 872 
 873                 keypass="${key.alias.password}"
 874 
 875                 verbose="${verbose}" />
 876 
 877 
 878 
 879         <!-- Zip aligns the APK -->
 880 
 881         <echo>Zipalign apk...</echo>
 882 
 883         <zipalign-helper in.package="${out.unaligned.package}" out.package="${out.release.package}" />
 884 
 885         <echo>Release Package: ${out.release.package}</echo>
 886 
 887 
 888 
 889         <delete file="${out.unsigned.package}"/>
 890 
 891         <delete file="${out.unaligned.package}"/>
 892 
 893     </target>
 894 
 895         
 896 
 897     <target name="release" depends="-package-no-sign, -release-nosign" if="release.sign"
 898 
 899                 description="Builds the application. The generated apk file must be signed before
 900 
 901                             it is published.">
 902 
 903         <!-- Gets passwords -->
 904 
 905         <input
 906 
 907                 message="Please enter keystore password (store:${key.store}):"
 908 
 909                 addproperty="key.store.password" />
 910 
 911         <input
 912 
 913                 message="Please enter password for alias '${key.alias}':"
 914 
 915                 addproperty="key.alias.password" />
 916 
 917 
 918 
 919         <!-- Signs the APK -->
 920 
 921         <echo>Signing final apk...</echo>
 922 
 923         <signjar
 924 
 925                 jar="${out.unsigned.package}"
 926 
 927                 signedjar="${out.unaligned.package}"
 928 
 929                 keystore="${key.store}"
 930 
 931                 storepass="${key.store.password}"
 932 
 933                 alias="${key.alias}"
 934 
 935                 keypass="${key.alias.password}"
 936 
 937                 verbose="${verbose}" />
 938 
 939 
 940 
 941         <!-- Zip aligns the APK -->
 942 
 943         <zipalign-helper in.package="${out.unaligned.package}"
 944 
 945                                    out.package="${out.release.package}" />
 946 
 947         <echo>Release Package: ${out.release.package}</echo>
 948 
 949     </target>
 950 
 951 
 952 
 953     <target name="install" depends="debug"
 954 
 955                 description="Installs/reinstalls the debug package onto a running
 956 
 957                             emulator or device. If the application was previously installed,
 958 
 959                             the signatures must match." >
 960 
 961         <install-helper />
 962 
 963     </target>
 964 
 965 
 966 
 967     <target name="-uninstall-check">
 968 
 969         <condition property="uninstall.run">
 970 
 971            <isset property="manifest.package" />
 972 
 973         </condition>
 974 
 975     </target>
 976 
 977 
 978 
 979     <target name="-uninstall-error" depends="-uninstall-check" unless="uninstall.run">
 980 
 981         <echo>Unable to run 'ant uninstall', manifest.package property is not defined.
 982 
 983         </echo>
 984 
 985     </target>
 986 
 987 
 988 
 989     <!-- Uninstalls the package from the default emulator/device -->
 990 
 991     <target name="uninstall" depends="-uninstall-error" if="uninstall.run"
 992 
 993                 description="Uninstalls the application from a running emulator or device.">
 994 
 995         <echo>Uninstalling ${manifest.package} from the default emulator or device...</echo>
 996 
 997         <exec executable="${adb}" failonerror="true">
 998 
 999             <arg line="${adb.device.arg}" />
1000 
1001             <arg value="uninstall" />
1002 
1003             <arg value="${manifest.package}" />
1004 
1005         </exec>
1006 
1007     </target>
1008 
1009 
1010 
1011     <target name="clean" description="Removes output files created by other targets.">
1012 
1013         <delete dir="${out.absolute.dir}" verbose="${verbose}" />
1014 
1015         <delete dir="${gen.absolute.dir}" verbose="${verbose}" />
1016 
1017     </target>
1018 
1019 
1020 
1021     <!-- Targets for code-coverage measurement purposes, invoked from external file -->
1022 
1023 
1024 
1025     <!-- Emma-instruments tested project classes (compiles the tested project if necessary)
1026 
1027              and writes instrumented classes to ${instrumentation.absolute.dir}/classes -->
1028 
1029     <target name="-emma-instrument" depends="compile">
1030 
1031         <echo>Instrumenting classes from ${out.absolute.dir}/classes...</echo>
1032 
1033         <!-- It only instruments class files, not any external libs -->
1034 
1035         <emma enabled="true">
1036 
1037             <instr verbosity="${verbosity}"
1038 
1039                    mode="overwrite"
1040 
1041                    instrpath="${out.absolute.dir}/classes"
1042 
1043                    outdir="${out.absolute.dir}/classes">
1044 
1045             </instr>
1046 
1047             <!-- TODO: exclusion filters on R*.class and allowing custom exclusion from
1048 
1049                  user defined file -->
1050 
1051         </emma>
1052 
1053     </target>
1054 
1055 
1056 
1057     <target name="-dex-instrumented" depends="-emma-instrument">
1058 
1059        <dex-helper>
1060 
1061           <extra-parameters>
1062 
1063             <arg value="--no-locals" />
1064 
1065           </extra-parameters>
1066 
1067           <external-libs>
1068 
1069             <fileset file="${emma.dir}/emma_device.jar" />
1070 
1071           </external-libs>
1072 
1073        </dex-helper>
1074 
1075     </target>
1076 
1077 
1078 
1079     <!-- Invoked from external files for code coverage purposes -->
1080 
1081     <target name="-package-with-emma" depends="-dex-instrumented, -package-resources">
1082 
1083         <package-helper sign.package="true">
1084 
1085             <extra-jars>
1086 
1087                 <!-- Injected from external file -->
1088 
1089                 <jarfile path="${emma.dir}/emma_device.jar" />
1090 
1091             </extra-jars>
1092 
1093         </package-helper>
1094 
1095     </target>
1096 
1097 
1098 
1099     <target name="-debug-with-emma" depends="-package-with-emma">
1100 
1101         <zipalign-helper in.package="${out.debug.unaligned.package}"
1102 
1103                                    out.package="${out.debug.package}" />
1104 
1105     </target>
1106 
1107 
1108 
1109     <target name="-install-with-emma" depends="-debug-with-emma">
1110 
1111         <install-helper />
1112 
1113     </target>
1114 
1115 
1116 
1117     <!-- End of targets for code-coverage measurement purposes -->
1118 
1119 
1120 
1121     <target name="help">
1122 
1123         <!-- displays starts at col 13
1124 
1125               |13                                                              80| -->
1126 
1127         <echo>Android Ant Build. Available targets:</echo>
1128 
1129         <echo>   help:      Displays this help.</echo>
1130 
1131         <echo>   clean:     Removes output files created by other targets.</echo>
1132 
1133         <echo>   compile:   Compiles project's .java files into .class files.</echo>
1134 
1135         <echo>   debug:     Builds the application and signs it with a debug key.</echo>
1136 
1137         <echo>   release:   Builds the application. The generated apk file must be</echo>
1138 
1139         <echo>              signed before it is published.</echo>
1140 
1141         <echo>   install:   Installs/reinstalls the debug package onto a running</echo>
1142 
1143         <echo>              emulator or device.</echo>
1144 
1145         <echo>              If the application was previously installed, the</echo>
1146 
1147         <echo>              signatures must match.</echo>
1148 
1149         <echo>   uninstall: Uninstalls the application from a running emulator or</echo>
1150 
1151         <echo>              device.</echo>
1152 
1153     </target>
1154 
1155 </project>

Then you can enter the project's directory and use command:

$ant release

to genarate a release version of result.

Some time there are mistakes like

[javac]****警告:编码 ascii 的不可映射字符********

  出现“警告:编码 ascii 的不可映射字符”错误

To solve this kind of mistake, find the javac part in build.xml file, change the encoding 'ascii' to an appropriate one.

such as 'gbk' or 'utf-8'

原文地址:https://www.cnblogs.com/jayceli/p/2457040.html