Ant automatically compiles the package & release Android Project

Source: Internet
Author: User
Tags echo message

While eclipse is easy to use, compiling and packaging Android projects is slow, especially when it comes to distributing apps to various channels, it's a little unrealistic to use eclipse to manually package the various channel packs, and then we use ant to help us automatically compile and pack.

1 ant Auto-compile packaged Android Project 1.1 ant installation

Ant installation is relatively simple, download ant compression package http://ant.apache.org (the latest version of 1.9.3), after downloading it to a directory (I extracted to E:\Program files\apache-ant-1.9.3), Then configure the environment variable (create a new ant_home environment variable, the value is the directory where ant resides, and then add the Ant_home/bin to path),

Open the command-line tool, enter Ant-version, and if the following results appear, the ant installation is successful.

1.2 Build ant configuration for Android project Build.xml

Open the command line tool, switch the path to the directory where the project is located; Enter a command

Android Update Project--name <project_name>--target <target_id>--path <path_to_your_project>

Http://developer.android.com/tools/projects/projects-cmdline.html

The project root directory has more build.xml, and local.properties two files.

where local.properties the directory of our Android SDK (which is actually the value of the environment variable android_home, so if this is not in the environment variable, increase).

Build.xml is the most important ant build script, open a look, found that in fact, most of them are written comments, useful not a few lines, this is because the generated build.xml refers to the Android SDK built-in script, the directory is {SDK directory}/tools/ Ant/build.xml.

In this way, the project supports ant compilation packaging, but someone might say that my project has a jar package that references a third party and also references other Android library projects.

1.3 Android When using ant packaging, add third-party jar packages 1.3.1 third-party jar packages in Libs library

If the project simply refers to a third-party jar package, it is OK to place the jar package in the Libs folder, and Ant will automatically add the third-party jar in the process of compiling the package. But when our Android project referenced other library projects, we initially had to enter the Android Update command with one more parameter--subprojects:

You find the error, do not worry, this is because the library does not support ant automatic compilation, we need to let it also support.

Go to the directory where the Library project is located, enter the command Android update lib-project-p./(note is lib-project);

Go back to the original project and enter the command "Android Update Project--name menudrawsample-p./--subprojects", this will be OK.

1.3.2 third-party jar packages in user library libraries

In Android development, in addition to the usual compilation methods in eclipse, sometimes it may be necessary to automate the compilation with Ant for continuous integration. The Android SDK itself already provides the default ant compilation script, which is referenced in the build.xml of each project, which references the SDK's compilation script ${sdk_dir}/tools/ant/build.xml.

Normally, you can perform a normal build with ant debug directly in the project root directory. The default classpath will include all jar files in the Libs directory. However, if the user library is used in the project, or if the external jar file is referenced, there may be a problem in the compilation because these jar files, such as the user library, are not automatically included in the Classpath, and the ant's Path variable is extended. Add your own jar file to the classpath.

By looking at the Build.xml compilation script provided by the SDK, you can see that the CLASSPATH definition used by Javac is as follows:

<path id= "Project.javac.classpath" >    <path refid= "Project.all.jars.path" ></path>    < Path refid= "Tested.project.classpath" ></path></path><javac encoding= "${java.encoding}"        Source= "${java.source}" target= "${java.target}"        debug= "true" extdirs= "" Includeantruntime= "false"        Destdir = "${out.classes.absolute.dir}"        bootclasspathref= "Project.target.class.path"        verbose= "${verbose}"        classpathref= "Project.javac.classpath"        fork= "${need.javac.fork}" >    <src path= "${ Source.absolute.dir} "></src>    <src path=" ${gen.absolute.dir} "></src>    < Compilerarg line= "${java.compilerargs}" ></compilerarg></javac>

Where Project.all.jars.path contains all the jar files, we can introduce additional jar files by redefining this variable in the Build.xml of the project directory.

For example, in my project, referencing the Ormlite ORM Library, in order to be able to use "attach source" to view the source code in development, the jar file cannot be placed in the Libs directory because Eclipse does not allow the jar file in the Libs directory "attach Source ".

So I put this file in the Libs/ormlite directory, in order to be able to add these two jar files to the classpath, it is necessary to redefine project.all.jars.path this element.

The basic idea is to redefine the target of-pre-compile, in which the value of Project.all.jars.path is redefined.

