I am developing a Java program that can build Android apk files, then install it to an emulator, then running tests. The code is listed below:
/*
* Generate APK through ANT API Method
*/
public static void generateApkThroughAnt(String buildXMLPath) {
File antBuildFile = new File(buildXMLPath);
Project p = new Project();
p.setUserProperty("ant.file", antBuildFile.getAbsolutePath());
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(ps);
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("debug"); // build a new apk file
//p.executeTarget("test"); // run test against project
//p.executeTarget("installd"); //install new apk file to vd
try {
// test for capturing output results
String output = os.toString("UTF8");
System.out.println("this is captured output: "+ output);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (BuildException e) {
ex = e;
} finally {
p.fireBuildFinished(ex);
}
}
So far, with Apache Ant, my program is able to do the above tasks without problems. Thanks to the answers in this post Run ant from Java
However, more and more Android open source apps are using gradle now. I am wondering if it's possible to achieve the above similar tasks with gradle? Does gradle have similar API that I can use? Thanks!