Esempio n. 1
0
 static string GetValidPath(string description, string path, Func <string, bool> validator, Func <string> defaultPath)
 {
     if (!string.IsNullOrEmpty(path))
     {
         if (Directory.Exists(path))
         {
             if (validator(path))
             {
                 return(path);
             }
             AndroidLogger.LogWarning(null, "{0} path '{1}' is explicitly specified, but it was not valid; skipping.", description, path);
         }
         else
         {
             AndroidLogger.LogWarning(null, "{0} path '{1}' is explicitly specified, but it was not found; skipping.", description, path);
         }
     }
     path = defaultPath();
     if (path != null && validator(path))
     {
         return(path);
     }
     if (path != null)
     {
         AndroidLogger.LogWarning(null, "{0} path is defaulted to '{1}', but it was not valid; skipping", description, path);
     }
     else
     {
         AndroidLogger.LogWarning(null, "{0} path is not found and no default location is provided; skipping", description);
     }
     return(null);
 }
Esempio n. 2
0
        protected override string FindRuntime()
        {
            string monoAndroidPath = Environment.GetEnvironmentVariable("MONO_ANDROID_PATH");

            if (!string.IsNullOrEmpty(monoAndroidPath))
            {
                string msbuildDir = Path.Combine(monoAndroidPath, "lib", "xamarin.android", "xbuild", "Xamarin", "Android");
                if (Directory.Exists(msbuildDir))
                {
                    if (ValidateRuntime(msbuildDir))
                    {
                        return(msbuildDir);
                    }
                    AndroidLogger.LogInfo(null, "MONO_ANDROID_PATH points to {0}, but it is invalid.", monoAndroidPath);
                }
                else
                {
                    AndroidLogger.LogInfo(null, "MONO_ANDROID_PATH points to {0}, but it does not exist.", monoAndroidPath);
                }
            }

            // check also in the users folder
            var personal = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var additionalSearchPaths = new [] {
                Path.GetFullPath(Path.GetDirectoryName(GetType().Assembly.Location)),
                Path.Combine(personal, @".xamarin.android/lib/xamarin.android/xbuild/Xamarin/Android")
            };

            return(additionalSearchPaths.Concat(SearchPaths).FirstOrDefault(ValidateRuntime));
        }
Esempio n. 3
0
        internal static int GetManifestVersion(string file)
        {
            // It seems that MfA 1.0 on Windows didn't include the xml files to get the runtime version.
            if (!File.Exists(file))
            {
                return(int.MaxValue);
            }

            try {
                using (var r = XmlReader.Create(file, GetSafeReaderSettings())) {
                    if (r.MoveToContent() == XmlNodeType.Element && r.MoveToAttribute("android:versionCode"))
                    {
                        int value;
                        if (int.TryParse(r.Value, out value))
                        {
                            return(value);
                        }
                        AndroidLogger.LogInfo("Cannot parse runtime version code: ({0})", r.Value);
                    }
                }
            } catch (Exception ex) {
                AndroidLogger.LogError("Error trying to find shared runtime version", ex);
            }
            return(int.MaxValue);
        }
        public AndroidVersions(IEnumerable <string> frameworkDirectories)
        {
            if (frameworkDirectories == null)
            {
                throw new ArgumentNullException(nameof(frameworkDirectories));
            }

            var dirs = new List <string> ();

            foreach (var d in frameworkDirectories)
            {
                if (!Directory.Exists(d))
                {
                    throw new ArgumentException($"`{d}` must be a directory!", nameof(frameworkDirectories));
                }

                var dp = d.TrimEnd(Path.DirectorySeparatorChar);
                var dn = Path.GetFileName(dp);
                // In "normal" use, `dp` will contain e.g. `...\MonoAndroid\v1.0`.
                // We want the `MonoAndroid` dir, not the versioned dir.
                var p = dn.StartsWith("v", StringComparison.Ordinal) ? Path.GetDirectoryName(dp) : dp;
                dirs.Add(Path.GetFullPath(p));
            }

            dirs = dirs.Distinct(StringComparer.OrdinalIgnoreCase)
                   .ToList();

            FrameworkDirectories = new ReadOnlyCollection <string> (dirs);

            ReadAndroidVersions();

            AndroidLogger.LogInfo(null, "Xamarin.Android Supported $(TargetFrameworkVersion)s: {0}", string.Join(", ", installedVersions));
        }
