Skip to content

Commit c0e98ff

Browse files
authored
Merge pull request #705 from processing/download_emu_on_demand
Download emu on demand
2 parents 475224e + 6bed21e commit c0e98ff

File tree

7 files changed

+110
-22
lines changed

7 files changed

+110
-22
lines changed

debug/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
mavenCentral()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:4.1.3'
9+
classpath 'com.android.tools.build:gradle:7.2.1'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

debug/gradle.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
#Wed Dec 30 11:02:34 EST 2020
1414
android.enableJetifier=true
1515
android.useAndroidX=true
16+
17+
# Line to fix the ''module java.base does not "opens java.io" to unnamed module '' error in Android Studio:
18+
# https://stackoverflow.com/questions/67782975/how-to-fix-the-module-java-base-does-not-opens-java-io-to-unnamed-module
19+
# Probably not needed if using a version of the Android Gradle Plugin compatible with the JDK:
20+
# https://docs.gradle.org/current/userguide/compatibility.html
21+
org.gradle.jvmargs=-Xmx1536M --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED

mode/mode.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ url = http://android.processing.org
44
sentence = This mode lets you use Processing to create Android apps
55
paragraph =
66
imports=processing.mode.java.JavaMode
7-
version = 404
8-
prettyVersion = 4.5.0a5
7+
version = 405
8+
prettyVersion = 4.5.0b1
99
minRevision = 1283
1010
maxRevision = 0
1111

