这个问题似乎不需要编程解决方案,但是无论如何,我的Google搜索都将我带到了这里。这是我的C#尝试根据最常见的安装路径来检测SDK的安装位置。
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;
}
我需要这样做,因为我正在编写C#程序的扩展以与Android Studio / Gradle一起使用。希望其他人会发现此方法有用。