Android ant automatic Batch packaging-with third-party project library

Source: Internet
Author: User
Tags version control system

If you need to keep packing every time you pack, by constantly copying and pasting, when you hit hundreds of packs, I'm sure you'll go crazy, and of course, this kind of thing can be solved by our programmers, because our job is to make the tedious work easier. Today we need to briefly introduce the problem of ant batch packing.

The information you see on the Internet has its merits, now is the time to do a summary, standing on the shoulders of the great God we can see farther.

First of all, you should go to the official website to download the latest version of Apache-ant, my side used a apache-ant-1.9.4, which is the current version of the latest, now attached download link-can go to my resources to download, download link: http// download.csdn.net/detail/litianpeng1991/8396457

After the download, we start to configure the environment variables, the following is the configuration process:

This is my ant's Path:


To configure environment variables:



After the configuration is ready, we can go to cmd to verify:


If the above situation indicates that Ant has been configured successfully, then we have to implement a jar package--ant-contrib-1.0b3.jar in order to be able to package in batches (loops).

Everyone can go to my Resources page download, attached download link--http://download.csdn.net/detail/litianpeng1991/8396465

After the download is done, you must change the suffix name from zip to jar, and then copy the jar package to the apche-ant-1.9.4/lib/folder.


Then it's our turn to project into the entry.

1, first we enter the project directory, enter the command, the command format is as follows:

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

Http://developer.android.com/tools/projects/projects-cmdline.html specific If you do not understand the site can refer to Google's official API documentation

2, we will be third-party jar package can be placed directly under the Libs folder, Android in the compilation of packaging can automatically add these jars, but we also have to consider a situation, that is, Android project is to support the reference to the third-party project library, is the third-party library is in the form of project, what should we do?

Here's how to fix it: (The following commands are always in the project directory, do not enter the wrong directory)

Android Project--name item name-p./--subprojects

There must be a problem at this point:

Error:the project either have no target set or the target is invalid,

Provide a--target to the "android.bat Update" command.

This is because our third-party project library is actually needed for ant setup,

At this time we are in the directory of third-party projects: run the following command

Android Update third-party project name-P./

After running this, we should have two more files in the third-party project directory Build.xml and local.properties two files

We then run the original command in the original project directory, but if the red flag is still present, you can run the following command:

Android Update project-p. -T Android-15-s (where this android-15 can be casually written, write your own target)

At this point we will find two newly generated files under our project name, namely Build.xml and local.properties

Build.xml content is as follows:

<?xml Version= "1.0" encoding= "UTF-8"? ><project name= "<span style=" Background-color:rgb (255, 0, 0); " > Project name </span> "default=" Help > <property file= "local.properties"/> <property file= "Ant.prope Rties "/> <property environment=" env "/> <condition property=" Sdk.dir "value=" ${env. Android_home} "> <isset property=" env. Android_home "/> </condition> <loadproperties srcfile=" project.properties "/> <fail mess Age= "Sdk.dir is missing.  Make sure to generate Local.properties using ' Android Update Project ' or to inject it through the android_home environment        Variable. "  unless= "Sdk.dir"/> <import file= "Custom_rules.xml" optional= "true"/> <!--version-tag:1 --<import file= "${sdk.dir}/tools/ant/build.xml"/></PROJECT> 
The contents of the Local.properties file are as follows:

# This file was automatically generated by Android tools.# does not modify this file--YOUR changes would be erased!## this F  Ile must *not* be checked into Version Control systems,# as it contains information specific to your local configuration.# Location of the SDK. This was only used by ant# for customization if using a Version Control System, please read the# header note.sdk.dir=d:\\ Android_developer\\androidsdk\\sdk

That's when we started writing custom_rules.xml--This is our new XML file, the red part below, to put the jar package we downloaded above into Libs

< /span>

