Use ant to package Android applications

Source: Internet
Author: User

Hello everyone, let's share today how to use ant to package Android applications.

We usually use eclipse to develop Android programs. It will automatically help us package the current applications. In the navigator view, we can see the following files:

Prepare is the final package file.

To package an application with ant, follow these steps:

1. Use the aapt command to generate the R. Java File

2. Use the aidl command to generate the corresponding Java File

3. Use the javac command to compile the Java source file to generate the class file

4. Use dx. BAT to convert the class file to the classes. Dex file.

5. Use the aapt command to generate the resource package file resources. AP _

6.use the apkbuilder.batpackage resource and classes.dexfile to generate the unsigned.apk

7.generate A signed.apk using the jarsingercommand to authenticate the API.

To facilitate understanding and memory, we will use a flowchart to describe the above processes:

The above is the overall process. Next we will explain each part in detail and clarify every step.

First, we need to familiarize ourselves with the commands used in each step:

1. aapt (Android asset packaging tool) command to generate the R. Java File Based on the Resource file

Parameter description:

-F forcibly overwrites existing files.
-M automatically generates the directory of the corresponding package at the location specified by-J.
-J specifies the directory generated by the R. Java file.
-S specifies the Resource Directory.
-M: Specifies the inventory file.
-I introduce the class library.

Note that the current location is the root directory of the ant project, so you need to enter a lot of command paths when necessary. The same is true in the following example.

2. The aidl (Android Interface Definition Language) command generates a Java file according to the. aidl definition file

The above example is located in COM/Scott/ant, according to the person in the package. aidl file, generate person in the directory corresponding to gen. java file. The example only processes a single file. The following describes how to process multiple aidl files in the directory.

3. The javac (Java compiler) command generates the corresponding class file based on the source file

Parameter description:

-D <directory> specifies the location where the generated class files are stored.
-Bootclasspath <path> overwrites the position of the boot class file

The example does not take into account the class library under the reference class path. complex situations will be encountered later.

4. dx command to convert the class file to a. Dex File

The above example is to convert the class file under the bin directory to the classes. Dex file and output it to the bin directory. We may use a third-party class library and will see it later.

5. aapt package resource files

Parameter description:

-F: Force Overwrite

-M specifies the manifest File

-S: Specify the Resource Directory

-A: Specify the asset directory

-I: Specifies the introduced class library.

-F: Specify the package to be generated

6.apk builder command, generate the APK package for the visa according to the classes. Dex file and resources. AP _

Parameter description:

-RF refers to the directory structure of the source file.

7. Run the jarsigner command to apply for a visa for the APK package generated above

During the visa process, you need to use the Certificate file. Note that the last release is the certificate alias. For details about how to create a certificate, see:

Of course, you can also use the graphic interface provided by ADT in eclipse to complete the above steps, select a project, right-click, and choose "android tools => export signed application package ", then select "create new keystore" for the keystore selection step, and enter the information as prompted.

The above is the command we used. Next we should analyze the build. xml required by ant:

First, we need to define a large number of variable attributes to indicate the paths and directories used, as shown below:

