4747import java .util .ArrayList ;
4848import 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
0 commit comments