<?xml version= "1.0" encoding= "UTF-8"?><!--Channel Pack packaging script ant deploy--><project name= "Custom_rules" > <        !--referencing Jar Pack Ant-contrib-1.0b3.jar---<taskdef resource= "Net/sf/antcontrib/antcontrib.properties" > <classpath> <span style= "color: #ff0000;"     ><pathelement location= "Libs/ant-contrib-1.0b3.jar"/></span> </classpath> </taskdef> <!--cyclic packaging, target named deploy, must be built with deploy---<target name= "Deploy" > <!--come in and clean the project first--&        Gt <antcall target= "clean"/> <!--the For loop content is passed in with a parameter of Market_channels, a "," builds a list for the separator, loops through the list, and passes the read string to the parameter channel Into Modify_manifest--<foreach delimiter= "," list= "${market_channels}" param = "Channel" target= "Modify_manifest" > </foreach> </target> <target name= "Modify_ Manifest "> <!--get channel name by regular match, assign to ChannelName--&LT;PROpertyregex input= "${channel}" Override= "true" property= "ChannelName" regexp= " (. *): "select=" \1 "/> <!--get channel number by regular match, assign to Channelkey--<propertyregex INP ut= "${channel}" Override= "true" property= "Channelkey" regexp= ":(. *)" select= "\ 1 "/> <!--replace the android:name=" ChannelName "in Androidmanifest.xml with Android:name=" ${channelname} ", where ${            ChannelName} is what we just matched above--<replaceregexp byline= "true" encoding= "Utf-8" File= "Androidmanifest.xml" match= "android:name=" ChannelName "" replace= "Android:name=" ${channelname} ""/> <!--replace android:value= "Channelkey" in Androidmanifest.xml with Android:value= "${channelkey}"--&L T;replaceregexp byline= "true" encoding= "Utf-8" file= "Androidmanifest.xml" MATC     H= "android:value=" Channelkey ""       Replace= "android:value=" ${channelkey} ""/> <!--with Release package--<antcall target= "release" /> <!--copy Channel package to the specified directory,            Out.absolute.dir is defined in its own build.xml, for the bin directory, and if you want to modify it, you can add a row out.absolute.dir= the specified directory in local.properties-<copy File= "${out.absolute.dir}/${ant.project.name}-release.apk" tofile= "${OUT.ABSOLUTE.DIR}/OUT/${ANT.PROJECT.N ame}-v${version}-${channelkey}.apk "/> <!--after a channel pack has been packaged, replace the channel name and channel number with the original format so that the next replacement can be            All matching items in Androidmanifest.xml will be replaced-<replaceregexp byline= "true" encoding= "Utf-8" File= "Androidmanifest.xml" match= "Android:name=" ${channelname} "replace=" Android:name= "Chann Elname ""/> <replaceregexp byline= "true" encoding= "Utf-8" file= "androidmanif Est.xml "match=" android:value= "${channelkey}" "Replace=" android:value= "Channelkey" "/> </ta Rget></projecT> 
Next you need to create a new ant.properties configuration file:

Key.store=key.store path key.store.password= Password key.alias= write your aliaskey.alias.password= password #proguard.config=${sdk.dir}/ TOOLS/PROGUARD/PROGUARD-ANDROID.TXT:PROGUARD-PROJECT.TXTMARKET_CHANNELS=UMENG_CHANNEL:3000,UMENG_CHANNEL:3001, umeng_channel:3002version=1.1.2
After writing this, we also need to change the androidmanifest.xml in one place:

        <meta-data android:name= "ChannelName" android:value= "Channelkey"/>

Remember, let this be in a row, even a space to control, because the match has been written in the relevant configuration.

Then everyone can run, but if it happens in the run--because a lot of warnings cause errors,

Then everyone can take a look, is not to write their own confusing files, can not say just joined the jar package confusion,

If there is still a problem, then directly add on the ignore warnings on it, thank you for your welcome, I hope you can enjoy a lot. Go up, Junior.




Android Ant automatic Bulk packaging-with a third-party project library

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.