2

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!

Community
  • 1
  • 1
dmark
  • 320
  • 2
  • 13

2 Answers2

1

Here is the solution I found. Hope it can help anyone also has similar needs. Gradle tooling API perfectly solves it.

/*
 * perform Gradle tasks such as compile, build, test
 */
public static void performGradleTasks(String path, String ... tasks)
{
    GradleConnector connector = GradleConnector.newConnector();

    connector.forProjectDirectory(new File(path));

    ProjectConnection connection = connector.connect();
    try {
        // Configure the build
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks(tasks);
        launcher.setStandardOutput(System.out);
        launcher.setStandardError(System.err);

        // Run the build
        launcher.run();
    } finally {
        // Clean up
        connection.close();
    }
} 
dmark
  • 320
  • 2
  • 13
0

There is another way to sign your APK without modifying you build.gradle file which is zipalign and apksigner. For example, if the build tools you are using is 27.0.3, you can find them in [your android sdk folder]/build-tools/27.0.3. You can then use ProcessBuilder to have them executed in your java code. See zipalign apksigner

Jason
  • 46
  • 5