<Project name = "ant" default = "release"> <! -- Ant environment variable --> <property environment = "env"/> <! -- Application name --> <property name = "appname" value = "$ {ant. Project. name}"/> <! -- SDK directory (obtain the android_sdk_home value of the operating system environment variable) --> <property name = "SDK-folder" value = "$ {env. android_sdk_home}"/> <! -- SDK specifies the platform directory --> <property name = "SDK-platform-folder" value = "$ {SDK-Folder}/platforms/Android-8"/> <! -- Tools Directory In SDK --> <property name = "SDK-Tools" value = "$ {SDK-Folder}/tools"/> <! -- SDK specifies the tools directory on the platform --> <property name = "SDK-platform-Tools" value = "$ {SDK-platform-Folder}/tools"/> <! -- Command used (the current system is windows. if the system is Linux. replace the BAT file with the corresponding command) --> <property name = "aapt" value = "$ {SDK-platform-tools}/aapt"/> <property name = "aidl" value = "$ {SDK-Platform -Tools}/aidl "/> <property name =" dx "value =" $ {SDK-platform-tools}/dx. bat "/> <property name =" apkbuilder "value =" $ {SDK-tools}/apkbuilder. bat "/> <property name =" jarsigner "value =" $ {Env. java_home}/bin/jarsigner "/> <! -- Compile the required jar. If the project uses the map service, maps is required. jar --> <property name = "Android-jar" value = "$ {SDK-platform-Folder}/android. jar "/> <property name =" Android-maps-jar "value =" $ {SDK-Folder}/add-ons/addon_google_apis_google_inc_8/libs/maps. jar "/> <! -- Compile the pre-processing framework file framework required by aidl. aidl --> <property name = "framework-aidl" value = "$ {SDK-platform-Folder}/framework. aidl "/> <! -- Generate the relative directory of the r file --> <property name = "outdir-gen" value = "gen"/> <! -- Directory for storing compiled files --> <property name = "outdir-bin" value = "bin"/> <! -- Configuration file --> <property name = "manifest-xml" value = "androidmanifest. xml"/> <! -- Source file directory --> <property name = "resource-Dir" value = "res"/> <property name = "asset-Dir" value = "assets"/> <! -- Java source file directory --> <property name = "srcdir" value = "src"/> <property name = "srcdir-ospath" value = "$ {basedir}/$ {srcdir} "/> <! -- Directory of the external class library --> <property name = "external-lib" value = "lib"/> <property name = "external-Lib-ospath" value = "$ {basedir }/$ {external-lib} "/> <! -- Generate the class directory --> <property name = "outdir-classes" value = "$ {outdir-bin}"/> <property name = "outdir-classes-ospath" value = "$ {basedir}/$ {outdir-classes}"/> <! -- Classes. dex variables --> <property name = "Dex-file" value = "classes. dex "/> <property name =" Dex-path "value =" $ {outdir-bin}/$ {Dex-file} "/> <property name =" Dex-ospath "value =" $ {basedir}/$ {Dex-path} "/> <! -- The resource package file generated by aapt --> <property name = "resources-package" value = "$ {outdir-bin}/resources. AP _ "/> <property name =" resources-package-ospath "value =" $ {basedir}/$ {resources-package} "/> <! -- Unauthenticated APK package --> <property name = "out-Unsigned-package" value = "$ {outdir-bin}/commandid appnameapps-unsigned.apk"/> <property name = "out-Unsigned -Package-ospath "value =" $ {basedir}/$ {out-Unsigned-package} "/> <! -- Certificate file --> <property name = "keystore-file" value = "$ {basedir}/release. keystore"/> <! -- Authenticated APK package --> <property name = "out-signed-package" value = "$ {outdir-bin}/${appnameapps.apk"/> <property name = "out-signed -Package-ospath "value =" $ {basedir}/$ {out-signed-package} "/>... </Project>

Next, we will perform this step by step. The first step is initialization:

<! -- Initialization --> <target name = "init"> <echo> initializing all output directories... </echo> <Delete dir = "$ {outdir-bin}"/> <mkdir dir = "$ {outdir-bin}"/> <mkdir dir = "$ {outdir -Classes} "/> </Target>

The second is to generate the R. Java file:

<! -- Generate r according to the resource file in the project. java file --> <target name = "gen-R" depends = "init"> <echo> generating R. java from the resources... </echo> <exec executable = "$ {aapt}" failonerror = "true"> <Arg value = "package"/> <Arg value = "-F"/> <arg value = "-M"/> <Arg value = "-J"/> <Arg value = "$ {outdir-gen}"/> <Arg value = "-S "/> <Arg value =" $ {resource-Dir} "/> <Arg value ="-M "/> <Arg value =" $ {manifest-XML }"/> <Arg value = "-I"/> <Arg value = "$ {Android-jar}"/> </exec> </Target>

Then aidl generates the Java source file:

<! -- Compile the aidl file --> <target name = "aidl" depends = "gen-R"> <echo> compiling. aidl into java files... </echo> <apply executable = "$ {aidl}" failonerror = "true"> <! -- Specify the pre-processing file --> <Arg value = "-p $ {framework-aidl}"/> <! -- Directory declared by aidl --> <Arg value = "-I $ {srcdir}"/> <! -- Target file directory --> <Arg value = "-o $ {outdir-gen}"/> <! -- Specify which files need to be compiled --> <fileset dir = "$ {srcdir}"> <include name = "**/*. aidl "/> </fileset> </apply> </Target>

