Exemple #1
0
        public override Task <DiagnosticResult> Examine(SharedState history)
        {
            AndroidSdk.AvdManager avdManager = null;

            var    javaHome = history.GetEnvironmentVariable("JAVA_HOME");
            string java     = null;

            if (!string.IsNullOrEmpty(javaHome) && Directory.Exists(javaHome))
            {
                java = Path.Combine(javaHome, "bin", "java" + (Util.IsWindows ? ".exe" : ""));
            }

            var avds = new List <AndroidSdk.AvdManager.Avd>();

            // Try invoking the java avdmanager library first
            if (File.Exists(java))
            {
                avdManager = new AndroidSdk.AvdManager(java,
                                                       history.GetEnvironmentVariable("ANDROID_SDK_ROOT") ?? history.GetEnvironmentVariable("ANDROID_HOME"));
                avds.AddRange(avdManager.ListAvds());
            }

            // Fallback to manually reading the avd files
            if (!avds.Any())
            {
                avds.AddRange(AndroidSdk.AvdManager.ListAvdsFromFiles());
            }

            if (avds.Any())
            {
                var emu = avds.FirstOrDefault();

                ReportStatus($"Emulator: {emu.Name ?? emu.SdkId} found.", Status.Ok);

                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }

            // If we got here, there are no emulators at all
            var missingEmulators = RequiredEmulators;

            if (!missingEmulators.Any())
            {
                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }

            AndroidSdk.AvdManager.AvdDevice preferredDevice = null;

            try
            {
                if (avdManager != null)
                {
                    var devices = avdManager.ListDevices();

                    preferredDevice = devices.FirstOrDefault(d => d.Name.Contains("pixel", StringComparison.OrdinalIgnoreCase));
                }
            }
            catch (Exception ex)
            {
                var msg = "Unable to find any Android Emulators.  You can use Visual Studio to create one if necessary: [underline]https://docs.microsoft.com/xamarin/android/get-started/installation/android-emulator/device-manager[/]";

                ReportStatus(msg, Status.Warning);

                Util.Exception(ex);
                return(Task.FromResult(
                           new DiagnosticResult(Status.Warning, this, msg)));
            }

            return(Task.FromResult(new DiagnosticResult(
                                       Status.Error,
                                       this,
                                       new Suggestion("Create an Android Emulator",
                                                      missingEmulators.Select(me =>
                                                                              new ActionSolution((sln, cancel) =>
            {
                try
                {
                    var installer = new AndroidSDKInstaller(new Helper(), AndroidManifestType.GoogleV2);
                    installer.Discover();

                    var sdkInstance = installer.FindInstance(null);

                    var installedPackages = sdkInstance.Components.AllInstalled(true);

                    var sdkPackage = installedPackages.FirstOrDefault(p => p.Path.Equals(me.SdkId, StringComparison.OrdinalIgnoreCase));

                    if (sdkPackage == null && (me.AlternateSdkIds?.Any() ?? false))
                    {
                        sdkPackage = installedPackages.FirstOrDefault(p => me.AlternateSdkIds.Any(a => a.Equals(p.Path, StringComparison.OrdinalIgnoreCase)));
                    }

                    var sdkId = sdkPackage?.Path ?? me.SdkId;

                    avdManager.Create($"Android_Emulator_{me.ApiLevel}", sdkId, device: preferredDevice?.Id, tag: "google_apis", force: true, interactive: true);
                    return Task.CompletedTask;
                }
                catch (Exception ex)
                {
                    ReportStatus("Unable to create Emulator.  Use Visual Studio to create one instead: https://docs.microsoft.com/xamarin/android/get-started/installation/android-emulator/device-manager", Status.Warning);
                    Util.Exception(ex);
                }

                return Task.CompletedTask;
            })).ToArray()))));
        }
        public override Task <DiagnosticResult> Examine(SharedState history)
        {
            var android = new AndroidSdk.AndroidSdkManager(
                history.GetEnvironmentVariable("ANDROID_SDK_ROOT") ?? history.GetEnvironmentVariable("ANDROID_HOME"));

            var avds = AndroidSdk.AvdManager.ListAvdsFromFiles();             // android.AvdManager.ListAvds();

            // This isn't really helpful anymore as avd cli tools don't seem to work with JDK 11
            //if (!avds.Any())
            //	avds = android.AvdManager.ListAvds();

            if (avds.Any())
            {
                var emu = avds.FirstOrDefault();

                ReportStatus($"Emulator: {emu.Name ?? emu.SdkId} found.", Status.Ok);

                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }

            // If we got here, there are no emulators at all
            var missingEmulators = RequiredEmulators;

            if (!missingEmulators.Any())
            {
                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }

            AndroidSdk.AvdManager.AvdDevice preferredDevice = null;

            try
            {
                var devices = android.AvdManager.ListDevices();

                preferredDevice = devices.FirstOrDefault(d => d.Name.Contains("pixel", StringComparison.OrdinalIgnoreCase));
            }
            catch (Exception ex)
            {
                var msg = "Unable to find any Android Emulators.  You can use Visual Studio to create one if necessary: [underline]https://docs.microsoft.com/xamarin/android/get-started/installation/android-emulator/device-manager[/]";

                ReportStatus(msg, Status.Warning);

                Util.Exception(ex);
                return(Task.FromResult(
                           new DiagnosticResult(Status.Warning, this, msg)));
            }

            return(Task.FromResult(new DiagnosticResult(
                                       Status.Error,
                                       this,
                                       new Suggestion("Create an Android Emulator",
                                                      missingEmulators.Select(me =>
                                                                              new ActionSolution(t =>
            {
                try
                {
                    var installer = new AndroidSDKInstaller(new Helper(), AndroidManifestType.GoogleV2);
                    installer.Discover();

                    var sdkInstance = installer.FindInstance(null);

                    var installedPackages = sdkInstance.Components.AllInstalled(true);

                    var sdkPackage = installedPackages.FirstOrDefault(p => p.Path.Equals(me.SdkId, StringComparison.OrdinalIgnoreCase));

                    if (sdkPackage == null && (me.AlternateSdkIds?.Any() ?? false))
                    {
                        sdkPackage = installedPackages.FirstOrDefault(p => me.AlternateSdkIds.Any(a => a.Equals(p.Path, StringComparison.OrdinalIgnoreCase)));
                    }

                    var sdkId = sdkPackage?.Path ?? me.SdkId;

                    android.AvdManager.Create($"Android_Emulator_{me.ApiLevel}", sdkId, device: preferredDevice?.Id, tag: "google_apis", force: true, interactive: true);
                    return Task.CompletedTask;
                }
                catch (Exception ex)
                {
                    ReportStatus("Unable to create Emulator.  Use Visual Studio to create one instead: https://docs.microsoft.com/xamarin/android/get-started/installation/android-emulator/device-manager", Status.Warning);
                    Util.Exception(ex);
                }

                return Task.CompletedTask;
            })).ToArray()))));
        }
 public override bool ShouldExamine(SharedState history)
 => Manifest?.Check?.VSWin != null &&
 !(history.GetEnvironmentVariable("CI") ?? "false").Equals("true", System.StringComparison.OrdinalIgnoreCase);
