Use ant to compile and package the android project into an APK file and install it on your mobile phone

Source: Internet
Author: User

This article briefly introduces how to use the ant script Android project to compile and package it into an APK file and install it on your mobile phone.

Main steps:

1. Generate the R. Java file: Use ant and command line to generate R. Java using the aapt. Ext program provided by Android SDK.
2. Generate the. aidl file. Java class file: Use ant and the command line to generate the. Java file using aidl.exe of Android sdk6.
3. Step 3 compile the. Java class file to generate the class file: Use ant and the command line to compile the Java class file using JDK javac to generate the class file.
4. Package the class file to generate the classes. Dex file: Use the ant command line to generate the classes. Dex file using the DX. Bat command line script provided by the android SDK.
5. Package the resource file (including res, assets, and androidmanifest. XML) in step 5: Use the ant command line to generate a resource package file using aapt.exe of Android sdk.pdf.
6. Generate the unsigned APK Installation File in Step 6: Use the apkbuilder. Bat Command Script provided by Android SDK to generate the unsigned APK Installation File in ant and command lines.
7. In Step 7, sign the unsigned APK to generate the signed Android file: ant and the command line use JDK's jarsigner to sign the unsigned package.
8. In Step 8, install and uninstall the APK file and use the ant command line to use adb.exe of Android sdk.exe.

The build script and property file is pasted below: the example can be downloaded from the http://download.csdn.net/source/3505789.

<? XML version = "1.0" encoding = "UTF-8"?>
<Project name = "anttest" default = "help">

<Property file = "build. properties"/>

<Path id = "android. antlibs">
<Pathelement Path = "$ {SDK. dir}/tools/lib/anttasks. Jar"/>
<Pathelement Path = "$ {SDK. dir}/tools/lib/sdklib. Jar"/>
<Pathelement Path = "$ {SDK. dir}/tools/lib/androidprefs. Jar"/>
</Path>

<Path id = "project. classpath">
<Pathelement location = "$ {Android-jar}"/>
</Path>
<Taskdef name = "setup" classname = "com. Android. Ant. setuptask" classpathref = "android. antlibs"/>
<Setup/>

<Target name = "copy">
<Copy todir = "$ {outdir-gen}">
<Fileset dir = "$ {srcdir}"/>
</Copy>
</Target>
<Target name = "genrjava">
<Echo> generating R. Java from the resources... </echo>
<Mkdir dir = "$ {outdir-gen}"/>
<Exec executable = "$ {aapt}" failonerror = "true">
<Arg value = "package"/>
<Arg value = "-M"/>
<Arg value = "-J"/>
<Arg value = "$ {outdir-gen}"/>
<Arg value = "-M"/>
<Arg value = "$ {projecthomedir}/androidmanifest. xml"/>
<Arg value = "-s"/>
<Arg value = "$ {resource-Dir}"/>
<Arg value = "-I"/>
<Arg value = "$ {Android-jar}"/>
</Exec>
</Target>
<Target name = "aidl" depends = "copy">
<Echo> compiling aidl files into Java classes... </echo>
<Apply executable = "$ {aidl}" failonerror = "true">
<Arg value = "-p $ {SDK. dir}"/>
<Arg value = "-I $ {srcdir}"/>
<Fileset dir = "$ {srcdir}">
<Include name = "**/*. aidl"/>
</Fileset>
</Apply>
</Target>
<! -- Compile this project's. java files into. class files. -->
<Target name = "compile" depends = "Copy, genrjava, aidl">
<Mkdir dir = "$ {outdir-classes}"/>
<Javac encoding = "UTF-8" target = "1.5" DEBUG = "true" extdirs = "" srcdir = "$ {outdir-gen}" destdir = "$ {outdir-classes} "bootclasspath =" $ {Android-jar} ">
<Classpath refID = "project. classpath"/>
</Javac>
</Target>

