Android uses ant to automatically package (change the attribute values in the Java file and generate them in batches)

Source: Internet
Author: User

Many people have added QQ to ask me to pack ant, and now I want to write another easy-to-understand

Http://www.stay4it.com /? P = 284

----------------------------------- I can't read the complicated ones. =

 

 

 

AntPackaging references:

Http://hi.baidu.com/%F5%CC%C4%A7/blog/item/3f9bc5ec2338ad3726979186.html

Http://www.diybl.com/course/3_program/java/javajs/20090201/154692.html

Android antPackaging:

Http://marshal.easymorse.com/archives/1665

Http://handsomeliuyang.iteye.com/blog/1156070

Http://www.bangchui.org/simple? T13358.html

 

Here are some of my personal information on antSome Learning income during the packaging process andBuildGeneral idea of Files.

First, configure the required Ant Compiling environment , In Build. Property Configure relevant parameters . (Build. Property The file mainly contains some paths that need to be used in compilation. , And some parameters to be configured , For example, the application package name , Project name , And some parameters that need to be passed in . Written in Build. Property To facilitate centralized management of configuration files ) In this Ant Packaging Requirements , You need to modify Java Two constant attributes in the file . Based on such requirements , First Http://blog.csdn.net/to_cm/article/details/6051470 Normal Android Project package Build. XML, And Build. Property. It should be noted that , Because Android SDK tool, And Platform tools Some upgrades , Part Android The compression compilation tool of is transferred Platform _ tools Directory , So , You need to change the configuration slightly. Build. Property Content in

Then,Start to implement our needs..BecauseAntNot deeply understood,The method used is relatively dead,The efficiency may not be the highest.,Let's talk about my ideas.

<Target name = "copyreplacejava">

<Copy file = "$ {basedir }\$ {srcdir }\$ {file. replace. path} \ $ {filename} "todir = ".. \ temp \ build \ META-INF "/>

<Replace file = "$ {basedir }\$ {srcdir }\$ {file. replace. path} \ $ {filename} "token =" @ company_name @ "value =" $ {company. name} "encoding =" UTF-8 "/>

<Replace file = "$ {basedir }\$ {srcdir }\$ {file. replace. path} \ $ {filename} "token =" @ app_id @ "value =" $ {app. id} "encoding =" UTF-8 "/>

</Target>

First , We copy what we need to modify Java File to a temporary Temp Folder , Then SRC In Java File to replace characters , We use @ Company_name @ Replace the position with this special character , Prevents replacement of normal filesCode . Replaced , Then execute subsequent compilation , Compression , Package , In this case, the constant value in the output package is the value that we pass as a parameter. . Because the file is replaced @ Company_name @ special characters of this type , To pack normally next time , Copy Temp In Java File replaced . After packaging , We use this code to implement ( Note: Depends The parameter determines Target Execution sequence , Here we give Compile After )

<Target name = "replacejava" depends = "compile">

<Delete file = "$ {basedir }\$ {srcdir }\$ {file. Replace. Path }\$ {filename}"/>

<Copy file = ".. \ temp \ build \ META-INF \ $ {filename} "todir =" $ {basedir }\$ {srcdir} \ $ {file. replace. path} "/>

</Target>

Next,We need to clear unnecessary intermediate files generated,For exampleClassesFolder.

<Delete dir = "$ {basedir }\$ {outdir} \ Classes"/>

<Delete file = "$ {basedir }\$ {outdir} \ Classes. Dex"/>

<Delete file = "$ {basedir }\$ {outdir} \ jjdd. AP _"/>

 

Cleared, Automatically generate APK The package is generated. . Next , The demand has been further strengthened , We need 10 Several same company names , Different ID Package . So how can we automatically generate such packages? , Here I used DoS Command to fulfill our needs (Ant Although it can also be implemented , But it seems Ant Executed For Support for plug-ins is required , And Ant Not too familiar , So , Here I use DoS Do it ), The following code generates different names for the specified company. ID Of DoS Code .

CD/d F: \ workspace \ online \ trunk \ project_name

@ Echo off

 

Set/P x =Enter the market name of the product.:

Set/P min =MinimumIDThe value is:

Set/P max =MaxIDThe value is:

 

For/L % I in (% min %, 1, % max %) Do ant-F build. XML-dcompany. name = % x %-dapp. id = % I

 

Ant-F build. XML-dcompany. Name = % x %-dapp. ID = % I, This command is executed Ant, And Build. xml In Company. Name Value assignment Input X, To App. ID Assignment I. At the same time, the input is completed in a loop Min To Max Call Ant Package , Generate ID Different Ant Package .

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.