Android Ant Build.xml Instance

Source: Internet
Author: User

Use ant to compile apk.

When using, only need to change the SKD path javahome path and project path can be.

<?xml version= "1.0" encoding= "UTF-8"? ><project name= "Scolview" default= "Zipalign" basedir= "." ><property name= "EXE" value= ". exe"/><property name= "Bat" value= ". bat"/><!--the local path of each tool used, make sure to change it! --><!--android_home The installation folder for ANDROIDSDK on this computer--><property name= "Android_home" value= "f:/ Adt-bundle-windows-x86_64-20140624/sdk "/><!--the root folder of the corresponding project--><property name=" root "value=" F:/andoridADT/ Workspace/scolview "/><!--javahome folder--<property name=" Javahome "value= C:/Program Jdk1.6.0_45 "></property> <!--version of the Android SDK. 18 means Android 4.3--><property name= "Android_api_level" value= "/><!--android_ Location of the Android.jar package under the SDK folder--<property name= "Androidjar" value= "${android_home}/platforms/android-${android_ Api_level}/android.jar "/> <!--Javac folder on this machine--><property name=" Javac "value=" ${javahome}/bin/javac.exe " /><!--aapt for production R.java and packaging resource resources--><property name= "AAPT" value= "${android_hOme}/build-tools/18.0.1/aapt${exe} "/><!--DX is used to package A. class file (plus a third-party library and A. class file) into a. dex file--><property name=" DX "value=" ${android_home}/build-tools/18.1.1/dx.bat "/><!-- Apkbuilder is used to package. dex files and packaged resource files (AAPT packaged) and other resource files into an apk file--><property name= "Apkbuilder" value= "${android_home }/tools/apkbuilder${bat} "/><!--zipalign is used to optimize the APK, not required step--><property name=" zipalign "value=" ${android _home}/tools/zipalign${exe} "/><!--the various folders involved in the packaging process--><!--bin file to store the files needed for the APK package (. Dex,resource.ap_,.class, Androidmanifest.xml, etc.) and the resulting apk--><property name= "out" value= "${root}/dest"/><!--Gen is mainly used for storing R.java, Under Eclipse, build for yourself. Ant for Aapt tool generation--><property name= "gen" value= "gen"/><!--store Javac generated class file folder--><property name= " Classes "value=" ${out}/classes "/><!--storage. Java Source files folder--><property name=" src "value=" src "/><!-- Store the referenced library folder--><property name= "Lib" value= "${out}/libs"/><!--the encoding format when packaging--><property name= "encoding "Value=" UTF-8 "/><!--Generate APK name--><property name= "file_name" value= "Scollview"/> <!--signature warehouse. Generated by Keytool--><property name= "KeyStore" value= "${root}/edsheng.keystore"/><!--need to set the signature name-->< Property Name= "Key_name" value= "Edsheng"/><!--need to set--><property name= "Key_pass" value= "a199210311"/> <!--need to set--><property name= "Store_pass" value= "a199210311"/><!--the location of the jar package used for signing--><property Name= "Jarsigner" value= "${javahome}/bin/jarsigner.exe"/><!--the steps to actually compile the APK! 1. Clear the output path of the file (overwriting the original packaging result) 2. Create a folder that needs to be exported 3. Call the AAPT tool to generate R.java file 4. Call Javac to compile the Java file into a Java bytecode (. class file) 5. Call Dx.bat to the third-party library file that the. class file and other calls to Compiled as a. dex file (. dex files are capable of directly executing file formats on Dalvik virtual machines and can be understood as optimizations for embedded devices that are made to Java bytecode) 6. Call the Apkbuilder tool to generate the resource files (AAPT, such as Resource.ap, in the. dex file and the Bin folder. _) packaged into APK7. Call Jarsigner for the previous 8. Call Zipalign for performance improvement--><!--The following is the actual packaging process--><!--1. Clear the output path of the file (overwriting the original packaging results)- <target name= "mkdir" > <echo> Generate output Folder ... </echo> <mkdir dir= "${out}" ></mkdir> <echo> generation complete </echo></tArget><target name= "clean" depends= "mkdir" ><echo> cleanup output folder ...</echo> <delete dir= "${out}"/&G T;<delete dir= "${gen}"/> <echo> cleanup completed </echo></target><target name= "init" depends= "clean" & Gt;<echo> Create output Folder ... </echo><mkdir dir= "${out}"/><mkdir dir= "${gen}"/><mkdir dir= "${ Classes} "/><mkdir dir=" ${lib} "/><echo> Create folder complete </echo></target><!--aapt detailed reference meaning see: http ://blog.csdn.net/liuhe688/article/details/6679879--><!--generate R.java file for the project resource--><target name= "GEN" depends= "Init" ><echo> generate R.java from resource file ... </echo><exec executable= "${aapt}" failonerror= "true" > <arg value= "package"/><arg value= "-M"/><arg value= "-j"/><arg value= "${gen}"/><arg value= "-M"/><arg value= "Androidmanifest.xml"/><arg value= "-S"/><arg value= "res"/><arg value= "-i" /><arg value= "${androidjar}"/></exec><echo>r.java fileCheng Chenggong </echo></target><!--Javac For detailed reference to the meaning see: Http://cyf0110.iteye.com/blog/1508991--><target name= "Compile" depends= "Gen" ><echo> start compiling. class files ... </echo><javac fork= "true" executable= "${javac}" encoding= "${encoding}" debug= "true" extdirs= "" source= "1.5" target= "1.5" destdir= "${classes}" bootclasspath= "${ Androidjar} "> <src path=" ${src} "/> <src path=" ${gen} "/> <classpath&gt                ; <fileset dir= "${root}/libs" includes= "*.jar"/> </classpath> </javac><echo>.clas s file compiled </echo></target><!--execute Android's own tool Dx.bat is used to package. class files and third-party libraries into Dex files--><!-- Apply tag can be seen: Https://ant.apache.org/manual/Tasks/apply.html--><target name= "dex" depends= "compile" >< Echo> compiling. dex files ... </echo><apply executable= "${DX}" failonerror= "true" parallel= "true" > <arg value= " -jxms1024m "/> <arg value="-jxmx1536m "/> <arg value="-jxss1024k "/><arg value="--dex "/><arg value="--num-threads=4 "/><arg value="--output=${out}/ Classes.dex "/><arg path=" ${classes} "/><fileset dir=" ${root}/libs "> <include name=" *. Jar "/> </fileset></apply><echo> compile. dex file complete </echo></target><!--Package Project resource FILE-- ><target name= "Package_res_with_assets" ><echo> packaging resources and asset files ... </echo><exec executable= "${ AAPT} "failonerror=" true "><arg value=" package "/><arg value="-F "/><arg value="-M "/><arg Value= "Androidmanifest.xml"/><arg value= "-S"/><arg value= "res"/><arg value= "-A"/><arg Value= "Assets"/><arg value= "-i"/><arg value= "${androidjar}"/><arg value= "-F"/><arg value= " ${OUT}/${FILE_NAME}.AP_ "/></exec><echo> packaging resources and asset files </echo></target><target name=" APK "depends=" Dex, package_res_with_assets "><echo> start packing APK...&LT;/ECHO&GT;&LT;exec executable= "${apkbuilder}" failonerror= "true" ><arg value= "${out}/${file_name}_unsigned.apk"/>< Arg value= "-u"/><arg value= "-Z"/><arg value= "${OUT}/${FILE_NAME}.AP_"/><arg value= "-F"/>< Arg value= "${out}/classes.dex"/><arg value= "-RF"/><arg value= "${root}/src"/><arg value= "-RJ"/ ><arg value= "${root}/libs"/><arg value= "-nf"/><arg value= "${root}/libs"/></exec>< Echo> successfully packaged apk</echo></target><!--call Jarsigner for signing--><target name= "sign" depends= "APK" > <echo> start signing ... </echo><exec executable= "${jarsigner}" failonerror= "true" ><arg value= "-verbose" /><arg value= "-sigalg"/><arg value= "Md5withrsa"/><arg value= "-digestalg"/><arg value= "SHA1 "/><arg value="-keystore "/><arg value=" ${keystore} "/><arg value="-storepass "/><arg value=" ${store_pass} "/><arg value="-keypass "/><arg value=" ${key_pass} "/&Gt;<arg value= "-signedjar"/><arg value= "${out}/${file_name}_signed_unzipalign.apk"/><arg value= "${ out}/${file_name}_unsigned.apk "/><arg value=" ${key_name} "/></exec><echo> Signature success </echo> </target><!--Call the Zipalign tool to optimize the signed package--><!--mainly to optimize the byte-aligned resources, reduce the ram-->< in memory execution!--http:// Developer.android.com/tools/help/zipalign.html--><target name= "zipalign" depends= "sign" ><echo> Start optimization ... </echo><exec executable= "${zipalign}" failonerror= "true" ><arg value= "-V"/><arg value= " 4 "/><arg value=" ${out}/${file_name}_signed_unzipalign.apk "/><arg value=" ${out}/${file_name}_signed_ zipaligned.apk "/></exec><echo> end optimization </echo><echo> purge data generated during compilation ... </echo><delete dir= "${out}/classes" > </delete><echo> erase Data is complete.

</echo></target></project>


Android Ant Build.xml Instance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.