We have specified a framework. aidl, which defines a lot of Android built-in objects. Then we have specified the directory where aidl is located and the output directory. After the group, we have specified a file with the suffix aidl for compilation.

Next, compile the source file into a class file:

<! -- Compile the Java source file in the project into a class file --> <target name = "compile" depends = "aidl"> <echo> compiling Java source code... </echo> <javac encoding = "UTF-8" target = "1.5" srcdir = ". "destdir =" $ {outdir-classes} "bootclasspath =" $ {Android-jar} "> <classpath> <fileset dir =" $ {external-lib} "includes =" *. jar "/> <filelist> <file name =" $ {Android-maps-jar} "/> </filelist> </classpath> </javac> </Target>

If a third-party class library is used, we can configure it under the classpath label.

Next, convert the class file to classes. DEX:

<! -- Set. class file. dex file --> <target name = "Dex" depends = "compile"> <echo> converting compiled files and external libraries into. dex file... </echo> <exec executable = "$ {DX}" failonerror = "true"> <Arg value = "-- Dex"/> <! -- Output file --> <Arg value = "-- output =$ {Dex-ospath}"/> <! -- Generate. dex file source classes and Libraries --> <Arg value = "$ {outdir-classes-ospath}"/> <Arg value = "$ {external-Lib-ospath }"/> </exec> </Target>

Like the code above, if you use a third-party class library, you can append it in the form of the last parameter.

Then, package the resource file:

<! -- Put the resource file into the output directory --> <target name = "package-res-and-assets"> <echo> packaging resources and assets... </echo> <exec executable = "$ {aapt}" failonerror = "true"> <Arg value = "package"/> <Arg value = "-F"/> <arg value = "-M"/> <Arg value = "$ {manifest-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>

Then, package the package into a visa-free APK package:

<! -- Package into an unapproved APK --> <target name = "package" depends = "Dex, package-res-and-assets "> <echo> packaging unsigned APK for release... </echo> <exec executable = "$ {apkbuilder}" failonerror = "true"> <Arg value = "$ {out-Unsigned-package-ospath}"/> <ARG value = "-U"/> <Arg value = "-z"/> <Arg value = "$ {resources-package-ospath}"/> <Arg value = "- f "/> <Arg value =" $ {Dex-ospath} "/> <Arg value ="-RF "/> <Arg value =" $ {srcdir-ospath }" /> </exec> <echo> it will need to be signed with jarsigner before being published. </echo> </Target>

Then there is an APK visa:

<! -- Visa APK --> <target name = "jarsigner" depends = "package"> <echo> packaging signed APK for release... </echo> <exec executable = "$ {jarsigner}" failonerror = "true"> <Arg value = "-keystore"/> <Arg value = "$ {keystore-File} "/> <Arg value ="-storepass "/> <Arg value =" 123456 "/> <Arg value ="-keypass "/> <Arg value =" 123456" /> <Arg value = "-signedjar"/> <Arg value = "$ {out-signed-package-ospath}"/> <Arg value = "$ {out-unsig Ned-package-ospath} "/> <! -- Do not forget the certificate alias --> <Arg value = "release"/> </exec> </Target>

Last release:

<! -- Release --> <target name = "release" depends = "jarsigner"> <! -- Delete the unapproved APK --> <delete file = "$ {out-Unsigned-package-ospath}"/> <echo> APK is released. path: $ {out-signed-package-ospath} </echo> </Target>

This completes the editing of build. xml. Eclipse inherits ant, so we can run it directly in eclipse or call it in code.

First, we need to download ant, configure the corresponding environment variable information, and finally call it like this:

Process p = Runtime.getRuntime().exec("ant.bat -buildfile d:/workspace/ant/build.xml");InputStream is = p.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String line = null;while ((line = br.readLine()) != null) {System.out.println(line);}System.out.println("SUCCESS.");

Let's talk about it first. Thank you.

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.