Esempio n. 5
0
        protected override string FindRuntime()
        {
            string monoAndroidPath = Environment.GetEnvironmentVariable("MONO_ANDROID_PATH");

            if (!string.IsNullOrEmpty(monoAndroidPath))
            {
                string libMandroid = Path.Combine(monoAndroidPath, "lib", "mandroid");
                if (Directory.Exists(libMandroid))
                {
                    if (ValidateRuntime(libMandroid))
                    {
                        return(libMandroid);
                    }
                    AndroidLogger.LogInfo(null, "MONO_ANDROID_PATH points to {0}, but it is invalid.", monoAndroidPath);
                }
                else
                {
                    AndroidLogger.LogInfo(null, "MONO_ANDROID_PATH points to {0}, but it does not exist.", monoAndroidPath);
                }
            }

            // check also in the users folder
            var personal = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var additionalSearchPaths = new [] {
                // for Mono.Posix and Mono.Data.Sqlite builds in xamarin-android.
                monoAndroidPath = Path.GetFullPath(Path.Combine(new Uri(GetType().Assembly.CodeBase).LocalPath, "..", "..", "..", "..", "..", "lib", "mandroid")),
                Path.Combine(personal, @".xamarin.android/lib/mandroid")
            };

            return(additionalSearchPaths.Concat(SearchPaths).FirstOrDefault(ValidateRuntime));
        }
Esempio n. 6
0
        public static void Refresh(string androidSdkPath = null, string androidNdkPath = null, string javaSdkPath = null)
        {
            if (OS.IsWindows)
            {
                sdk = new AndroidSdkWindows();
            }
            else
            {
                sdk = new AndroidSdkUnix();
            }

            try {
                sdk.Initialize(androidSdkPath ?? sdk.PreferedAndroidSdkPath, androidNdkPath ?? sdk.PreferedAndroidNdkPath,
                               javaSdkPath ?? sdk.PreferedJavaSdkPath);
                if (IsInstalled)
                {
                    AndroidLogger.LogInfo(null, "Found Android SDK.");
                }
                else
                {
                    AndroidLogger.LogInfo(null, "Did not find Android SDK");
                }
            } catch (Exception ex) {
                AndroidLogger.LogError("Error finding Android/Java SDKs", ex);
            }
        }
Esempio n. 7
0
        protected override IEnumerable <string> GetAllAvailableAndroidSdks()
        {
            var roots  = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine };
            var wow    = RegistryEx.Wow64.Key32;
            var regKey = GetMDRegistryKey();

            AndroidLogger.LogInfo("sdk", "Looking for Android SDK..");

            // Check for the key the user gave us in the VS/addin options
            foreach (var root in roots)
            {
                if (CheckRegistryKeyForExecutable(root, regKey, MDREG_ANDROID_SDK, wow, "platform-tools", Adb))
                {
                    yield return(RegistryEx.GetValueString(root, regKey, MDREG_ANDROID_SDK, wow));
                }
            }

            // Check for the key written by the Xamarin installer
            if (CheckRegistryKeyForExecutable(RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow, "platform-tools", Adb))
            {
                yield return(RegistryEx.GetValueString(RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow));
            }

            // Check for the key written by the Android SDK installer
            foreach (var root in roots)
            {
                if (CheckRegistryKeyForExecutable(root, ANDROID_INSTALLER_PATH, ANDROID_INSTALLER_KEY, wow, "platform-tools", Adb))
                {
                    yield return(RegistryEx.GetValueString(root, ANDROID_INSTALLER_PATH, ANDROID_INSTALLER_KEY, wow));
                }
            }

            // Check some hardcoded paths for good measure
            var xamarin_private = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Xamarin", "MonoAndroid", "android-sdk-windows");
            var android_default = Path.Combine(OS.ProgramFilesX86, "Android", "android-sdk-windows");
            var cdrive_default  = @"C:\android-sdk-windows";

            if (ValidateAndroidSdkLocation(xamarin_private))
            {
                yield return(xamarin_private);
            }

            if (ValidateAndroidSdkLocation(android_default))
            {
                yield return(android_default);
            }

            if (ValidateAndroidSdkLocation(cdrive_default))
            {
                yield return(cdrive_default);
            }
        }
