This section describes how to automatically build the Ant tool, package applications in batches, and generate applications in different markets:
First, let's take a look at the Java project AntTest for packaging and the Android project structure to be packaged and released: market.txt to save the market logo to be packaged, such as: youmenggfan ....... add the channel name as needed in this file. next, let's take a look at the content in the batch packaging AntTest class:
Package com.cn. ant; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. file; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; import java. text. simpleDateFormat; import java. util. calendar; import org. apache. tools. ant. defaultLogger; import org. apache. tools. ant. project; import org. apache. tools. ant. projectHelper; public class AntTest {private Project proje Ct; public void init (String _ buildFile, String _ baseDir) throws Exception {project = new Project (); project. init (); DefaultLogger consoleLogger = new DefaultLogger (); consoleLogger. setErrorPrintStream (System. err); consoleLogger. setOutputPrintStream (System. out); consoleLogger. setMessageOutputLevel (Project. MSG_INFO); project. addBuildListener (consoleLogger); // Set the base directory. if none is given ,". "I S used. if (_ baseDir = null) _ baseDir = new String (". "); project. setBasedir (_ baseDir); if (_ buildFile = null) _ buildFile = new String (projectBasePath + File. separator + "build. xml "); // ProjectHelper. getProjectHelper (). parse (project, new // File (_ buildFile); <span style = "color: # FF0000;"> // key code </span> ProjectHelper. configureProject (project, new File (_ buildFile);} public void runTarget (String _ target) thr Ows Exception {// Test if the project existsif (project = null) throw new Exception ("No target can be launched because the project has not been initialized. please call the 'init 'method first! "); // If no target is specified, run the default one. if (_ target = null) _ target = project. getDefaultTarget (); // Run the targetproject.exe cuteTarget (_ target);} <span style = "color: # FF0000;"> private final static String projectBasePath = "D: androidworkspace3XXX "; // The project root directory private final static String copyApkPath = "D: androidapktest"; // Save the root directory private final static String signApk = "XXX-release.apk ";/ /The file name must be an accurate project name! Private final static String reNameApk = "XXX _"; // the prefix of the renamed project name (Map project does not need to be changed) private final static String placeHolder = "@ market @"; // The place where the manifest file needs to be modified (placeholder) </span> public static void main (String args []) {long startTime = 0L; long endTime = 0L; long totalTime = 0L; Calendar date = Calendar. getInstance (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd: HH: mm: ss"); try {System. out. println ("--------- ant automatic batch packaging Start ---------- "); startTime = System. currentTimeMillis (); date. setTimeInMillis (startTime); System. out. println ("Start Time:" + sdf. format (date. getTime (); BufferedReader br = new BufferedReader (new FileReader ("market.txt"); String flag = null; while (flag = br. readLine ())! = Null) {// modify the manifest file first: Read @ market @ from the temporary file and change it to the market ID, and then write it to manifest. string tempFilePath = projectBasePath + File in xml. separator + "AndroidManifest. xml. temp "; String filePath = projectBasePath + File. separator + "AndroidManifest. xml "; write (filePath, read (tempFilePath, flag. trim (); // execute the packaging command AntTest mytest = new AntTest (); mytest. init (projectBasePath + File. separator + "build. xml ", projectBasePath); mytest. runTa Rget ("clean"); mytest. runTarget ("release"); // after the package is completed, rename and copy the File file = new File (projectBasePath + File. separator + "bin" + File. separator + signApk); // File renameFile = new File (copyApkPath + File) in the apk File signed under the bin directory. separator + reNameApk + flag + ". apk "); boolean renametag = file. renameTo (renameFile); System. out. println ("rename ------>" + renametag); System. out. println ("file ------>" + file. getAbsolutePath (); S Ystem. out. println ("rename ------>" + renameFile. getAbsolutePath ();} System. out. println ("--------- ant batch automatic packaging ends ----------"); endTime = System. currentTimeMillis (); date. setTimeInMillis (endTime); System. out. println ("End Time:" + sdf. format (date. getTime (); totalTime = endTime-startTime; System. out. println ("time-consuming:" + getBeapartDate (totalTime);} catch (Exception e) {e. printStackTrace (); System. out. println ("---- ----- Ant batch automatic packaging exception ---------- "); endTime = System. currentTimeMillis (); date. setTimeInMillis (endTime); System. out. println ("exception occurred at:" + sdf. format (date. getTime (); totalTime = endTime-startTime; System. out. println ("Time consumed:" + getBeapartDate (totalTime) ;}}/*** based on the number of seconds, calculates the time difference and returns ** @ param d1 * @ param d2 * @ return */public static String getBeapartDate (long m) in ** minutes) {m = m/1000; String beapartdate = ""; Int nDay = (int) m/(24*60*60); int nHour = (int) (m-nDay * 24*60*60) /(60*60); int nMinute = (int) (m-nDay * 24*60*60-nHour * 60*60)/60; int nSecond = (int) m-nDay * 24*60*60-nHour * 60*60-nMinute * 60; beapartdate = nDay + "day" + nHour + "Hour" + nMinute + "Minute" + nSecond + "second"; return beapartdate;} public static String read (String filePath, string replaceStr) {Buff EredReader br = null; String line = null; StringBuffer buf = new StringBuffer (); try {// create a buffer input stream based on the file path br = new BufferedReader (new FileReader (filePath )); // read each row of the file cyclically, modify the row to be modified, and put it into the buffer object while (line = br. readLine ())! = Null) {// modify the content of some rows if (line. contains (placeHolder) {line = line. replace (placeHolder, replaceStr); buf. append (line);} else {buf. append (line);} buf. append (System. getProperty ("line. separator ") ;}} catch (Exception e) {e. printStackTrace ();} finally {// close the stream if (br! = Null) {try {br. close () ;}catch (IOException e) {br = null ;}} return buf. toString ();}/*** write the content back to the file ** @ param filePath * @ param content */public static void write (String filePath, String content) {BufferedWriter bw = null; try {// create a buffer output stream bw = new BufferedWriter (new FileWriter (filePath) based on the file path; // write the content into the file bw. write (content);} catch (Exception e) {e. printStackTrace ();} finally {// close the stream if (bw! = Null) {try {bw. close () ;}catch (IOException e) {bw = null ;}}}}}
The following is the part to be modified in the Android project:
1. Modify the sdk root directory in local. properties:
Sdk. dir = D: androidandroid-sdk-windows-r17android-sdk-windows-r17
2. Modify the path and password of the signature file in ant. properties (if needed)
Key. store = D: androidmykeystore
Key. store. password = 123456
Key. alias = mykey
Key. alias. password = 123456
3. Modify AndroidManifest. xml. temp
Copy AndroidManifest. xml and name it AndroidManifest. xml. temp.
Change the place to be replaced to a placeHolder, which must be consistent with the placeHolder constant in the package project AntTest.
For example: <meta-data android: value = "@ market @" android: name = "UMENG_CHANNEL"/>
4. In Build. xml:
<Project name = "XXX" default = "help">, XXX must be the Android project name.
If the Ant environment variable is not configured on the machine, perform the following steps:
ANT environment variable settings:
In Windows, ANT mainly uses two environment variables: ANT_HOME and PATH.
Set ANT_HOME to point to the ant installation directory.
Setting method:
ANT_HOME = D:/apache_ant_1.7.0
Add % ANT_HOME %/bin; % ANT_HOME %/lib to the path of the environment variable.
Setting method:
PATH = % ANT_HOME %/bin; % ANT_HOME %/lib