47
47
import java .util .ArrayList ;
48
48
import java .util .Date ;
49
49
50
+ import java .io .PrintWriter ;
51
+
50
52
/**
51
53
* Class holding all needed references (path, tools, etc) to the SDK used by
52
54
* the mode.
@@ -67,7 +69,8 @@ class AndroidSDK {
67
69
private final File cmdlineTools ;
68
70
private final File avdManager ;
69
71
private final File sdkManager ;
70
- private final File emulator ;
72
+
73
+ private File emulator ;
71
74
72
75
private static final String SDK_DOWNLOAD_URL =
73
76
"https://developer.android.com/studio/index.html#downloads" ;
@@ -147,6 +150,25 @@ public AndroidSDK(File folder) throws BadSDKException, IOException {
147
150
avdManager = findCliTool (new File (cmdlineTools , "bin" ), "avdmanager" );
148
151
sdkManager = findCliTool (new File (cmdlineTools , "bin" ), "sdkmanager" );
149
152
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 {
150
172
File emuFolder = new File (folder , "emulator" );
151
173
if (emuFolder .exists ()) {
152
174
// First try the new location of the emulator inside its own folder
@@ -158,28 +180,49 @@ public AndroidSDK(File folder) throws BadSDKException, IOException {
158
180
emulator = findCliTool (cmdlineTools , "emulator" );
159
181
} else {
160
182
emulator = null ;
161
- if (SDKDownloader .DOWNLOAD_EMU ) {
183
+ if (SDKDownloader .DOWNLOAD_EMU_WITH_SDK ) {
162
184
// Only throw an exception if the downloader was supposed to download the emulator
163
185
throw new BadSDKException (AndroidMode .getTextString ("android_sdk.error.missing_emulator" ,
164
186
AndroidBuild .TARGET_SDK , highestPlatform .getAbsolutePath ()));
165
187
}
166
188
}
167
189
}
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
+ }
174
204
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
+ }
181
224
182
- checkDebugCertificate () ;
225
+ return false ;
183
226
}
184
227
185
228
0 commit comments