简体   繁体   中英

Running Gradle Build Script from Java Desktop Application for Android App

I'm trying to automate building of an Android app through a Java Desktop app by pressing a build button - as I need to change some manifest values, and string values. All that is done, but I need to perform a "gradle assembleRelease -pMyProjectPath," but I keep running into issues. Here are the combinations I've tried:

Test 1

 ProcessBuilder builder = new ProcessBuilder("call", "gradle", "assemble ", "-p" , projectPath);

Output for Test 1:

java.io.IOException: Cannot run program "call": CreateProcess error=2, The system cannot find the file specified

I tried the "call" since from what I understand, gradle isn't an executable (it was a shot in the dark)

Test 2

 ProcessBuilder builder = new ProcessBuilder( "C:\\developer\\tools\\gradle-2.3\\bin\\gradle", "assemble ", "-p"+projectPath);

Output for Test 2

java.io.IOException: Cannot run program "C:\\developer\\tools\\gradle-2.3\\bin\\gradle": CreateProcess error=193, %1 is not a valid Win32 application

gradle is in my environment path.

Test 3

 ProcessBuilder builder = new ProcessBuilder("cmd", "gradle", "assemble ", "-p" + projectPath);

Output for Test 3: -- None. There is no output. Don't know if it's running.

One the big issues is that I didn't include the "/C" in the ProcessBuilder paramter.

And here's my working version for compiling with gradle from UI:

  private void startGradleExecutorService() {
    statusLabel.setText("Building...");
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.submit(() -> {

        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/C", projectPath + "/gradlew assembleRelease -p" + projectPath, "--info");
        builder.redirectErrorStream(true);
        try {
            Process p = builder.start();
            BufferedReader stdout = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));

            System.out.println("outputString:: " + stdout.readLine());

            while ((outputString = stdout.readLine()) != null) {
                System.out.println("outputString:: " + outputString);
                Platform.runLater(() -> {
                    //if you change the UI, do it here !

                    statusLabel.setText(outputString);

                });
                if (outputString.contains("BUILD")) {
                    break;
                }
            }
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
            p.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM