运行 Gradle 从 Java 桌面应用程序为 Android 应用构建脚本
Running Gradle Build Script from Java Desktop Application for Android App
我正在尝试通过按 build 按钮通过 Java 桌面应用程序自动构建 Android 应用程序 - 因为我需要更改一些清单值和字符串值。所有这些都已完成,但我需要执行 "gradle assembleRelease -pMyProjectPath," 但我将 运行 放入问题中。以下是我尝试过的组合:
测试 1
ProcessBuilder builder = new ProcessBuilder("call", "gradle", "assemble ", "-p" , projectPath);
测试 1 的输出:
java.io.IOException: Cannot run program "call": CreateProcess error=2, The system cannot find the file specified
我尝试了 "call",因为据我了解,gradle 不是可执行文件(它是在黑暗中拍摄的)
测试 2
ProcessBuilder builder = new ProcessBuilder( "C:\developer\tools\gradle-2.3\bin\gradle", "assemble ", "-p"+projectPath);
测试 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 在我的环境路径中。
测试 3
ProcessBuilder builder = new ProcessBuilder("cmd", "gradle", "assemble ", "-p" + projectPath);
测试 3 的输出:
-- None。没有输出。不知道是不是运行。
一个大问题是我没有在 ProcessBuilder 参数中包含“/C”。
这是我使用 UI 中的 gradle 编译的工作版本:
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();
}
});
}
我正在尝试通过按 build 按钮通过 Java 桌面应用程序自动构建 Android 应用程序 - 因为我需要更改一些清单值和字符串值。所有这些都已完成,但我需要执行 "gradle assembleRelease -pMyProjectPath," 但我将 运行 放入问题中。以下是我尝试过的组合:
测试 1
ProcessBuilder builder = new ProcessBuilder("call", "gradle", "assemble ", "-p" , projectPath);
测试 1 的输出:
java.io.IOException: Cannot run program "call": CreateProcess error=2, The system cannot find the file specified
我尝试了 "call",因为据我了解,gradle 不是可执行文件(它是在黑暗中拍摄的)
测试 2
ProcessBuilder builder = new ProcessBuilder( "C:\developer\tools\gradle-2.3\bin\gradle", "assemble ", "-p"+projectPath);
测试 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 在我的环境路径中。
测试 3
ProcessBuilder builder = new ProcessBuilder("cmd", "gradle", "assemble ", "-p" + projectPath);
测试 3 的输出: -- None。没有输出。不知道是不是运行。
一个大问题是我没有在 ProcessBuilder 参数中包含“/C”。
这是我使用 UI 中的 gradle 编译的工作版本:
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();
}
});
}