Exemple #4
0
        public override Task <DiagnosticResult> Examine(SharedState history)
        {
            var jdkPath = history.GetEnvironmentVariable("JAVA_HOME") ?? Environment.GetEnvironmentVariable("JAVA_HOME");

            string androidSdkPath = null;

            try
            {
                // Set the logger to override the default one that is set in this library
                // So we can catch output from failed path lookups that are otherwise swallowed
                var _ = new AndroidSdkInfo((traceLevel, msg) =>
                {
                    if (Util.Verbose || traceLevel == System.Diagnostics.TraceLevel.Error)
                    {
                        Util.LogAlways(msg);
                    }
                }, androidSdkPath, null, jdkPath);
            }
            catch (Exception ex)
            {
                Util.Exception(ex);
            }

            if (string.IsNullOrEmpty(androidSdkPath))
            {
                androidSdkPath = FindBestSdkLocation();
            }

            var missingPackages = new List <IAndroidComponent>();

            var installer = new AndroidSDKInstaller(new Helper(), AndroidManifestType.GoogleV2);

            installer.Discover(new List <string> {
                androidSdkPath
            });

            var sdkInstance = installer.FindInstance(androidSdkPath);

            if (string.IsNullOrEmpty(sdkInstance?.Path))
            {
                return(Task.FromResult(
                           new DiagnosticResult(
                               Status.Error,
                               this,
                               "Failed to find Android SDK.",
                               new Suggestion("Install the Android SDK",
                                              "For more information see: [underline]https://aka.ms/dotnet-androidsdk-help[/]"))));
            }

            history.SetEnvironmentVariable("ANDROID_SDK_ROOT", sdkInstance.Path);
            history.SetEnvironmentVariable("ANDROID_HOME", sdkInstance.Path);

            var installed = sdkInstance?.Components?.AllInstalled(true);

            foreach (var package in RequiredPackages)
            {
                var v = !string.IsNullOrWhiteSpace(package.Version) ? new AndroidRevision(package.Version) : null;

                var installedPkg = FindInstalledPackage(installed, package)
                                   ?? FindInstalledPackage(installed, package.Alternatives?.ToArray());

                if (installedPkg == null)
                {
                    var pkgToInstall = sdkInstance?.Components?.AllNotInstalled()?
                                       .FirstOrDefault(p => p.Path.Equals(package.Path.Trim(), StringComparison.OrdinalIgnoreCase) &&
                                                       p.Revision >= (v ?? p.Revision));

                    ReportStatus($"{package.Path} ({package.Version}) missing.", Status.Error);

                    if (pkgToInstall != null)
                    {
                        missingPackages.Add(pkgToInstall);
                    }
                }
                else
                {
                    if (!package.Path.Equals(installedPkg.Path) || v != (installedPkg.Revision ?? installedPkg.InstalledRevision))
                    {
                        ReportStatus($"{installedPkg.Path} ({installedPkg.InstalledRevision ?? installedPkg.Revision})", Status.Ok);
                    }
                    else
                    {
                        ReportStatus($"{package.Path} ({package.Version})", Status.Ok);
                    }
                }
            }

            if (!missingPackages.Any())
            {
                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }


            var installationSet = installer.GetInstallationSet(sdkInstance, missingPackages);

            var desc =
                @$ "Your Android SDK has missing or outdated packages.
You can use the Android SDK Manager to install / update them.
For more information see: [underline]https://aka.ms/dotnet-androidsdk-help[/]";
Exemple #5
0
        public override Task <DiagnosticResult> Examine(SharedState history)
        {
            var android = new AndroidSdk.AndroidSdkManager(
                history.GetEnvironmentVariable("ANDROID_SDK_ROOT") ?? history.GetEnvironmentVariable("ANDROID_HOME"));

            var avds = AndroidSdk.AvdManager.ListAvdsFromFiles();

            if (!avds.Any())
            {
                avds = android.AvdManager.ListAvds();
            }

            if (avds.Any())
            {
                var emu = avds.FirstOrDefault();

                ReportStatus($"Emulator: {emu.Name ?? emu.SdkId} found.", Status.Ok);

                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }

            var missingEmulators = new List <AndroidEmulator>();

            foreach (var emu in RequiredEmulators)
            {
                var existingAvd = avds.FirstOrDefault(a => a.SdkId.Equals(emu.SdkId, StringComparison.OrdinalIgnoreCase));

                if (existingAvd == null)
                {
                    missingEmulators.Add(emu);
                    ReportStatus($"{emu.Description ?? emu.SdkId} not created.", Status.Error);
                }
                else
                {
                    ReportStatus($"{emu.Description ?? emu.SdkId} exists.", Status.Ok);
                }
            }

            if (!missingEmulators.Any())
            {
                return(Task.FromResult(DiagnosticResult.Ok(this)));
            }

            var devices = android.AvdManager.ListDevices();

            var preferredDevice = devices.FirstOrDefault(d => d.Name.Contains("pixel", StringComparison.OrdinalIgnoreCase));

            return(Task.FromResult(new DiagnosticResult(
                                       Status.Error,
                                       this,
                                       new Suggestion("Create an Android Emulator",
                                                      missingEmulators.Select(me =>
                                                                              new ActionSolution(t =>
            {
                var installer = new AndroidSDKInstaller(new Helper(), AndroidManifestType.GoogleV2);
                installer.Discover();

                var sdkInstance = installer.FindInstance(null);

                var installedPackages = sdkInstance.Components.AllInstalled(true);

                var sdkPackage = installedPackages.FirstOrDefault(p => p.Path.Equals(me.SdkId, StringComparison.OrdinalIgnoreCase));

                if (sdkPackage == null && (me.AlternateSdkIds?.Any() ?? false))
                {
                    sdkPackage = installedPackages.FirstOrDefault(p => me.AlternateSdkIds.Any(a => a.Equals(p.Path, StringComparison.OrdinalIgnoreCase)));
                }

                var sdkId = sdkPackage?.Path ?? me.SdkId;

                android.AvdManager.Create($"Android_Emulator_{me.ApiLevel}", sdkId, device: preferredDevice?.Id, tag: "google_apis", force: true, interactive: true);
                return Task.CompletedTask;
            })).ToArray()))));
        }