Eclipse uses ant to package android

Source: Internet
Author: User

In general, we use eclipse to develop Android applications, and almost never considered using command line compilation, because eclipse is so powerful that android development has become a fool.

Today, I am determined to use ant to compile Android applications. ant to compile Android applications is actually calling multiple compilation tools provided by multiple Android sdks, and finally compiling and packaging signatures, form an APK file.

My development environment is Windows XP, Java is 1.6, and eclipse is the one that comes with ADT. The following describes how to use ant to compile the Android app build. xml.

<project default="main" basedir=".">     <property name="apk-name" value="product" />    <property name="apk-version" value="latest" />    <property name="apk-market" value="dev" />    <property name="basedir" value="." />     <property name="android-jar" value="E:/adt-bundle-windows-x86-20130219/sdk/platforms/android-8/android.jar" />    <!-- <property name="android-jar" value="/usr/lib/android-sdk/platforms/android-10/android.jar" /> -->     <target name="init">        <echo>start initing ... </echo>         <mkdir dir="out" />        <delete>            <fileset dir="out"></fileset>        </delete>                 <mkdir dir="gen" />        <delete>            <fileset dir="gen"></fileset>        </delete>                 <mkdir dir="bin/classes" />        <delete>            <fileset dir="bin/classes"></fileset>        </delete>                 <mkdir dir="build/${apk-version}" />         <echo>finish initing. </echo>    </target>     <target name="main" depends="init">        <echo>generating R.java for project to dir gen (using aapt) ... </echo>        <exec executable="aapt">            <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="-S" />            <arg value="${basedir}/res" />            <arg value="-I" />            <arg value="${android-jar}" />            <arg value="--auto-add-overlay" />        </exec>         <echo>generating R.java for library to dir gen (using aapt) ... </echo>        <exec executable="aapt">            <arg value="package" />            <arg value="-m" />            <arg value="--non-constant-id" />            <arg value="--auto-add-overlay" />            <arg value="-J" />            <arg value="gen" />            <arg value="-M" />            <arg value="${basedir}/AndroidManifest.xml" />            <arg value="-S" />            <arg value="res" />            <arg value="-S" />            <arg value="${basedir}/res" />            <arg value="-I" />            <arg value="${android-jar}" />        </exec>         <path id="project.libs">            <fileset dir="libs">                <include name="*.jar" />            </fileset>        </path>        <echo>compiling java files to class files (include R.java, library and the third-party jars) ... </echo>        <javac destdir="bin/classes" bootclasspath="${android-jar}">            <src path="${basedir}/src" />            <src path="src" />            <src path="gen" />            <classpath refid="project.libs" />        </javac>         <echo>packaging class files (include the third-party jars) to calsses.dex ... </echo>        <!-- <exec executable="dx"> -->        <exec executable="dx.bat">            <arg value="--dex" />            <arg value="--output=${basedir}/out/classes.dex" />            <arg value="${basedir}/bin/classes" />            <arg value="${basedir}/libs" />        </exec>         <echo>packaging resource (include res, assets, AndroidManifest.xml, etc.) to res.zip ... </echo>        <exec executable="aapt">            <arg value="package" />            <arg value="-f" />            <arg value="-M" />            <arg value="AndroidManifest.xml" />            <arg value="-S" />            <arg value="res" />            <arg value="-S" />            <arg value="${basedir}/res" />            <arg value="-A" />            <arg value="assets" />            <arg value="-I" />            <arg value="${android-jar}" />            <arg value="-F" />            <arg value="out/res.zip" />            <arg value="--auto-add-overlay" />        </exec>         <echo>building unsigned.apk ... </echo>        <!-- <exec executable="apkbuilder"> -->        <exec executable="apkbuilder.bat">            <arg value="${basedir}/out/unsigned.apk" />            <arg value="-u" />            <arg value="-z" />            <arg value="${basedir}/out/res.zip" />            <arg value="-f" />            <arg value="${basedir}/out/classes.dex" />        </exec>         <echo>signing the unsigned apk to final product apk ... </echo>        <exec executable="jarsigner">            <arg value="-keystore" />            <arg value=".keystore" />            <arg value="-storepass" />            <arg value="hello123456" />            <arg value="-keypass" />            <arg value="hello123456" />            <arg value="-signedjar" />            <arg value="${basedir}/build/${apk-version}/${apk-name}_${apk-version}_${apk-market}.apk" />            <arg value="${basedir}/out/unsigned.apk" />            <arg value="zhy" />        </exec>         <echo>done.</echo>    </target></project>

The above Build. xml file passed the test on my machine. Note: The bold part is the part to be slightly modified. you should understand it.

Note that before execution, use keytool-genkey-alias yourname to generate a key. Copy the. keystore file to the directory or specify a specific location. Otherwise, if the last step fails, you cannot generate an APK file that can be installed and used on your mobile phone.

E: \ workspace \ hello8>AntBuildfile: e: \ workspace \ hello8 \ build. xmlinit: [Echo] Start initing... [mkdir] created dir: e: \ workspace \ hello8 \ out [mkdir] created dir: e: \ workspace \ hello8 \ Gen [mkdir] created dir: e: \ workspace \ hello8 \ bin \ Classes [mkdir] created dir: e: \ workspace \ hello8 \ build \ latest [Echo] Finish initing. main: [Echo] generating R. java for project to Dir Gen (using aapt )... [Echo] generating R. java for library Dir Gen (using aapt )... [Echo] compiling java files to class files (include R. java, library and the third-party jars )... [javac] E: \ workspace \ hello8 \ build. XML: 76: Warning: 'includeantruntime' was not set, defaulting to build. sysclasspath = last; set to false for Repeatable builds [javac] compiling 2 source files to E: \ workspace \ hello8 \ bin \ Classes [Echo] packaging class files (include the third-Par Ty jars) to calsses. dex... [Echo] packaging Resource (include res, assets, androidmanifest. XML, etc .) to res.zip... [Echo] building unsigned.apk... [exec] [exec] this tool is deprecated. see -- Help for more information. [exec] [Echo] signing the unsigned APK to final product APK... [exec] [exec] Warning: [exec] the signatory certificate will expire within six months. [Echo] Done. Build successfultotal time: 9 secondse: \ workspace \ hello8>

Because I didn't specify the validity period when using keytool-genkey, the default value is six months. In actual use, it must refer to the time length, such as 10 years and 20 years.

The reason why I can use ant to compile Android applications so quickly is all the functions of the following article (but I modified build. XML to run it ):

Android Learning Series (31) -- How to Use ant to compile project multi-channel packaging for app Automation

Http://www.cnblogs.com/qianxudetianxia/archive/2012/07/04/2573687.html#commentform

Here, I would like to express my gratitude and respect to the author of this article!

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.