Android SDK location

后端 未结 15 1710
一整个雨季
一整个雨季 2020-11-28 05:48

I have Xamarin Studio, and I need to specify the Android SDK Location. I have previously had Xamarin Studio working on my pc, and for some reason, I need to enter this again

15条回答
  •  离开以前
    2020-11-28 06:12

    The question doesn't seem to require a programmatic solution, but my Google search brought me here anyway. Here's my C# attempt at detecting where the SDK is installed, based on the most common installation paths.

    static string FindAndroidSDKPath()
    {
        string uniqueFile = Path.Combine("platform-tools", "adb.exe"); // look for adb in Android folders
        string[] searchDirs =
        {
            // User/AppData/Local
            Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
            // Program Files
            Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
            // Program Files (x86) (it's okay if we're on 32-bit, we check if this folder exists first)
            Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + " (x86)",
            // User/AppData/Roaming
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
        };
        foreach (string searchDir in searchDirs)
        {
            string androidDir = Path.Combine(searchDir, "Android");
            if (Directory.Exists(androidDir))
            {
                string[] subDirs = Directory.GetDirectories(androidDir, "*sdk*", SearchOption.TopDirectoryOnly);
                foreach (string subDir in subDirs)
                {
                    string path = Path.Combine(subDir, uniqueFile);
                    if (File.Exists(path))
                    {
                        // found unique file at DIR/Android
                        return subDir;
                    }
                }
            }
        }
        // no luck finding SDK! :(
        return null;
    }
    

    I need this because I'm writing an extension to a C# program to work with Android Studio/Gradle. Hopefully someone else will find this approach useful.

提交回复
热议问题