java 执行cmd 命令经常容易忘记,这次需要用在android中命令编译Gradle 所以做个笔记
package com.myunity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Created by Administrator on 2017/12/13.
*/
public class GenerateUnityAAR {
public static void main(String[] args) {
// gradle clean
// gradle assembleDebug
String mCurrentPath = System.getProperty("user.dir");
String mCurrentDrive = mCurrentPath.substring(0, 2);
String cmdCode = "cmd /c cd " + mCurrentPath + " && " + mCurrentDrive + " && gradle clean";
System.out.println("打印Code: >>> "+cmdCode);
exeCmd(cmdCode);
}
private static void exeCmd(String cmdCode) {
BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec(cmdCode);
br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}