Esempio n. 8
0
        protected override string GetJavaSdkPath()
        {
            // check the user specified path
            var roots = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine };
            const RegistryEx.Wow64 wow = RegistryEx.Wow64.Key32;
            var regKey = GetMDRegistryKey();

            foreach (var root in roots)
            {
                if (CheckRegistryKeyForExecutable(root, regKey, MDREG_JAVA_SDK, wow, "bin", JarSigner))
                {
                    return(RegistryEx.GetValueString(root, regKey, MDREG_JAVA_SDK, wow));
                }
            }

            string subkey = @"SOFTWARE\JavaSoft\Java Development Kit";

            AndroidLogger.LogInfo("sdk", "Looking for Java 6 SDK..");

            foreach (var wow64 in new[] { RegistryEx.Wow64.Key32, RegistryEx.Wow64.Key64 })
            {
                string key_name       = string.Format(@"{0}\{1}\{2}", "HKLM", subkey, "CurrentVersion");
                var    currentVersion = RegistryEx.GetValueString(RegistryEx.LocalMachine, subkey, "CurrentVersion", wow64);

                if (!string.IsNullOrEmpty(currentVersion))
                {
                    AndroidLogger.LogInfo("sdk", "  Key {0} found.", key_name);

                    // No matter what the CurrentVersion is, look for 1.6 or 1.7 or 1.8
                    if (CheckRegistryKeyForExecutable(RegistryEx.LocalMachine, subkey + "\\" + "1.6", "JavaHome", wow64, "bin", JarSigner))
                    {
                        return(RegistryEx.GetValueString(RegistryEx.LocalMachine, subkey + "\\" + "1.6", "JavaHome", wow64));
                    }

                    if (CheckRegistryKeyForExecutable(RegistryEx.LocalMachine, subkey + "\\" + "1.7", "JavaHome", wow64, "bin", JarSigner))
                    {
                        return(RegistryEx.GetValueString(RegistryEx.LocalMachine, subkey + "\\" + "1.7", "JavaHome", wow64));
                    }

                    if (CheckRegistryKeyForExecutable(RegistryEx.LocalMachine, subkey + "\\" + "1.8", "JavaHome", wow64, "bin", JarSigner))
                    {
                        return(RegistryEx.GetValueString(RegistryEx.LocalMachine, subkey + "\\" + "1.8", "JavaHome", wow64));
                    }
                }

                AndroidLogger.LogInfo("sdk", "  Key {0} not found.", key_name);
            }

            // We ran out of things to check..
            return(null);
        }
Esempio n. 9
0
        public static void Refresh(string runtimePath = null, string binPath = null, string bclPath = null)
        {
            if (OS.IsWindows)
            {
                sdk = new MonoDroidSdkWindows();
            }
            else
            {
                sdk = new MonoDroidSdkUnix();
            }

            try {
                sdk.Initialize(runtimePath, binPath, bclPath);
            } catch (Exception ex) {
                AndroidLogger.LogError("Error finding Xamarin.Android SDK", ex);
            }
        }
Esempio n. 10
0
 static string GetValidPath(string description, string path, Func <string, bool> validator, Func <string> defaultPath)
 {
     if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
     {
         if (validator(path))
         {
             return(path);
         }
         AndroidLogger.LogWarning("{0} path {1} is not valid; skipping.", description, path);
     }
     path = defaultPath();
     if (path != null && validator(path))
     {
         return(path);
     }
     return(null);
 }
