public static bool TryLocateJavaSdk(out string sdkPath) { string path; // Check #1 - JAVA_HOME environment variables path = GetEnvironmentVariable("JAVA_HOME"); if (!string.IsNullOrEmpty(path)) { sdkPath = path; return(true); } // Check #2 - Default Microsoft's Mobile OpenJDK location // https://docs.microsoft.com/en-us/xamarin/android/get-started/installation/openjdk#troubleshooting string jdkRoot; if (OperatingSystem.IsWindows()) { jdkRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Android", "jdk"); } else { jdkRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Developer", "Xamarin", "jdk"); } path = Directory.EnumerateDirectories(jdkRoot, "microsoft_dist_openjdk_1.8.0.*").FirstOrDefault(); if (!string.IsNullOrEmpty(path) && Directory.Exists(path)) { sdkPath = path; return(true); } // Check #3 - Try look for Java SDK location inside Android Studio settings if (TryParseAndroidStudioConfig("JavaSDK", out path)) { sdkPath = path; return(true); } // Check #4 - Try locate java executable/script if (FlutnetShell.TryLocateFile("java", out string javaFullPath)) { try { sdkPath = Path.GetDirectoryName(Path.GetDirectoryName(javaFullPath)); return(true); } catch { // ignored } } sdkPath = default; return(false); }
public static bool TryLocateAndroidSdk(out string sdkPath) { string path; // Check #1 - ANDROID_SDK_ROOT or ANDROID_HOME environment variables path = GetEnvironmentVariable("ANDROID_SDK_ROOT"); if (!string.IsNullOrEmpty(path)) { sdkPath = path; return(true); } path = GetEnvironmentVariable("ANDROID_HOME"); if (!string.IsNullOrEmpty(path)) { sdkPath = path; return(true); } // Check #2 - Default Xamarin Android SDK location if (OperatingSystem.IsWindows()) { // https://docs.microsoft.com/en-us/xamarin/android/troubleshooting/questions/android-sdk-location?tabs=windows path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Android", "android-sdk"); } else { // https://docs.microsoft.com/en-us/xamarin/android/troubleshooting/questions/android-sdk-location?tabs=macos path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Developer", "Xamarin", "android-sdk-macosx"); } if (Directory.Exists(path)) { sdkPath = path; return(true); } // Check #3 - Try look for Android SDK location inside Android Studio settings if (TryParseAndroidStudioConfig("Android SDK", out path)) { sdkPath = path; return(true); } // Check #4 - Try locate adb executable/script //NOTE: This command can fail also on machines where Android SDK and Xamarin.Android are correctly installed // (You're not required to add Android SDK platform-tools folder - where adb resides - to PATH environment variable) if (FlutnetShell.TryLocateFile("adb", out string adbFullPath)) { try { sdkPath = Path.GetDirectoryName(Path.GetDirectoryName(adbFullPath)); return(true); } catch { // ignored } } sdkPath = default; return(false); }
public static bool TryParseAndroidStudioConfig(string sdkType, out string sdkPath) { // https://developer.android.com/studio/intro/studio-config // Android Studio 4.1: The locations of user configuration directories have been changed // https://developer.android.com/studio/releases#directory-configuration-changes try { if (OperatingSystem.IsWindows()) { // Example: C:\Users\YourUserName\AppData\Roaming\Google\AndroidStudio4.1 foreach (string dir in Directory.EnumerateDirectories( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Google"), "AndroidStudio*", SearchOption.TopDirectoryOnly)) { string jdkTableFile = Path.Combine(dir, "options", "jdk.table.xml"); if (ParseAndValidateJdkTable(jdkTableFile, sdkType, out string path)) { sdkPath = path; return(true); } } // Example: C:\Users\YourUserName\.AndroidStudio4.0 foreach (string dir in Directory.EnumerateDirectories( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".AndroidStudio*", SearchOption.TopDirectoryOnly)) { string jdkTableFile = Path.Combine(dir, "config", "options", "jdk.table.xml"); if (ParseAndValidateJdkTable(jdkTableFile, sdkType, out string path)) { sdkPath = path; return(true); } } } else if (OperatingSystem.IsMacOS()) { // Example: ~/Library/Application Support/Google/AndroidStudio4.1 foreach (string dir in Directory.EnumerateDirectories( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Application Support", "Google"), "AndroidStudio*", SearchOption.TopDirectoryOnly)) { string jdkTableFile = Path.Combine(dir, "options", "jdk.table.xml"); if (ParseAndValidateJdkTable(jdkTableFile, sdkType, out string path)) { sdkPath = path; return(true); } } // Example: ~/Library/Preferences/AndroidStudio4.0 foreach (string dir in Directory.EnumerateDirectories( Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library", "Preferences"), "AndroidStudio*", SearchOption.TopDirectoryOnly)) { string jdkTableFile = Path.Combine(dir, "options", "jdk.table.xml"); if (ParseAndValidateJdkTable(jdkTableFile, sdkType, out string path)) { sdkPath = path; return(true); } } } } catch { // ignored } sdkPath = default; return(false); }