Skip to content

Commit b02535c

Browse files
committed
Replaced android tool with avdmanager; issues with emulator
1 parent 9b78586 commit b02535c

File tree

2 files changed

+109
-68
lines changed

2 files changed

+109
-68
lines changed

src/processing/mode/android/AVD.java

Lines changed: 99 additions & 55 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,38 @@ 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

8076
static ArrayList<String> avdList;
8177
static ArrayList<String> badList;
8278
// static ArrayList<String> skinList;
8379

8480
private Map<String, String> preferredAbi = new HashMap<>(30);
8581
private List<String> abiList = new ArrayList<>();
86-
private String skin;
82+
private static Process process;
8783

8884
/** Default virtual device used by Processing. */
8985
static public final AVD mobileAVD =
9086
new AVD("Processing-0" + Base.getRevision(),
91-
AndroidBuild.TARGET_PLATFORM, SysImageDownloader.SYSTEM_IMAGE_TAG, DEFAULT_SKIN);
87+
"system-images;" + AndroidBuild.TARGET_PLATFORM + ";" +
88+
SysImageDownloader.SYSTEM_IMAGE_TAG + ";x86", SysImageDownloader.SYSTEM_IMAGE_TAG);
9289
// "Google Inc.:Google APIs:" + AndroidBuild.sdkVersion);
9390

9491
/** Default virtual wear device used by Processing. */
9592
static public final AVD wearAVD =
9693
new AVD("Processing-Wear-0" + Base.getRevision(),
97-
AndroidBuild.TARGET_PLATFORM, SysImageDownloader.SYSTEM_IMAGE_WEAR_TAG, WEAR_SKIN);
94+
"system-images;" + AndroidBuild.TARGET_PLATFORM + ";" +
95+
SysImageDownloader.SYSTEM_IMAGE_WEAR_TAG + ";x86", SysImageDownloader.SYSTEM_IMAGE_WEAR_TAG);
9896

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

107104
private void initializeAbiList(String tag) {
@@ -121,11 +118,22 @@ static protected void list(final AndroidSDK sdk) throws IOException {
121118
try {
122119
avdList = new ArrayList<String>();
123120
badList = new ArrayList<String>();
124-
ProcessResult listResult =
125-
new ProcessHelper(sdk.getAndroidToolPath(), "list", "avds").execute();
126-
if (listResult.succeeded()) {
121+
ProcessBuilder pb =
122+
new ProcessBuilder(sdk.getAvdManagerPath(), "list", "avd");
123+
Map<String, String> env = pb.environment();
124+
env.clear();
125+
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
126+
pb.redirectErrorStream(true);
127+
128+
process = pb.start();
129+
InputStream stdout = process.getInputStream();
130+
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
131+
process.waitFor();
132+
133+
if (process.exitValue() == 0) {
127134
boolean badness = false;
128-
for (String line : listResult) {
135+
String line;
136+
while ((line = reader.readLine()) != null) {
129137
String[] m = PApplet.match(line, "\\s+Name\\:\\s+(\\S+)");
130138
if (m != null) {
131139
if (!badness) {
@@ -149,9 +157,14 @@ static protected void list(final AndroidSDK sdk) throws IOException {
149157
}
150158
} else {
151159
System.err.println("Unhappy inside exists()");
152-
System.err.println(listResult);
160+
String line;
161+
while ((line = reader.readLine()) != null)
162+
System.err.println(line);
153163
}
154164
} catch (final InterruptedException ie) { }
165+
finally {
166+
process.destroy();
167+
}
155168
}
156169

157170

@@ -188,18 +201,24 @@ protected boolean badness() {
188201

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

196-
ProcessHelper p = new ProcessHelper(list_abi);
197215
try {
198-
final ProcessResult abiListResult = p.execute();
216+
process.waitFor();
199217

200218
String api = null;
201219
String[] abis = null;
202-
for (String line : abiListResult) {
220+
String line;
221+
while ((line = reader.readLine()) != null) {
203222
line = line.trim();
204223
if (line.equals("")) continue;
205224

@@ -236,6 +255,8 @@ protected void initTargets(final AndroidSDK sdk) throws IOException {
236255
}
237256
}
238257
} catch (InterruptedException e) {
258+
} finally {
259+
process.destroy();
239260
}
240261
}
241262

@@ -248,43 +269,66 @@ protected boolean noTargets(final AndroidSDK sdk) throws IOException {
248269

249270

250271
protected boolean create(final AndroidSDK sdk) throws IOException {
251-
initTargets(sdk);
272+
//initTargets(sdk);
252273

253-
final String[] params = {
254-
sdk.getAndroidToolPath(),
274+
ProcessBuilder pb = new ProcessBuilder(
275+
sdk.getAvdManagerPath(),
255276
"create", "avd",
256277
"-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
278+
"-k", sdkId,
279+
"-c", DEFAULT_SDCARD_SIZE
280+
);
264281

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

272-
final ProcessResult createAvdResult = p.execute("no");
273-
if (createAvdResult.succeeded()) {
307+
process.waitFor();
308+
309+
if (process.exitValue() == 0) {
274310
return true;
275311
}
276-
if (createAvdResult.toString().contains("Target id is not valid")) {
312+
313+
String line;
314+
StringBuilder output = new StringBuilder();
315+
while((line = reader.readLine()) != null) {
316+
output.append(line);
317+
}
318+
if (output.toString().contains("Package path is not valid")) {
277319
// They didn't install the Google APIs
278320
AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
279321
} else {
280322
// Just generally not working
281323
AndroidUtil.showMessage(AVD_CREATE_TITLE,
282324
String.format(AVD_CREATE_MESSAGE, AndroidBuild.TARGET_SDK));
283-
System.out.println(createAvdResult);
284325
}
326+
System.out.println(output.toString());
285327
//System.err.println(createAvdResult);
286328
} catch (final InterruptedException ie) {
287329
ie.printStackTrace();
330+
} finally {
331+
process.destroy();
288332
}
289333

290334
return false;
@@ -309,12 +353,12 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
309353
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
310354
return false;
311355
}
312-
if (wearAVD.noTargets(sdk)) {
313-
boolean res = AndroidSDK.locateSysImage(window, mode, true);
314-
if (!res) {
315-
return false;
316-
}
317-
}
356+
// if (wearAVD.noTargets(sdk)) {
357+
// boolean res = AndroidSDK.locateSysImage(window, mode, true);
358+
// if (!res) {
359+
// return false;
360+
// }
361+
// }
318362
if (wearAVD.create(sdk)) {
319363
return true;
320364
}
@@ -326,12 +370,12 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
326370
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
327371
return false;
328372
}
329-
if (mobileAVD.noTargets(sdk)) {
330-
boolean res = AndroidSDK.locateSysImage(window, mode, false);
331-
if (!res) {
332-
return false;
333-
}
334-
}
373+
// if (mobileAVD.noTargets(sdk)) {
374+
// boolean res = AndroidSDK.locateSysImage(window, mode, false);
375+
// if (!res) {
376+
// return false;
377+
// }
378+
// }
335379
if (mobileAVD.create(sdk)) {
336380
return true;
337381
}

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

0 commit comments

Comments
 (0)