Esempio n. 11
0
        protected override IEnumerable <string> GetAllAvailableAndroidNdks()
        {
            var roots  = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine };
            var wow    = RegistryEx.Wow64.Key32;
            var regKey = GetMDRegistryKey();

            AndroidLogger.LogInfo("sdk", "Looking for Android NDK..");

            // Check for the key the user gave us in the VS/addin options
            foreach (var root in roots)
            {
                if (CheckRegistryKeyForExecutable(root, regKey, MDREG_ANDROID_NDK, wow, ".", NdkStack))
                {
                    yield return(RegistryEx.GetValueString(root, regKey, MDREG_ANDROID_NDK, wow));
                }
            }

            /*
             * // Check for the key written by the Xamarin installer
             * if (CheckRegistryKeyForExecutable (RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow, "platform-tools", Adb))
             *      yield return RegistryEx.GetValueString (RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow);
             */

            // Check some hardcoded paths for good measure
            var xamarin_private = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Xamarin", "MonoAndroid");
            var android_default = Path.Combine(OS.ProgramFilesX86, "Android");
            var cdrive_default  = @"C:\";

            foreach (var basePath in new string [] { xamarin_private, android_default, cdrive_default })
            {
                if (Directory.Exists(basePath))
                {
                    foreach (var dir in Directory.GetDirectories(basePath, "android-ndk-r*"))
                    {
                        if (ValidateAndroidNdkLocation(dir))
                        {
                            yield return(dir);
                        }
                    }
                }
            }
        }
 void ReadAndroidVersions()
 {
     foreach (var frameworkDirectory in FrameworkDirectories)
     {
         foreach (var file in Directory.EnumerateFiles(frameworkDirectory, "AndroidApiInfo.xml", SearchOption.AllDirectories))
         {
             try {
                 var v = ToAndroidVersion(file);
                 installedVersions.Add(v);
                 if (MaxStableVersion == null || (v.Stable && MaxStableVersion.Version < v.Version))
                 {
                     MaxStableVersion = v;
                 }
             }
             catch (Exception e) {
                 AndroidLogger.LogError(message: $"Could not create AndroidVersion information for `{file}`.", ex: e);
             }
         }
     }
 }
Esempio n. 13
0
        // expectedRuntimePath: contains Mono.Android.DebugRuntime-*.apk
        // binPath:     contains mandroid
        // mscorlibDir: contains mscorlib.dll
        public void Initialize(string expectedRuntimePath = null, string binPath = null, string bclPath = null)
        {
            var runtimePath = GetValidPath("MonoAndroidToolsPath", expectedRuntimePath, ValidateRuntime, () => FindRuntime());

            if (runtimePath != null)
            {
                binPath = GetValidPath("MonoAndroidBinPath", binPath, ValidateBin, () => FindBin(runtimePath));
                bclPath = GetValidPath("mscorlib.dll", bclPath, ValidateFramework, () => FindFramework(runtimePath));
            }
            else
            {
                if (expectedRuntimePath != null)
                {
                    AndroidLogger.LogWarning(null, "Runtime was not found at {0}", expectedRuntimePath);
                }
                binPath = bclPath = null;
            }

            if (runtimePath == null || binPath == null || bclPath == null)
            {
                Reset();
                return;
            }

            RuntimePath = runtimePath;
                        #pragma warning disable 0618
            SdkPath = Path.GetFullPath(Path.Combine(runtimePath, "..", ".."));
                        #pragma warning restore 0618
            BinPath       = binPath;
            BclPath       = bclPath;
            LibrariesPath = FindLibraries(runtimePath);

            IncludePath = FindInclude(runtimePath);
            if (IncludePath != null && !Directory.Exists(IncludePath))
            {
                IncludePath = null;
            }

            SharedRuntimeVersion = GetCurrentSharedRuntimeVersion();
            FindSupportedFrameworks();
        }