Where is this code written? The custom_rules.xml in the current directory is referenced in the build.xml of each project, so we create a custom_rules.xml under the project root directory, as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><project name= "Custom_rules" default= "release" ><target name= "- Pre-compile "><echo message=" Jarpath=${tostring:project.all.jars.path} "></echo><echo message=" Jarpath=${jar.libs.dir} "></echo>    <property name=" Ormlite.dir "value=" ${jar.libs.dir}/ormlite " > </property>    <path id= "Ormlite.lib" >        <path path= "${tostring:project.all.jars.path}" ></path>        <pathelement location= "${ormlite.dir}/ormlite-android-4.41.jar" ></pathelement >         <pathelement location= "${ormlite.dir}/ormlite-core-4.41.jar" ></pathelement>    </path >    <path id= "Project.all.jars.path" >        <path refid= "Ormlite.lib" ></path>    </path >    <echo message= "Jarpath=${tostring:project.all.jars.path}" ></echo></target></ Project>

http://my.oschina.net/yunfound/blog/169288

1.4 Compiling a packaged project

Ant debug: Generates a beta apk, which is signed by default with Debug key, and the generated apk (your_project_name-debug.apk) is in the bin directory.

Ant release: Generates an unsigned and aligned APK package, Project_name-release-unsigned.ap and project_name-release-unaligned.apk in the bin directory.

2 Signature and channel package

Based on Ant Auto-compilation, you can package your existing Android project by adding signature information and automatically packaging the channel pack when your ant packs your app.

2.1 Adding signature information

In the project root directory to build a ant.properties file, enter the following, where the keystore password and alias password can not be specified (anti-leakage), then in the process of command execution will ask you to enter.

#keystore的路径, you must use a forward slash  key.store= "E:/wp_android_sample/me.key" #keystore的密码  #key. store.password=*****# Alias name  key.alias=me#alias password  

Running the ant release command under the project root will help you generate a signed and aligned APK, the generated apk (your_project_name-release.apk) in the bin directory

2.2 Automatic Packaging Channel package

Implementing bulk cycle wrapping requires a function similar to for loops, where there is no related for loop task in the ant core package, that is, the for loop is not supported, but Ant supports third-party expansion packs to support additional features.

So we're going to download the appropriate extension package that supports the for loop. You can use the Open source Ant-contrib package. : http://ant-contrib.sourceforge.net/.

The downloaded jar files are placed into the Ant's Lib directory. Next we can package the channel package, the specific method is:

(1) First add attribute Market_channels (channel list, comma-separated) in Ant.properties file, version (application version name)

#渠道市场列表  market_channels=91,360,wandoujia,baidu  #版本号  version=1.2.1  

(2) Add the following code to the Build.xml of our project:

<!--channel Pack packaging script ant deploy--> <taskdef resource= "Net/sf/antcontrib/antcontrib.properties" > <classpath > <pathelement location= "Lib/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <t Arget name= "Deploy" > <foreach target= "modify_manifest" list= "${market_channels}" param= "Channel" delimiter= "," > </foreach> </target> <target name= "Modify_manifest" > <replaceregexp flags= "G" by Line= "false" > <!--match is android:value= "* * * * * * * *" android:name= "Umeng_channel"-<regexp Pat Tern= ' android:value= ' (. *) "android:name=" Umeng_channel "'/> <!--match and replace it with android:value=" channel name "Android:name = "Umeng_channel"--<substitution expression= ' android:value= "${channel}" android:name= "UMENG_CHANNEL" '/&G         T <!--regular expressions need to match files of Androidmanifest.xml---<fileset dir= "" includes= "Androidmanifest.xml"/> &lt ;/replaceregexp> <property name= "out.release.file" location= "${out.absolute.dir}/${ant.project.name}_${channel}.apk"/> <!--Pack--<antcall target= "release"/> <!--output channel package to bin/out directory--<copy tofile= "${o ut.absolute.dir}/out/${ant.project.name}v${version}-${channel}.apk "file=" bin/${ant.project.name}-release.apk "/   > </target>

Running the ant deploy command at the root of the project will help you to sign the package for each channel (for the full automatic execution, the password for the KeyStore in the Ant.properties file can be specified so that you do not need to enter the password manually during execution), In the Out directory of the bin directory.

There was an error running the Ant Bulk Packaging tool in eclipse with the following log information:
D:\android\android-sdk-windows\tools\ant\build.xml:601:the following error occurred while executing the line:
D:\android\android-sdk-windows\tools\ant\build.xml:720:the following error occurred while executing the line:
D:\android\android-sdk-windows\tools\ant\build.xml:734:unable to find a javac compiler;
Com.sun.tools.javac.Main isn't on the classpath.
Perhaps Java_home does not point to the JDK.
It is currently set to "D:\Program Files\java\jre7″

Solution:

1:eclipse Menu –window–preferences–java–installed jres– The JRE entry in the selected column table –edit–add External jars– Select the JDK directory/lib/tools. Jar-Confirm that it joins the JRE system Libraries–finish–ok

2:ant Clean

3:ant Release

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.