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;
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, "." is 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 ));
// Key code
Projecthelper. configureproject (project, new file (_ buildfile ));
}
Public void runtarget (string _ target) throws exception {
// Test if the project exists
If (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 target
Project.exe cutetarget (_ target );
}
Private Final Static string projectbasepath = "F: \ mywork \ hello"; // the root directory of the project to be packaged
Private Final Static string copyapkpath = "f :\\ antapks"; // Save the root directory of the packaged APK
Private Final Static string signapk = "Hello-release.apk"; // the file name here must be an accurate project name!
Private Final Static string renameapk = "Hello _"; // 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)
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 batch automatic packaging starts ----------");
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. xml.
String tempfilepath = projectbasepath + file. 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. runtarget ("clean ");
Mytest. runtarget ("release ");
// Rename and copy the package
File file = new file (projectbasepath + file. Separator + "bin"
+ File. Separator + signapk); // The signed APK file in the bin directory
File renamefile = new file (copyapkpath + file. Separator + renameapk
+ Flag + ". APK ");
Boolean renametag = file. renameto (renamefile );
System. Out. println ("RENAME ------>" + renametag );
System. Out. println ("file ------>" + file. getabsolutepath ());
System. 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 consumed:" + getbeapartdate (totaltime ));
} Catch (exception e ){
E. printstacktrace ();
System. Out. println ("--------- ant exceptions occurred in batch automatic packaging ----------");
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 ));
}
}
/**
* Calculates the time difference based on the number of seconds and returns the result in ** hour ** minute ** seconds.
*
* @ Param d1
* @ Param D2
* @ Return
*/
Public static string getbeapartdate (long m ){
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 ){
Bufferedreader 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 in the buffer object
While (line = Br. Readline ())! = NULL ){
// Modify the content of some rows as needed.
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 based on the file path
BW = new bufferedwriter (New filewriter (filepath ));
// Write the content to 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;
}
}
}
}
}
Need to import ant. jar and ant-launcher.jar package