Esempio n. 14
0
        protected virtual string FindRuntime()
        {
            string monoAndroidPath = Environment.GetEnvironmentVariable("MONO_ANDROID_PATH");

            if (!string.IsNullOrEmpty(monoAndroidPath))
            {
                string msbuildDir = Path.Combine(monoAndroidPath, "lib", "xamarin.android", "xbuild", "Xamarin", "Android");
                if (Directory.Exists(msbuildDir))
                {
                    if (ValidateRuntime(msbuildDir))
                    {
                        return(msbuildDir);
                    }
                    AndroidLogger.LogInfo(null, $"MONO_ANDROID_PATH points to {monoAndroidPath}, but `{msbuildDir}{Path.DirectorySeparatorChar}class-parse.exe` does not exist.");
                }
                else
                {
                    AndroidLogger.LogInfo(null, $"MONO_ANDROID_PATH points to {monoAndroidPath}, but it does not exist.");
                }
            }
            return(null);
        }
Esempio n. 15
0
        private bool CheckRegistryKeyForExecutable(UIntPtr key, string subkey, string valueName, RegistryEx.Wow64 wow64, string subdir, string exe)
        {
            string key_name = string.Format(@"{0}\{1}\{2}", key == RegistryEx.CurrentUser ? "HKCU" : "HKLM", subkey, valueName);

            var path = NullIfEmpty(RegistryEx.GetValueString(key, subkey, valueName, wow64));

            if (path == null)
            {
                AndroidLogger.LogInfo("sdk", "  Key {0} not found.", key_name);
                return(false);
            }

            if (!FindExecutableInDirectory(exe, Path.Combine(path, subdir)).Any())
            {
                AndroidLogger.LogInfo("sdk", "  Key {0} found:\n    Path does not contain {1} in \\{2} ({3}).", key_name, exe, subdir, path);
                return(false);
            }

            AndroidLogger.LogInfo("sdk", "  Key {0} found:\n    Path contains {1} in \\{2} ({3}).", key_name, exe, subdir, path);

            return(true);
        }
Esempio n. 16
0
        private static XDocument GetUnixConfigFile()
        {
            var       file = UnixConfigPath;
            XDocument doc  = null;

            if (!File.Exists(file))
            {
                string dir = Path.GetDirectoryName(file);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }
            else
            {
                try {
                    doc = XDocument.Load(file);
                } catch (Exception ex) {
                    AndroidLogger.LogError("Could not load monodroid configuration file", ex);

                    // move out of the way and create a new one
                    doc = new XDocument(new XElement("monodroid"));
                    var newFileName = file + ".old";
                    if (File.Exists(newFileName))
                    {
                        File.Delete(newFileName);
                    }

                    File.Move(file, newFileName);
                }
            }

            if (doc == null || doc.Root == null)
            {
                doc = new XDocument(new XElement("monodroid"));
            }
            return(doc);
        }
Esempio n. 17
0
        public static void Refresh(string androidSdkPath = null, string androidNdkPath = null, string javaSdkPath = null)
        {
            if (OS.IsWindows)
            {
                sdk = new AndroidSdkWindows();
            }
            else
            {
                sdk = new AndroidSdkUnix();
            }

            try {
                sdk.Initialize(androidSdkPath ?? sdk.PreferedAndroidSdkPath, androidNdkPath ?? sdk.PreferedAndroidNdkPath,
                               javaSdkPath ?? sdk.PreferedJavaSdkPath);
                if (IsInstalled)
                {
                    var    levels = GetInstalledPlatformVersions().Select(l => l.ApiLevel.ToString()).ToArray();
                    string levelList;
                    if (levels == null || levels.Length == 0)
                    {
                        levelList = "(none)";
                    }
                    else
                    {
                        levelList = string.Join(", ", levels);
                    }
                    AndroidLogger.LogInfo(null, "Found Android SDK. API levels: {0}", levelList);
                }
                else
                {
                    AndroidLogger.LogInfo(null, "Did not find Android SDK");
                }
            } catch (Exception ex) {
                AndroidLogger.LogError("Error finding Android/Java SDKs", ex);
            }
        }