Reputation: 503
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.
Upvotes: 2
Views: 1088
Reputation: 503
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();
}
});
}
Upvotes: 2