<! -- Convert this project's. class files into. Dex files. -->
<Target name = "Dex" depends = "compile">
<Echo> converting compiled files and external libraries into $ {outdir-DX}/test. Dex... </echo>
<Mkdir dir = "$ {outdir-DX}"/>
<Apply executable = "$ {DX}" failonerror = "true" parallel = "true">
<Arg value = "-- Dex"/>
<Arg value = "-- output =$ {outdir-DX}/test. Dex"/>
<Arg Path = "$ {outdir-classes}"/>
<Fileset dir = "$ {projecthomedir}" includes = "*. Jar"/>
</Apply>
</Target>
<! -- Put the project's resources into the output package file. -->
<Target name = "package-res">
<Echo> packaging resources and assets... </echo>
<Mkdir dir = "$ {apkdir}"/>
<Exec executable = "$ {aapt}" failonerror = "true">
<Arg value = "package"/>
<Arg value = "-F"/>
<Arg value = "-M"/>
<Arg value = "$ {projecthomedir}/androidmanifest. xml"/>
<Arg value = "-s"/>
<Arg value = "$ {resource-Dir}"/>
<Arg value = "-a"/>
<Arg value = "$ {asset-Dir}"/>
<Arg value = "-I"/>
<Arg value = "$ {Android-jar}"/>
<Arg value = "-F"/>
<Arg value = "$ {resources-package}"/>
</Exec>
</Target>

<Target name = "packageapk" depends = "Dex, package-res">
<Echo> packaging APK for release... </echo>
<Exec executable = "$ {apkbuilder}" failonerror = "true">
<Arg value = "$ {apkdir}/$ {unsignedapkname}"/>
<Arg value = "-U"/>
<Arg value = "-z"/>
<Arg value = "$ {resources-package}"/>
<Arg value = "-F"/>
<Arg value = "$ {outdir-DX}/test. Dex"/>
<Arg value = "-RF"/>
<Arg value = "$ {srcdir}"/>
<Arg value = "-RJ"/>
<Arg value = "$ {outdir-gen}"/>
</Exec>
<Echo> it will need to be signed with jarsigner before being published. </echo>
</Target>
<Target name = "signapk" depends = "packageapk">
<Echo> signapk $ {apkdir}/$ {apkname} </echo>
<Signjar
Jar = "$ {apkdir}/$ {unsignedapkname }"
Signedjar = "$ {apkdir}/$ {apkname }"
Keystore = "keystore"
Storepass = "123456"
Alias = "alias"
Keypass = "123456"
Verbose = "-verbose"/>
<! --
<Exec executable = "$ {jarsigner}" failonerror = "true">
<Arg value = "-verbose"/>
<Arg value = "-storepass"/>
<Arg value = "test"/>
<Arg value = "-keypass"/>
<Arg value = "test"/>
<Arg value = "-keystore"/>
<Arg value = "bbyread. keystore"/>
<Arg value = "-signedjar"/>
<Arg value = "$ {apkdir}/$ {apkname}"/>
<Arg value = "$ {apkdir}/$ {unsignedapkname}"/>
<Arg value = "byread"/>
</Exec> -->
</Target>

<Target name = "installapk" depends = "signapk">
<Echo> install APK... </echo>

<Exec executable = "ADB" failonerror = "true">
<Arg value = "Install"/>
<Arg value = "$ {apkdir}/$ {apkname}"/>
</Exec>
</Target>

<Target name = "uninstallapk">
<Echo> uninstall APK .. </echo>
<Exec executable = "ADB" failonerror = "true">
<Arg value = "Uninstall"/>
<Arg value = "com. anttest"/>
</Exec>
</Target>

<Target name = "help"/>

</Project>

Property file content:

Target = Android-8
SDK. dir = D :\\ Android \ Android-SDK-Windows

Apkbuilder =$ {SDK. dir} \ tools \ apkbuilder. bat

Android-jar =$ {SDK. dir} \ platforms \ Android-8 \ Android. Jar

Projecthomedir = D :\\ Android \\ andriodwk \ anttest
Builddir =$ {projecthomedir} \ build
Resource-Dir =$ {projecthomedir} \ res
Asset-Dir =$ {projecthomedir} \ assets
Srcdir =$ {projecthomedir} \ SRC

Outdir-gen =$ {builddir} \ SRC
Outdir-classes =$ {builddir} \ Classes
Outdir-DX =$ {builddir} \ DX
Resources-package =$ {builddir} \ respak
Apkdir =$ {builddir} \ APK
Unsignedapknameappsunsigntest.apk
Apknameappstest.apk

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.