您可以使用ANT jars ant.jar和ant-launcher.jar.
在这种情况下,应完全指定build.xml的路径.
以这种方式从Java类中调用它:
public class AntTest {
public static void main(String[] args) {
String build = "D:/xampp/htdocs/aud/TempProject/build.xml";
generateApkThroughAnt(build);
}
/*
* Generate APK through ANT API Method
*/
public static void generateApkThroughAnt(String buildPath) {
File antBuildFile = new File(buildPath);
Project p = new Project();
p.setUserProperty("ant.file", antBuildFile.getAbsolutePath());
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
BuildException ex = null;
try {
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, antBuildFile);
p.executeTarget("clean");
p.executeTarget("release");
} catch (BuildException e) {
ex = e;
} finally {
p.fireBuildFinished(ex);
}
}
}
要创建build.xml文件,请转到Eclipse => Your Project =>右键单击=> Export => General => Ant Buildfiles.
之后,您将需要运行:
android update project --name --target --path