您可以使用 ANT 罐子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=>Right click=>Export=>General=>Ant Buildfiles。之后,您将需要运行:
android update project --name <project_name> --target <target_ID> --path <path_to_your_project>