Skip to content

Commit

Permalink
Replaced android tool with avdmanager; issues with emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
rupak0577 committed Jul 29, 2017
1 parent 9b78586 commit b02535c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 68 deletions.
154 changes: 99 additions & 55 deletions src/processing/mode/android/AVD.java
Expand Up @@ -22,12 +22,11 @@
package processing.mode.android;

import processing.app.Base;
import processing.app.exec.ProcessHelper;
import processing.app.exec.ProcessResult;
import processing.app.Platform;
import processing.core.PApplet;

import java.awt.Frame;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -68,40 +67,38 @@ public class AVD {

static final String DEFAULT_SDCARD_SIZE = "64M";

static final String DEFAULT_SKIN = "WVGA800";
static final String WEAR_SKIN = "AndroidWearSquare";

/** Name of this avd. */
protected String name;

/** "android-7" or "Google Inc.:Google APIs:7" */
protected String target;
protected String sdkId;

static ArrayList<String> avdList;
static ArrayList<String> badList;
// static ArrayList<String> skinList;

private Map<String, String> preferredAbi = new HashMap<>(30);
private List<String> abiList = new ArrayList<>();
private String skin;
private static Process process;

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

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

public AVD(final String name, final String target,
final String tag, final String skin) {
public AVD(final String name, final String sdkId,
final String tag) {
this.name = name;
this.target = target;
this.skin = skin;
initializeAbiList(tag);
this.sdkId = sdkId;
//initializeAbiList(tag);
}

private void initializeAbiList(String tag) {
Expand All @@ -121,11 +118,22 @@ static protected void list(final AndroidSDK sdk) throws IOException {
try {
avdList = new ArrayList<String>();
badList = new ArrayList<String>();
ProcessResult listResult =
new ProcessHelper(sdk.getAndroidToolPath(), "list", "avds").execute();
if (listResult.succeeded()) {
ProcessBuilder pb =
new ProcessBuilder(sdk.getAvdManagerPath(), "list", "avd");
Map<String, String> env = pb.environment();
env.clear();
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
pb.redirectErrorStream(true);

process = pb.start();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
process.waitFor();

if (process.exitValue() == 0) {
boolean badness = false;
for (String line : listResult) {
String line;
while ((line = reader.readLine()) != null) {
String[] m = PApplet.match(line, "\\s+Name\\:\\s+(\\S+)");
if (m != null) {
if (!badness) {
Expand All @@ -149,9 +157,14 @@ static protected void list(final AndroidSDK sdk) throws IOException {
}
} else {
System.err.println("Unhappy inside exists()");
System.err.println(listResult);
String line;
while ((line = reader.readLine()) != null)
System.err.println(line);
}
} catch (final InterruptedException ie) { }
finally {
process.destroy();
}
}


Expand Down Expand Up @@ -188,18 +201,24 @@ protected boolean badness() {

protected void initTargets(final AndroidSDK sdk) throws IOException {
preferredAbi.clear();
final String[] list_abi = {
sdk.getAndroidToolPath(),
"list", "targets"
};
ProcessBuilder pb = new ProcessBuilder(sdk.getAvdManagerPath(), "list", "target");

Map<String, String> env = pb.environment();
env.clear();
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
pb.redirectErrorStream(true);

process = pb.start();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));

ProcessHelper p = new ProcessHelper(list_abi);
try {
final ProcessResult abiListResult = p.execute();
process.waitFor();

String api = null;
String[] abis = null;
for (String line : abiListResult) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.equals("")) continue;

Expand Down Expand Up @@ -236,6 +255,8 @@ protected void initTargets(final AndroidSDK sdk) throws IOException {
}
}
} catch (InterruptedException e) {
} finally {
process.destroy();
}
}

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


protected boolean create(final AndroidSDK sdk) throws IOException {
initTargets(sdk);
//initTargets(sdk);

final String[] params = {
sdk.getAndroidToolPath(),
ProcessBuilder pb = new ProcessBuilder(
sdk.getAvdManagerPath(),
"create", "avd",
"-n", name,
"-t", target,
"-c", DEFAULT_SDCARD_SIZE,
"-s", skin,
"--abi", preferredAbi.get(AndroidBuild.TARGET_SDK)
};

// sdk/tools/android create avd -n "Wear-Processing-0254" -t android-23 -c 64M -s AndroidWearSquare --abi android-wear/x86
"-k", sdkId,
"-c", DEFAULT_SDCARD_SIZE
);

// avdmanager create avd -n "Wear-Processing-0254" -k "system-images;android-25;google_apis;x86" -c 64M

// Set the list to null so that exists() will check again
avdList = null;

ProcessHelper p = new ProcessHelper(params);

Map<String, String> env = pb.environment();
env.clear();
env.put("JAVA_HOME", Platform.getJavaHome().getCanonicalPath());
pb.redirectErrorStream(true);

try {
process = pb.start();

InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));

// Passes 'no' to "Do you wish to create a custom hardware profile [no]"
OutputStream os = process.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
pw.println("no");
pw.flush();
pw.close();
os.flush();
os.close();

final ProcessResult createAvdResult = p.execute("no");
if (createAvdResult.succeeded()) {
process.waitFor();

if (process.exitValue() == 0) {
return true;
}
if (createAvdResult.toString().contains("Target id is not valid")) {

String line;
StringBuilder output = new StringBuilder();
while((line = reader.readLine()) != null) {
output.append(line);
}
if (output.toString().contains("Package path is not valid")) {
// They didn't install the Google APIs
AndroidUtil.showMessage(AVD_TARGET_TITLE, AVD_TARGET_MESSAGE);
} else {
// Just generally not working
AndroidUtil.showMessage(AVD_CREATE_TITLE,
String.format(AVD_CREATE_MESSAGE, AndroidBuild.TARGET_SDK));
System.out.println(createAvdResult);
}
System.out.println(output.toString());
//System.err.println(createAvdResult);
} catch (final InterruptedException ie) {
ie.printStackTrace();
} finally {
process.destroy();
}

return false;
Expand All @@ -309,12 +353,12 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
return false;
}
if (wearAVD.noTargets(sdk)) {
boolean res = AndroidSDK.locateSysImage(window, mode, true);
if (!res) {
return false;
}
}
// if (wearAVD.noTargets(sdk)) {
// boolean res = AndroidSDK.locateSysImage(window, mode, true);
// if (!res) {
// return false;
// }
// }
if (wearAVD.create(sdk)) {
return true;
}
Expand All @@ -326,12 +370,12 @@ static public boolean ensureProperAVD(final Frame window, final AndroidMode mode
AndroidUtil.showMessage(AVD_LOAD_TITLE, AVD_LOAD_MESSAGE);
return false;
}
if (mobileAVD.noTargets(sdk)) {
boolean res = AndroidSDK.locateSysImage(window, mode, false);
if (!res) {
return false;
}
}
// if (mobileAVD.noTargets(sdk)) {
// boolean res = AndroidSDK.locateSysImage(window, mode, false);
// if (!res) {
// return false;
// }
// }
if (mobileAVD.create(sdk)) {
return true;
}
Expand Down
23 changes: 10 additions & 13 deletions src/processing/mode/android/AndroidSDK.java
Expand Up @@ -62,7 +62,7 @@ class AndroidSDK {
private final File androidJar;
private final File platformTools;
private final File buildTools;
private final File androidTool;
private final File avdManager;
private final File wearablePath;
private final File supportLibPath;

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

androidTool = findAndroidTool(tools);
avdManager = findAvdManager(new File(tools, "bin"));

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

Expand Down Expand Up @@ -252,8 +252,8 @@ public File getToolsFolder() {
}


public String getAndroidToolPath() {
return androidTool.getAbsolutePath();
public String getAvdManagerPath() {
return avdManager.getAbsolutePath();
}


Expand Down Expand Up @@ -296,17 +296,14 @@ public File getSupportLibrary() {
* for the SDK installation. Also figures out the name of android/android.bat
* so that it can be called explicitly.
*/
private static File findAndroidTool(final File tools) throws BadSDKException {
if (new File(tools, "android.exe").exists()) {
return new File(tools, "android.exe");
private static File findAvdManager(final File tools) throws BadSDKException {
if (new File(tools, "avdmanager.bat").exists()) {
return new File(tools, "avdmanager.bat");
}
if (new File(tools, "android.bat").exists()) {
return new File(tools, "android.bat");
if (new File(tools, "avdmanager").exists()) {
return new File(tools, "avdmanager");
}
if (new File(tools, "android").exists()) {
return new File(tools, "android");
}
throw new BadSDKException("Cannot find the android tool in " + tools);
throw new BadSDKException("Cannot find avdmanager in " + tools);
}


Expand Down

0 comments on commit b02535c

Please sign in to comment.