Skip to content

Commit 1c1c462

Browse files
authored
Merge pull request #364 from rupak0577/avdmanager
Rupak's emulator fixes (using avdmanager instead of the android tool)
2 parents e9d1515 + efc47f0 commit 1c1c462

File tree

7 files changed

+185
-149
lines changed

7 files changed

+185
-149
lines changed

src/processing/mode/android/AVD.java

Lines changed: 112 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
package processing.mode.android;
2323

2424
import processing.app.Base;
25-
import processing.app.exec.ProcessHelper;
26-
import processing.app.exec.ProcessResult;
25+
import processing.app.Platform;
2726
import processing.core.PApplet;
2827

2928
import java.awt.Frame;
30-
import java.io.IOException;
29+
import java.io.*;
3130
import java.util.ArrayList;
3231
import java.util.HashMap;
3332
import java.util.List;
@@ -68,40 +67,39 @@ public class AVD {
6867

6968
static final String DEFAULT_SDCARD_SIZE = "64M";
7069

71-
static final String DEFAULT_SKIN = "WVGA800";
72-
static final String WEAR_SKIN = "AndroidWearSquare";
73-
7470
/** Name of this avd. */
7571
protected String name;
7672

7773
/** "android-7" or "Google Inc.:Google APIs:7" */
78-
protected String target;
74+
protected String sdkId;
7975

76+
static boolean invalidPackage;
8077
static ArrayList<String> avdList;
8178
static ArrayList<String> badList;
8279
// static ArrayList<String> skinList;
8380

8481
private Map<String, String> preferredAbi = new HashMap<>(30);
8582
private List<String> abiList = new ArrayList<>();
86-
private String skin;
83+
private static Process process;
8784

8885
/** Default virtual device used by Processing. */
8986
static public final AVD mobileAVD =
9087
new AVD("Processing-0" + Base.getRevision(),
91-
AndroidBuild.TARGET_PLATFORM, SysImageDownloader.SYSTEM_IMAGE_TAG, DEFAULT_SKIN);
88+
"system-images;" + AndroidBuild.TARGET_PLATFORM + ";" +
89+
SysImageDownloader.SYSTEM_IMAGE_TAG + ";x86", SysImageDownloader.SYSTEM_IMAGE_TAG);
9290
// "Google Inc.:Google APIs:" + AndroidBuild.sdkVersion);
9391

9492
/** Default virtual wear device used by Processing. */
9593
static public final AVD wearAVD =
9694
new AVD("Processing-Wear-0" + Base.getRevision(),
97-
AndroidBuild.TARGET_PLATFORM, SysImageDownloader.SYSTEM_IMAGE_WEAR_TAG, WEAR_SKIN);
95+
"system-images;" + AndroidBuild.TARGET_PLATFORM + ";" +
96+
SysImageDownloader.SYSTEM_IMAGE_WEAR_TAG + ";x86", SysImageDownloader.SYSTEM_IMAGE_WEAR_TAG);
9897

