Skip to content

Commit ff1e185

Browse files
committed
accept SDK licenses with sdkmanager
1 parent 9c6326f commit ff1e185

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

src/processing/mode/android/AndroidSDK.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.io.File;
3939
import java.io.FileReader;
4040
import java.io.IOException;
41+
import java.io.InputStream;
42+
import java.io.OutputStream;
4143
import java.text.DateFormat;
4244
import java.text.ParseException;
4345
import java.text.SimpleDateFormat;
@@ -63,11 +65,15 @@ class AndroidSDK {
6365
private final File platformTools;
6466
private final File buildTools;
6567
private final File avdManager;
68+
private final File sdkManager;
6669
private final File wearablePath;
6770
private final File supportLibPath;
6871

6972
private static final String SDK_DOWNLOAD_URL =
7073
"https://developer.android.com/studio/index.html#downloads";
74+
75+
private static final String SDK_LICENSE_URL =
76+
"https://developer.android.com/studio/terms.html";
7177

7278
private static final String USE_ENV_SDK_TITLE = "Found an Android SDK!";
7379
private static final String USE_ENV_SDK_MESSAGE =
@@ -218,6 +224,7 @@ public AndroidSDK(File folder) throws BadSDKException, IOException {
218224
}
219225

220226
avdManager = findAvdManager(new File(tools, "bin"));
227+
sdkManager = findSdkManager(new File(tools, "bin"));
221228

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

@@ -345,6 +352,60 @@ public File getZipAlignTool() {
345352
return null;
346353
}
347354

355+
356+
public void acceptLicenses() {
357+
ArrayList<String> commands = new ArrayList<String>();
358+
commands.add(sdkManager.getAbsolutePath());
359+
commands.add("--licenses");
360+
ProcessBuilder pb = new ProcessBuilder(commands);
361+
pb.redirectErrorStream(true);
362+
try {
363+
364+
Process prs = pb.start();
365+
OutputStream writeTo = prs.getOutputStream();
366+
for (int i = 0; i < 7; i++) {
367+
Thread inThread = new Thread(new In(prs.getInputStream()));
368+
inThread.start();
369+
Thread.sleep(100);
370+
writeTo.write("y\n".getBytes());
371+
writeTo.flush();
372+
}
373+
374+
writeTo.close();
375+
376+
} catch (IOException e) {
377+
e.printStackTrace();
378+
} catch (InterruptedException e) {
379+
// TODO Auto-generated catch block
380+
e.printStackTrace();
381+
}
382+
}
383+
384+
385+
class In implements Runnable {
386+
private InputStream is;
387+
388+
public In(InputStream is) {
389+
this.is = is;
390+
}
391+
392+
@Override
393+
public void run() {
394+
byte[] b = new byte[1024];
395+
int size = 0;
396+
try {
397+
while ((size = is.read(b)) != -1) {
398+
System.err.println(new String(b));
399+
}
400+
is.close();
401+
} catch (IOException e) {
402+
// TODO Auto-generated catch block
403+
e.printStackTrace();
404+
}
405+
406+
}
407+
}
408+
348409
static public File getHAXMInstallerFolder() {
349410
String sdkPrefsPath = Preferences.get("android.sdk.path");
350411
File sdkPath = new File(sdkPrefsPath);
@@ -374,6 +435,16 @@ private static File findAvdManager(final File tools) throws BadSDKException {
374435
throw new BadSDKException("Cannot find avdmanager in " + tools);
375436
}
376437

438+
439+
private static File findSdkManager(final File tools) throws BadSDKException {
440+
if (new File(tools, "sdkmanager.bat").exists()) {
441+
return new File(tools, "sdkmanager.bat");
442+
}
443+
if (new File(tools, "sdkmanager").exists()) {
444+
return new File(tools, "sdkmanager");
445+
}
446+
throw new BadSDKException("Cannot find sdkdmanager in " + tools);
447+
}
377448

378449
/**
379450
* Check for a set android.sdk.path preference. If the pref
@@ -399,6 +470,7 @@ public static AndroidSDK load(boolean checkEnvSDK, Frame editor) throws IOExcept
399470
try {
400471
final AndroidSDK androidSDK = new AndroidSDK(new File(sdkPrefsPath));
401472
Preferences.set("android.sdk.path", sdkPrefsPath);
473+
androidSDK.acceptLicenses();
402474
return androidSDK;
403475
} catch (final BadSDKException badPref) {
404476
Preferences.unset("android.sdk.path");
@@ -433,6 +505,7 @@ public static AndroidSDK load(boolean checkEnvSDK, Frame editor) throws IOExcept
433505
// welcome message with some useful info.
434506
AndroidUtil.showMessage(SDK_EXISTS_TITLE, SDK_EXISTS_MESSAGE);
435507

508+
androidSDK.acceptLicenses();
436509
return androidSDK;
437510
} catch (final BadSDKException badEnv) {
438511
Preferences.unset("android.sdk.path");
@@ -509,7 +582,9 @@ static public AndroidSDK download(final Frame editor, final AndroidMode androidM
509582
msg += DRIVER_INSTALL_MESSAGE + driver.getAbsolutePath();
510583
}
511584
AndroidUtil.showMessage(SDK_INSTALL_TITLE, msg);
512-
585+
586+
sdk.acceptLicenses();
587+
513588
return sdk;
514589
}
515590

0 commit comments

Comments
 (0)