mode/src/processing/mode/android/AndroidMode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ public void handleRunEmulator(Sketch sketch, AndroidEditor editor,
239239

240240
listener.statusNotice(AndroidMode.getTextString("android_mode.status.building_project"));
241241
build.build("debug", "");
242+
243+
if (sdk.getEmulatorTool() == null) {
244+
System.out.println("Try to download the emulator using the SDK Manager...");
245+
sdk.downloadEmuOnDemand();
246+
}
242247

243248
boolean avd = AVD.ensureProperAVD(editor, this, sdk, build.isWear());
244249
if (!avd) {

mode/src/processing/mode/android/AndroidSDK.java

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import java.util.ArrayList;
4848
import java.util.Date;
4949

50+
import java.io.PrintWriter;
51+
5052
/**
5153
* Class holding all needed references (path, tools, etc) to the SDK used by
5254
* the mode.
@@ -67,7 +69,8 @@ class AndroidSDK {
6769
private final File cmdlineTools;
6870
private final File avdManager;
6971
private final File sdkManager;
70-
private final File emulator;
72+
73+
private File emulator;
7174

7275
private static final String SDK_DOWNLOAD_URL =
7376
"https://developer.android.com/studio/index.html#downloads";
@@ -147,6 +150,25 @@ public AndroidSDK(File folder) throws BadSDKException, IOException {
147150
avdManager = findCliTool(new File(cmdlineTools, "bin"), "avdmanager");
148151
sdkManager = findCliTool(new File(cmdlineTools, "bin"), "sdkmanager");
149152

153+
initEmu();
154+
155+
String path = Platform.getenv("PATH");
156+
157+
Platform.setenv("ANDROID_SDK", folder.getCanonicalPath());
158+
path = platformTools.getCanonicalPath() + File.pathSeparator +
159+
cmdlineTools.getCanonicalPath() + File.pathSeparator + path;
160+
161+
String javaHomeProp = System.getProperty("java.home");
162+
File javaHome = new File(javaHomeProp).getCanonicalFile();
163+
Platform.setenv("JAVA_HOME", javaHome.getCanonicalPath());
164+
165+
path = new File(javaHome, "bin").getCanonicalPath() + File.pathSeparator + path;
166+
Platform.setenv("PATH", path);
167+
168+
checkDebugCertificate();
169+
}
170+
171+
private void initEmu() throws BadSDKException, IOException {
150172
File emuFolder = new File(folder, "emulator");
151173
if (emuFolder.exists()) {
152174
// First try the new location of the emulator inside its own folder
@@ -158,28 +180,49 @@ public AndroidSDK(File folder) throws BadSDKException, IOException {
158180
emulator = findCliTool(cmdlineTools, "emulator");
159181
} else {
160182
emulator = null;
161-
if (SDKDownloader.DOWNLOAD_EMU) {
183+
if (SDKDownloader.DOWNLOAD_EMU_WITH_SDK) {
162184
// Only throw an exception if the downloader was supposed to download the emulator
163185
throw new BadSDKException(AndroidMode.getTextString("android_sdk.error.missing_emulator",
164186
AndroidBuild.TARGET_SDK, highestPlatform.getAbsolutePath()));
165187
}
166188
}
167189
}
168-
169-
String path = Platform.getenv("PATH");
170-
171-
Platform.setenv("ANDROID_SDK", folder.getCanonicalPath());
172-
path = platformTools.getCanonicalPath() + File.pathSeparator +
173-
cmdlineTools.getCanonicalPath() + File.pathSeparator + path;
190+
}
191+
public boolean downloadEmuOnDemand() {
192+
final String[] cmd = new String[] {
193+
sdkManager.getAbsolutePath(),
194+
"emulator"
195+
};
196+
197+
ProcessBuilder pb = new ProcessBuilder(cmd);
198+
Process process = null;
199+
try {
200+
process = pb.start();
201+
} catch (IOException e) {
202+
e.printStackTrace();
203+
}
174204

175-
String javaHomeProp = System.getProperty("java.home");
176-
File javaHome = new File(javaHomeProp).getCanonicalFile();
177-
Platform.setenv("JAVA_HOME", javaHome.getCanonicalPath());
178-
179-
path = new File(javaHome, "bin").getCanonicalPath() + File.pathSeparator + path;
180-
Platform.setenv("PATH", path);
205+
try {
206+
new RedirectStreamHandler(new PrintWriter(System.out, true), process.getInputStream());
207+
new RedirectStreamHandler(new PrintWriter(System.out, true), process.getErrorStream());
208+
209+
int emulatorDownloadResultCode = process.waitFor();
210+
System.out.println("Output from emulator download " + emulatorDownloadResultCode);
211+
if (emulatorDownloadResultCode == 0) {
212+
initEmu();
213+
return true;
214+
}
215+
} catch (IOException e) {
216+
e.printStackTrace();
217+
} catch (BadSDKException e) {
218+
e.printStackTrace();
219+
} catch (InterruptedException e) {
220+
e.printStackTrace();
221+
} finally {
222+
process.destroy();
223+
}
181224

182-
checkDebugCertificate();
225+
return false;
183226
}
184227

185228

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package processing.mode.android;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.PrintWriter;
8+
9+
10+
public class RedirectStreamHandler extends Thread {
11+
// Streams to redirect from and to
12+
private final InputStream input;
13+
private final PrintWriter output;
14+
15+
RedirectStreamHandler(PrintWriter output, InputStream input) {
16+
this.input = input;
17+
this.output = output;
18+
start();
19+
}
20+
21+
@Override
22+
public void run() {
23+
try {
24+
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
25+
String line;
26+
while ((line = reader.readLine()) != null) {
27+
output.println(line);
28+
}
29+
} catch (IOException ioe) {
30+
// OK to ignore...
31+
System.out.println("Level.WARNING____I/O Redirection failure: "+ ioe.toString());
32+
}
33+
}
34+
}

mode/src/processing/mode/android/SDKDownloader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class SDKDownloader extends JDialog implements PropertyChangeListener {
6666
private static final String REPOSITORY_LIST = "repository2-1.xml";
6767
private static final String ADDON_LIST = "addon2-1.xml";
6868

69-
public static final boolean DOWNLOAD_EMU = false;
69+
public static final boolean DOWNLOAD_EMU_WITH_SDK = false;
7070

7171
private JProgressBar progressBar;
7272
private JLabel downloadedTextArea;
@@ -114,7 +114,7 @@ protected Object doInBackground() throws Exception {
114114
File haxmFolder = new File(extrasFolder, "intel/HAXM");
115115
if (!haxmFolder.exists()) haxmFolder.mkdirs();
116116

117-
if (DOWNLOAD_EMU) {
117+
if (DOWNLOAD_EMU_WITH_SDK) {
118118
File emulatorFolder = new File(sdkFolder, "emulator");
119119
if (!emulatorFolder.exists()) emulatorFolder.mkdir();
120120
}
@@ -173,7 +173,7 @@ protected Object doInBackground() throws Exception {
173173
downloadAndUnpack(downloadUrls.haxmUrl, downloadedFolder, haxmFolder, true);
174174
}
175175

176-
if (DOWNLOAD_EMU) {
176+
if (DOWNLOAD_EMU_WITH_SDK) {
177177
// Emulator, unpacks directly to sdk folder
178178
File downloadedEmulator = new File(tempFolder, downloadUrls.emulatorFilename);
179179
downloadAndUnpack(downloadUrls.emulatorUrl, downloadedEmulator, sdkFolder, true);
@@ -391,7 +391,7 @@ private void getMainDownloadUrls(SDKUrlHolder urlHolder,
391391
throw new IOException(AndroidMode.getTextString("sdk_downloader.error_cannot_find_tools"));
392392
}
393393

394-
if (DOWNLOAD_EMU) {
394+
if (DOWNLOAD_EMU_WITH_SDK) {
395395
// -----------------------------------------------------------------------
396396
// Emulator
397397
expr = xpath.compile("//remotePackage[@path=\"emulator\"]");

0 commit comments

Comments
 (0)