99-
public AVD(final String name, final String target,
100-
final String tag, final String skin) {
98+
public AVD(final String name, final String sdkId,
99+
final String tag) {
101100
this.name = name;
102-
this.target = target;
103-
this.skin = skin;
104-
initializeAbiList(tag);
101+
this.sdkId = sdkId;
102+
//initializeAbiList(tag);
105103
}
106104

107105
private void initializeAbiList(String tag) {
@@ -121,11 +119,22 @@ static protected void list(final AndroidSDK sdk) throws IOException {
121119
try {
122120
avdList = new ArrayList<String>();
123121
badList = new ArrayList<String>();
124-
ProcessResult listResult =
125-
new ProcessHelper(sdk.getAndroidToolPath(), "list", "avds").execute();
126-
if (listResult.succeeded()) {
122+
ProcessBuilder pb =
123+
new ProcessBuilder(sdk.getAvdManagerPath(), "list", "avd");
124+
Map<String, String> env = pb.environment();
125+
env.clear();
126+
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
127+
pb.redirectErrorStream(true);
128+
129+
process = pb.start();
130+
InputStream stdout = process.getInputStream();
131+
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
132+
process.waitFor();
133+
134+
if (process.exitValue() == 0) {
127135
boolean badness = false;
128-
for (String line : listResult) {
136+
String line;
137+
while ((line = reader.readLine()) != null) {
129138
String[] m = PApplet.match(line, "\\s+Name\\:\\s+(\\S+)");
130139
if (m != null) {
131140
if (!badness) {
@@ -149,9 +158,14 @@ static protected void list(final AndroidSDK sdk) throws IOException {
149158
}
150159
} else {
151160
System.err.println("Unhappy inside exists()");
152-
System.err.println(listResult);
161+
String line;
162+
while ((line = reader.readLine()) != null)
163+
System.err.println(line);
153164
}
154165
} catch (final InterruptedException ie) { }
166+
finally {
167+
process.destroy();
168+
}
155169
}
156170

157171

@@ -188,18 +202,24 @@ protected boolean badness() {
188202

189203
protected void initTargets(final AndroidSDK sdk) throws IOException {
190204
preferredAbi.clear();
191-
final String[] list_abi = {
192-
sdk.getAndroidToolPath(),
193-
"list", "targets"
194-
};
205+
ProcessBuilder pb = new ProcessBuilder(sdk.getAvdManagerPath(), "list", "target");
206+
207+
Map<String, String> env = pb.environment();
208+
env.clear();
209+
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
210+
pb.redirectErrorStream(true);
211+
212+
process = pb.start();
213+
InputStream stdout = process.getInputStream();
214+
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
195215

196-
ProcessHelper p = new ProcessHelper(list_abi);
197216
try {
198-
final ProcessResult abiListResult = p.execute();
217+
process.waitFor();
199218

200219
String api = null;
201220
String[] abis = null;
202-
for (String line : abiListResult) {
221+
String line;
222+
while ((line = reader.readLine()) != null) {
203223
line = line.trim();
204224
if (line.equals("")) continue;
205225

@@ -236,6 +256,8 @@ protected void initTargets(final AndroidSDK sdk) throws IOException {
236256
}
237257
}
238258
} catch (InterruptedException e) {
259+
} finally {
260+
process.destroy();
239261
}
240262
}
241263

@@ -248,43 +270,67 @@ protected boolean noTargets(final AndroidSDK sdk) throws IOException {
248270

249271

250272
protected boolean create(final AndroidSDK sdk) throws IOException {
251-
initTargets(sdk);
273+
//initTargets(sdk);
252274

253-
final String[] params = {
254-
sdk.getAndroidToolPath(),
275+
ProcessBuilder pb = new ProcessBuilder(
276+
sdk.getAvdManagerPath(),
255277
"create", "avd",
256278
"-n", name,
257-
"-t", target,
258-
"-c", DEFAULT_SDCARD_SIZE,
259-
"-s", skin,
260-
"--abi", preferredAbi.get(AndroidBuild.TARGET_SDK)
261-
};
262-
263-
// sdk/tools/android create avd -n "Wear-Processing-0254" -t android-23 -c 64M -s AndroidWearSquare --abi android-wear/x86
279+
"-k", sdkId,
280+
"-c", DEFAULT_SDCARD_SIZE
281+
);
264282

283+
// avdmanager create avd -n "Wear-Processing-0254" -k "system-images;android-25;google_apis;x86" -c 64M
284+
265285
// Set the list to null so that exists() will check again
266286
avdList = null;
267-
268-
ProcessHelper p = new ProcessHelper(params);
287+
288+
Map<String, String> env = pb.environment();
289+
env.clear();
290+
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
291+
pb.redirectErrorStream(true);
292+
269293
try {
294+
process = pb.start();
295+
296+
InputStream stdout = process.getInputStream();
297+
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
298+
270299
// Passes 'no' to "Do you wish to create a custom hardware profile [no]"
300+
OutputStream os = process.getOutputStream();
301+
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
302+
pw.println("no");
303+
pw.flush();
304+
pw.close();
305+
os.flush();
306+
os.close();
271307

272-
final ProcessResult createAvdResult = p.execute("no");
273-
if (createAvdResult.succeeded()) {
308+
process.waitFor();
309+
310+
if (process.exitValue() == 0) {
274311
return true;
275312
}
276-
if (createAvdResult.toString().contains("Target id is not valid")) {
313+
314+
String line;
315+
StringBuilder output = new StringBuilder();
316+
while((line = reader.readLine()) != null) {
317+
output.append(line);
318+
}
319+
if (output.toString().contains("Package path is not valid")) {
277320
// They didn't install the Google APIs
278-
AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
321+
//AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
322+
invalidPackage = true;
279323
} else {
280324
// Just generally not working
281325
AndroidUtil.showMessage(AVD_CREATE_TITLE,
282326
String.format(AVD_CREATE_MESSAGE, AndroidBuild.TARGET_SDK));
283-
System.out.println(createAvdResult);
284327
}
328+
System.out.println(output.toString());
285329
//System.err.println(createAvdResult);
286330
} catch (final InterruptedException ie) {
287331
ie.printStackTrace();
332+
} finally {
333+
process.destroy();
288334
}
289335

290336
return false;
@@ -309,15 +355,20 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
309355
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
310356
return false;
311357
}
312-
if (wearAVD.noTargets(sdk)) {
313-
boolean res = AndroidSDK.locateSysImage(window, mode, true);
314-
if (!res) {
315-
return false;
316-
}
317-
}
318358
if (wearAVD.create(sdk)) {
319359
return true;
320-
}
360+
}
361+
if (invalidPackage) {
362+
boolean res = AndroidSDK.locateSysImage(window, mode, true);
363+
if (!res) {
364+
return false;
365+
} else {
366+
// Try again
367+
if (wearAVD.create(sdk)) {
368+
return true;
369+
}
370+
}
371+
}
321372
} else {
322373
if (mobileAVD.exists(sdk)) {
323374
return true;
@@ -326,14 +377,19 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
326377
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
327378
return false;
328379
}
329-
if (mobileAVD.noTargets(sdk)) {
380+
if (mobileAVD.create(sdk)) {
381+
return true;
382+
}
383+
if (invalidPackage) {
330384
boolean res = AndroidSDK.locateSysImage(window, mode, false);
331385
if (!res) {
332-
return false;
386+
return false;
387+
} else {
388+
// Try again
389+
if (mobileAVD.create(sdk)) {
390+
return true;
391+
}
333392
}
334-
}
335-
if (mobileAVD.create(sdk)) {
336-
return true;
337393
}
338394
}
339395
} catch (final Exception e) {

src/processing/mode/android/AndroidMode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public void handleRunEmulator(Sketch sketch, AndroidEditor editor,
298298
}
299299

300300
int comp = build.getAppComponent();
301-
Future<Device> emu = Devices.getInstance().getEmulator(build.isWear(), build.usesOpenGL());
301+
Future<Device> emu = Devices.getInstance().getEmulator(sdk.getToolsFolder(), build.isWear(), build.usesOpenGL());
302302
runner = new AndroidRunner(build, listener);
303303
runner.launch(emu, comp, true);
304304
}

src/processing/mode/android/AndroidSDK.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AndroidSDK {
6262
private final File androidJar;
6363
private final File platformTools;
6464
private final File buildTools;
65-
private final File androidTool;
65+
private final File avdManager;
6666
private final File wearablePath;
6767
private final File supportLibPath;
6868

@@ -173,7 +173,7 @@ public AndroidSDK(File folder) throws BadSDKException, IOException {
173173
throw new BadSDKException("There is no support library folder in " + folder);
174174
}
175175

176-
androidTool = findAndroidTool(tools);
176+
avdManager = findAvdManager(new File(tools, "bin"));
177177

178178
String path = Platform.getenv("PATH");
179179

@@ -252,8 +252,8 @@ public File getToolsFolder() {
252252
}
253253

254254

255-
public String getAndroidToolPath() {
256-
return androidTool.getAbsolutePath();
255+
public String getAvdManagerPath() {
256+
return avdManager.getAbsolutePath();
257257
}
258258

259259

@@ -296,17 +296,14 @@ public File getSupportLibrary() {
296296
* for the SDK installation. Also figures out the name of android/android.bat
297297
* so that it can be called explicitly.
298298
*/
299-
private static File findAndroidTool(final File tools) throws BadSDKException {
300-
if (new File(tools, "android.exe").exists()) {
301-
return new File(tools, "android.exe");
299+
private static File findAvdManager(final File tools) throws BadSDKException {
300+
if (new File(tools, "avdmanager.bat").exists()) {
301+
return new File(tools, "avdmanager.bat");
302302
}
303-
if (new File(tools, "android.bat").exists()) {
304-
return new File(tools, "android.bat");
303+
if (new File(tools, "avdmanager").exists()) {
304+
return new File(tools, "avdmanager");
305305
}
306-
if (new File(tools, "android").exists()) {
307-
return new File(tools, "android");
308-
}
309-
throw new BadSDKException("Cannot find the android tool in " + tools);
306+
throw new BadSDKException("Cannot find avdmanager in " + tools);
310307
}
311308

312309

src/processing/mode/android/Commander.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void execute() {
239239
if (task == RUN) {
240240
AndroidRunner runner = new AndroidRunner(build, this);
241241
runner.launch(runOnEmu ?
242-
Devices.getInstance().getEmulator(build.isWear(), build.usesOpenGL()) :
242+
Devices.getInstance().getEmulator(androidMode.getSDK().getToolsFolder(), build.isWear(), build.usesOpenGL()) :
243243
Devices.getInstance().getHardware(), build.getAppComponent(), runOnEmu);
244244
}
245245

src/processing/mode/android/Devices.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import processing.app.exec.ProcessResult;
2525
import processing.mode.android.EmulatorController.State;
2626

27+
import java.io.File;
2728
import java.io.IOException;
2829
import java.util.ArrayList;
2930
import java.util.Collections;
@@ -111,10 +112,10 @@ public void run() {
111112
}
112113

113114

114-
public Future<Device> getEmulator(final boolean wear, final boolean gpu) {
115+
public Future<Device> getEmulator(final File sdkToolsPath, final boolean wear, final boolean gpu) {
115116
final Callable<Device> androidFinder = new Callable<Device>() {
116117
public Device call() throws Exception {
117-
return blockingGetEmulator(wear, gpu);
118+
return blockingGetEmulator(sdkToolsPath, wear, gpu);
118119
}
119120
};
120121
final FutureTask<Device> task = new FutureTask<Device>(androidFinder);
@@ -123,7 +124,7 @@ public Device call() throws Exception {
123124
}
124125

125126

126-
private final Device blockingGetEmulator(final boolean wear, final boolean gpu) {
127+
private final Device blockingGetEmulator(File sdkToolsPath, final boolean wear, final boolean gpu) {
127128
// System.out.println("going looking for emulator");
128129
String port = AVD.getPort(wear);
129130
Device emu = find(true, port);
@@ -138,7 +139,7 @@ private final Device blockingGetEmulator(final boolean wear, final boolean gpu)
138139
if (emuController.getState() == State.NOT_RUNNING) {
139140
try {
140141
// System.out.println("not running, gonna launch");
141-
emuController.launch(wear, gpu); // this blocks until emulator boots
142+
emuController.launch(sdkToolsPath, wear, gpu); // this blocks until emulator boots
142143
// System.out.println("not just gonna, we've done the launch");
143144
} catch (final IOException e) {
144145
System.err.println("Problem while launching emulator.");

0 commit comments

Comments
 (0)