@@ -83,14 +83,14 @@ class AndroidSDK {
83
83
"Make sure to install the SDK platform for API " + AndroidBuild .TARGET_SDK + "." ;
84
84
85
85
private static final String INVALID_SDK_TITLE =
86
- "Cannot find the required SDK platform version ..." ;
86
+ "The Android SDK is not valid ..." ;
87
87
88
88
private static final String INVALID_SDK_MESSAGE =
89
- "The Android SDK appears to be installed, " +
90
- "however the SDK platform for API " + AndroidBuild . TARGET_SDK +
91
- " was not found. If it is available in a different location, " +
89
+ "The found Android SDK cannot be used by Processing. " +
90
+ "It could be missing files, or does not include the required platform for " +
91
+ "API " + AndroidBuild . TARGET_SDK + ". If a valid SDK is available in a different location, " +
92
92
"click \" Locate SDK path\" to select the " +
93
- "location of the alternative SDK, or \" Download SDK\" to let " +
93
+ "location of the valid SDK, or \" Download SDK\" to let " +
94
94
"Processing download the SDK automatically.<br><br>" +
95
95
"If you want to download the SDK manually, you can get " +
96
96
"the command line tools from <a href=\" " + SDK_DOWNLOAD_URL + "\" >here</a>. " +
@@ -137,7 +137,7 @@ class AndroidSDK {
137
137
private static final int NO_ERROR = 0 ;
138
138
private static final int MISSING_SDK = 1 ;
139
139
private static final int INVALID_SDK = 2 ;
140
- private static int SDK_LOAD_ERROR = NO_ERROR ;
140
+ private static int loadError = NO_ERROR ;
141
141
142
142
public AndroidSDK (File folder ) throws BadSDKException , IOException {
143
143
this .folder = folder ;
@@ -336,12 +336,12 @@ private static File findAvdManager(final File tools) throws BadSDKException {
336
336
337
337
338
338
/**
339
+ * Check for a set android.sdk.path preference. If the pref
340
+ * is set, and refers to a legitimate Android SDK, then use that.
341
+ *
339
342
* Check for the ANDROID_SDK environment variable. If the variable is set,
340
343
* and refers to a legitimate Android SDK, then use that and save the pref.
341
344
*
342
- * Check for a previously set android.sdk.path preference. If the pref
343
- * is set, and refers to a legitimate Android SDK, then use that.
344
- *
345
345
* Prompt the user to select an Android SDK. If the user selects a
346
346
* legitimate Android SDK, then use that, and save the preference.
347
347
*
@@ -350,9 +350,29 @@ private static File findAvdManager(final File tools) throws BadSDKException {
350
350
* @throws IOException
351
351
*/
352
352
public static AndroidSDK load () throws IOException {
353
- SDK_LOAD_ERROR = NO_ERROR ;
353
+ loadError = NO_ERROR ;
354
+
355
+ // Give priority to preferences. Rationale: when user runs the mode for the first time
356
+ // and there is a valid SDK in the environment, it will be stored in the
357
+ // preferences. So in this case, priority is given to an existing (and valid) SDK.
358
+ // From that point on, if the preference value is changed then it means that the user
359
+ // wants to use a custom SDK, so the mode should not revert back to the global SDK.
360
+ // Also, using the global SDK risks breaking the mode as that SDK is most likely
361
+ // going to be used by Android Studio, and we don't know what updates it may apply
362
+ // that could be incompatible with the mode. So better keep using the local SDK in
363
+ // the preferences.
364
+ final String sdkPrefsPath = Preferences .get ("android.sdk.path" );
365
+ if (sdkPrefsPath != null ) {
366
+ try {
367
+ final AndroidSDK androidSDK = new AndroidSDK (new File (sdkPrefsPath ));
368
+ Preferences .set ("android.sdk.path" , sdkPrefsPath );
369
+ return androidSDK ;
370
+ } catch (final BadSDKException badPref ) {
371
+ Preferences .unset ("android.sdk.path" );
372
+ loadError = INVALID_SDK ;
373
+ }
374
+ }
354
375
355
- // The environment variable is king. The preferences.txt entry is a page.
356
376
final String sdkEnvPath = Platform .getenv ("ANDROID_SDK" );
357
377
if (sdkEnvPath != null ) {
358
378
try {
@@ -362,27 +382,14 @@ public static AndroidSDK load() throws IOException {
362
382
// which nukes all env variables when launching from the IDE.
363
383
Preferences .set ("android.sdk.path" , sdkEnvPath );
364
384
return androidSDK ;
365
- } catch (final BadSDKException drop ) { }
366
- }
367
-
368
- // If android.sdk.path exists as a preference, make sure that the folder
369
- // is not bogus, otherwise the SDK may have been removed or deleted.
370
- final String sdkPrefsPath = Preferences .get ("android.sdk.path" );
371
- if (sdkPrefsPath != null ) {
372
- try {
373
- final AndroidSDK androidSDK = new AndroidSDK (new File (sdkPrefsPath ));
374
- // Set this value in preferences.txt, in case ANDROID_SDK
375
- // gets knocked out later. For instance, by that pesky Eclipse,
376
- // which nukes all env variables when launching from the IDE.
377
- Preferences .set ("android.sdk.path" , sdkPrefsPath );
378
- return androidSDK ;
379
- } catch (final BadSDKException wellThatsThat ) {
380
- Preferences .unset ("android.sdk.path" );
381
- SDK_LOAD_ERROR = INVALID_SDK ;
385
+ } catch (final BadSDKException badEnv ) {
386
+ Preferences .unset ("android.sdk.path" );
387
+ loadError = INVALID_SDK ;
382
388
}
383
- } else {
384
- SDK_LOAD_ERROR = MISSING_SDK ;
389
+ } else if ( loadError == NO_ERROR ) {
390
+ loadError = MISSING_SDK ;
385
391
}
392
+
386
393
return null ;
387
394
}
388
395
@@ -467,10 +474,10 @@ static public int showLocateDialog(Frame editor) {
467
474
"width: " + TEXT_WIDTH + "px }" +
468
475
"</style> </head>" ;
469
476
String title = "" ;
470
- if (SDK_LOAD_ERROR == MISSING_SDK ) {
477
+ if (loadError == MISSING_SDK ) {
471
478
htmlString += "<body> <p>" + MISSING_SDK_MESSAGE + "</p> </body> </html>" ;
472
479
title = MISSING_SDK_TITLE ;
473
- } else if (SDK_LOAD_ERROR == INVALID_SDK ) {
480
+ } else if (loadError == INVALID_SDK ) {
474
481
htmlString += "<body> <p>" + INVALID_SDK_MESSAGE + "</p> </body> </html>" ;
475
482
title = INVALID_SDK_TITLE ;
476
483
}
0 commit comments