Skip to content

Commit 7430944

Browse files
committed
Merge pull request #127 from omerjerk/master
Choose the best available ABI automatically
2 parents d5aeb29 + 30c8410 commit 7430944

File tree

3 files changed

+89
-119
lines changed

3 files changed

+89
-119
lines changed

src/processing/mode/android/AVD.java

Lines changed: 87 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package processing.mode.android;
22

33
import processing.app.Base;
4-
//import processing.app.Preferences;
54
import processing.app.exec.ProcessHelper;
65
import processing.app.exec.ProcessResult;
76
import processing.core.PApplet;
@@ -17,7 +16,7 @@ public class AVD {
1716
static private final String AVD_CREATE_SECONDARY =
1817
"The default Android emulator could not be set up. Make sure<br>" +
1918
"that the Android SDK is installed properly, and that the<br>" +
20-
"Android and Google APIs are installed for level %s.<br>" +
19+
"system images are installed for level %s.<br>" +
2120
"(Between you and me, occasionally, this error is a red herring,<br>" +
2221
"and your sketch may be launching shortly.)";
2322

@@ -44,100 +43,62 @@ public class AVD {
4443

4544
/** "android-7" or "Google Inc.:Google APIs:7" */
4645
protected String target;
47-
48-
/** x86, x86_64 or armeabi **/
49-
protected String abi;
50-
51-
protected VirtualDevice virtualDevice;
52-
53-
public static final String PREF_KEY_ABI = "android.sdk.abi";
54-
public static final String[] ABI = {"armeabi", "x86", "x86_64"};
5546

5647
/** Default virtual device used by Processing. */
57-
static public AVD defaultAVD;
48+
static public final AVD defaultAVD =
49+
new AVD("Processing-0" + Base.getRevision(),
50+
"android-" + AndroidBuild.sdkVersion);
5851
// "Google Inc.:Google APIs:" + AndroidBuild.sdkVersion);
5952

60-
static ArrayList<VirtualDevice> avdList;
61-
static ArrayList<VirtualDevice> badList;
62-
63-
64-
private static class VirtualDevice {
65-
public String name;
66-
public String target;
67-
public String abi;
68-
public VirtualDevice(String name, String target, String abi) {
69-
this.name = name;
70-
this.target = target;
71-
this.abi = abi;
72-
}
53+
static ArrayList<String> avdList;
54+
static ArrayList<String> badList;
55+
// static ArrayList<String> skinList;
7356

74-
@Override
75-
public boolean equals(Object o) {
76-
VirtualDevice device = (VirtualDevice) o;
77-
if (device.name.equals(name) && device.target.equals(target)
78-
&& device.abi.equals(abi)) {
79-
return true;
80-
}
81-
return false;
82-
}
83-
}
84-
57+
private String[] preferredAbi = new String[50];
58+
private static ArrayList<String> abiList = new ArrayList<>();
8559

86-
public AVD(String name, String target, String abi) {
60+
public AVD(final String name, final String target) {
8761
this.name = name;
8862
this.target = target;
89-
this.abi = abi;
90-
virtualDevice = new VirtualDevice(name, target, abi);
63+
}
64+
65+
private void initializeAbiList() {
66+
if (abiList.size() == 0) {
67+
abiList.add("armeabi");
68+
abiList.add("x86");
69+
abiList.add("x86_64");
70+
}
9171
}
9272

9373

9474
static protected void list(final AndroidSDK sdk) throws IOException {
9575
try {
96-
avdList = new ArrayList<VirtualDevice>();
97-
badList = new ArrayList<VirtualDevice>();
76+
avdList = new ArrayList<String>();
77+
badList = new ArrayList<String>();
9878
ProcessResult listResult =
9979
new ProcessHelper(sdk.getAndroidToolPath(), "list", "avds").execute();
10080
if (listResult.succeeded()) {
10181
boolean badness = false;
102-
String mTarget = null;
103-
String mAbi = null;
104-
String mName = null;
10582
for (String line : listResult) {
10683
String[] m = PApplet.match(line, "\\s+Name\\:\\s+(\\S+)");
10784
if (m != null) {
108-
mName = m[1];
109-
continue;
110-
}
111-
112-
m = PApplet.match(line, "API\\slevel\\s([0-9]+)");
113-
if (m != null) {
114-
mTarget = m[1];
115-
continue;
116-
}
117-
118-
m = PApplet.match(line, "\\s+Tag\\/ABI\\:\\s\\S+\\/(\\S+)");
119-
if (m != null) {
120-
mAbi = m[1];
121-
}
122-
123-
if (mName != null && mTarget != null && mAbi != null) {
124-
VirtualDevice mVirtualDevice = new VirtualDevice(mName, mTarget, mAbi);
125-
mTarget = null;
126-
mAbi = null;
12785
if (!badness) {
128-
avdList.add(mVirtualDevice);
86+
// System.out.println("good: " + m[1]);
87+
avdList.add(m[1]);
12988
} else {
130-
badList.add(mVirtualDevice);
89+
// System.out.println("bad: " + m[1]);
90+
badList.add(m[1]);
13191
}
92+
// } else {
93+
// System.out.println("nope: " + line);
13294
}
133-
13495
// "The following Android Virtual Devices could not be loaded:"
13596
if (line.contains("could not be loaded:")) {
13697
// System.out.println("starting the bad list");
13798
// System.err.println("Could not list AVDs:");
13899
// System.err.println(listResult);
139100
badness = true;
140-
break;
101+
// break;
141102
}
142103
}
143104
} else {
@@ -152,9 +113,15 @@ protected boolean exists(final AndroidSDK sdk) throws IOException {
152113
if (avdList == null) {
153114
list(sdk);
154115
}
155-
virtualDevice.target = AndroidBuild.sdkVersion;
156-
virtualDevice.abi = abi;
157-
return avdList.contains(virtualDevice);
116+
for (String avd : avdList) {
117+
if (Base.DEBUG) {
118+
System.out.println("AVD.exists() checking for " + name + " against " + avd);
119+
}
120+
if (avd.equals(name)) {
121+
return true;
122+
}
123+
}
124+
return false;
158125
}
159126

160127

@@ -164,24 +131,69 @@ protected boolean exists(final AndroidSDK sdk) throws IOException {
164131
* (Prestigious may also not be the right word.)
165132
*/
166133
protected boolean badness() {
167-
return badList.contains(virtualDevice);
134+
for (String avd : badList) {
135+
if (avd.equals(name)) {
136+
return true;
137+
}
138+
}
139+
return false;
168140
}
169141

170142

171143
protected boolean create(final AndroidSDK sdk) throws IOException {
144+
145+
final String[] list_abi = {
146+
sdk.getAndroidToolPath(),
147+
"list", "targets"
148+
};
149+
150+
ProcessHelper p = new ProcessHelper(list_abi);
151+
try {
152+
final ProcessResult abiListResult = p.execute();
153+
String api = null;
154+
String abi = null;
155+
for (String line : abiListResult) {
156+
String[] m = PApplet.match(line, "API\\slevel:\\s(\\S+)");
157+
if (m != null) {
158+
api = m[1];
159+
}
160+
161+
m = PApplet.match(line, "Tag\\/ABIs\\s:\\sdefault\\/(\\S+)");
162+
if (m != null) {
163+
abi = m[1];
164+
165+
if (api != null && abi != null) {
166+
int index = Integer.parseInt(api);
167+
if (preferredAbi[index] == null) {
168+
preferredAbi[index] = abi;
169+
} else if (abiList.indexOf(preferredAbi[index]) < abiList.indexOf(abi)) {
170+
preferredAbi[index] = abi;
171+
}
172+
api = null;
173+
abi = null;
174+
}
175+
}
176+
}
177+
} catch (InterruptedException e) {}
178+
179+
if (preferredAbi[Integer.parseInt(AndroidBuild.sdkVersion)] == null) {
180+
return false;
181+
}
182+
172183
final String[] params = {
173184
sdk.getAndroidToolPath(),
174185
"create", "avd",
175186
"-n", name,
176187
"-t", target,
177188
"-c", DEFAULT_SDCARD_SIZE,
178189
"-s", DEFAULT_SKIN,
179-
"--abi", abi
190+
"--abi", preferredAbi[Integer.parseInt(AndroidBuild.sdkVersion)]
180191
};
181192

182193
// Set the list to null so that exists() will check again
183194
avdList = null;
184-
final ProcessHelper p = new ProcessHelper(params);
195+
196+
p = new ProcessHelper(params);
185197
try {
186198
// Passes 'no' to "Do you wish to create a custom hardware profile [no]"
187199
// System.out.println("CREATE AVD STARTING");
@@ -197,8 +209,7 @@ protected boolean create(final AndroidSDK sdk) throws IOException {
197209
} else {
198210
// Just generally not working
199211
// Base.showWarning("Android Error", AVD_CREATE_ERROR, null);
200-
Base.showWarningTiered("Android Error", AVD_CREATE_PRIMARY,
201-
String.format(AVD_CREATE_SECONDARY, AndroidBuild.sdkVersion), null);
212+
Base.showWarningTiered("Android Error", AVD_CREATE_PRIMARY, AVD_CREATE_SECONDARY, null);
202213
System.out.println(createAvdResult);
203214
// throw new IOException("Error creating the AVD");
204215
}
@@ -209,11 +220,8 @@ protected boolean create(final AndroidSDK sdk) throws IOException {
209220
}
210221

211222

212-
static public boolean ensureProperAVD(final AndroidSDK sdk, final String abi) {
223+
static public boolean ensureProperAVD(final AndroidSDK sdk) {
213224
try {
214-
defaultAVD = new AVD("Processing-0" + Base.getRevision() + "-" + AndroidBuild.sdkVersion +
215-
"-" + abi,
216-
"android-" + AndroidBuild.sdkVersion, abi);
217225
if (defaultAVD.exists(sdk)) {
218226
// System.out.println("the avd exists");
219227
return true;
@@ -229,12 +237,10 @@ static public boolean ensureProperAVD(final AndroidSDK sdk, final String abi) {
229237
return true;
230238
}
231239
} catch (final Exception e) {
232-
e.printStackTrace();
233240
// Base.showWarning("Android Error", AVD_CREATE_ERROR, e);
234241
Base.showWarningTiered("Android Error", AVD_CREATE_PRIMARY,
235-
String.format(AVD_CREATE_SECONDARY, AndroidBuild.sdkVersion), null);
242+
String.format(AVD_CREATE_SECONDARY, AndroidBuild.sdkVersion), null);
236243
}
237-
System.out.println("at bottom of ensure proper");
238244
return false;
239245
}
240246
}

src/processing/mode/android/AndroidEditor.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import javax.swing.*;
3535
import javax.swing.event.ChangeEvent;
3636
import javax.swing.event.ChangeListener;
37-
3837
import java.awt.event.ActionEvent;
3938
import java.awt.event.ActionListener;
4039
import java.io.File;
@@ -248,38 +247,7 @@ public void run() {
248247
}.start();
249248

250249
menu.add(sdkMenu);
251-
menu.addSeparator();
252-
253-
final JMenu abiMenu = new JMenu("Select CPU/ABI");
254-
boolean abiSelected = false;
255-
for (int i = 0; i < AVD.ABI.length; ++i) {
256-
JMenuItem menuItem = new JCheckBoxMenuItem(AVD.ABI[i]);
257-
abiMenu.add(menuItem);
258-
if (AVD.ABI[i].equals(Preferences.get(AVD.PREF_KEY_ABI))) {
259-
menuItem.setSelected(true);
260-
abiSelected = true;
261-
}
262-
}
263-
if (!abiSelected) {
264-
abiMenu.getItem(2).setSelected(true); //Select x86_64 as the default
265-
Preferences.set(AVD.PREF_KEY_ABI, AVD.ABI[2]);
266-
}
267-
268-
for (int i = 0; i < abiMenu.getItemCount(); ++i) {
269-
final JMenuItem abiItem = abiMenu.getItem(i);
270-
abiItem.addActionListener(new ActionListener() {
271-
@Override
272-
public void actionPerformed(ActionEvent e) {
273-
for (int j = 0; j < abiMenu.getItemCount(); ++j) {
274-
abiMenu.getItem(j).setSelected(false);
275-
}
276-
abiItem.setSelected(true);
277-
Preferences.set(AVD.PREF_KEY_ABI, abiItem.getText());
278-
}
279-
});
280-
}
281-
282-
menu.add(abiMenu);
250+
283251
menu.addSeparator();
284252

285253
item = new JMenuItem("Android SDK Manager");

src/processing/mode/android/AndroidMode.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ public void handleRunEmulator(Sketch sketch, RunnerListener listener) throws Ske
198198
listener.statusNotice("Building Android project...");
199199
build.build("debug");
200200

201-
String abi = Preferences.get(AVD.PREF_KEY_ABI);
202-
if (abi.equals("")) {
203-
abi = "x86_64";
204-
}
205-
boolean avd = AVD.ensureProperAVD(sdk, abi);
201+
boolean avd = AVD.ensureProperAVD(sdk);
206202
if (!avd) {
207203
SketchException se =
208204
new SketchException("Could not create a virtual device for the emulator.");

0 commit comments

Comments
 (0)