Пример #1
0
        string GetResourceDesignerClass(MetadataReader reader)
        {
            // Looking for:
            // [assembly: Android.Runtime.ResourceDesignerAttribute("MyApp.Resource", IsApplication=true)]

            var assembly = reader.GetAssemblyDefinition();

            foreach (var handle in assembly.GetCustomAttributes())
            {
                var attribute = reader.GetCustomAttribute(handle);
                var fullName  = reader.GetCustomAttributeFullName(attribute);
                if (fullName == "Android.Runtime.ResourceDesignerAttribute")
                {
                    var values = attribute.GetCustomAttributeArguments();
                    foreach (var arg in values.NamedArguments)
                    {
                        // application resource IDs are constants, cannot merge.
                        if (arg.Name == "IsApplication" && arg.Value is bool isApplication && isApplication)
                        {
                            return(null);
                        }
                    }
                    return((string)values.FixedArguments.First().Value);
                }
            }
            return(null);
        }
Пример #2
0
 /// <summary>
 /// Returns the TargetFrameworkIdentifier of an assembly, or null if not found
 /// </summary>
 public static string GetTargetFrameworkIdentifier(this AssemblyDefinition assembly, MetadataReader reader)
 {
     foreach (var handle in assembly.GetCustomAttributes())
     {
         var attribute = reader.GetCustomAttribute(handle);
         var name      = reader.GetCustomAttributeFullName(attribute);
         if (name == "System.Runtime.Versioning.TargetFrameworkAttribute")
         {
             var arguments = attribute.GetCustomAttributeArguments();
             foreach (var p in arguments.FixedArguments)
             {
                 // Of the form "MonoAndroid,Version=v8.1"
                 var value = p.Value?.ToString();
                 if (!string.IsNullOrEmpty(value))
                 {
                     int commaIndex = value.IndexOf(",", StringComparison.Ordinal);
                     if (commaIndex != -1)
                     {
                         return(value.Substring(0, commaIndex));
                     }
                 }
             }
             return(null);
         }
     }
     return(null);
 }
Пример #3
0
        bool IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader)
        {
            foreach (var handle in assembly.GetCustomAttributes())
            {
                var attribute = reader.GetCustomAttribute(handle);
                var name      = reader.GetCustomAttributeFullName(attribute);
                switch (name)
                {
                case "System.Runtime.Versioning.TargetFrameworkAttribute":
                    string targetFrameworkIdentifier = null;
                    foreach (var p in attribute.GetCustomAttributeArguments().FixedArguments)
                    {
                        // Of the form "MonoAndroid,Version=v8.1"
                        var value = p.Value?.ToString();
                        if (!string.IsNullOrEmpty(value))
                        {
                            int commaIndex = value.IndexOf(",", StringComparison.Ordinal);
                            if (commaIndex != -1)
                            {
                                targetFrameworkIdentifier = value.Substring(0, commaIndex);
                                break;
                            }
                        }
                    }
                    if (string.Equals(targetFrameworkIdentifier, "MonoAndroid", StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                    break;

                case "System.Runtime.Versioning.TargetPlatformAttribute":
                    foreach (var p in attribute.GetCustomAttributeArguments().FixedArguments)
                    {
                        // Of the form "android30"
                        var value = p.Value?.ToString();
                        if (value != null && value.StartsWith("android", StringComparison.OrdinalIgnoreCase))
                        {
                            return(true);
                        }
                    }
                    break;

                case "Android.IncludeAndroidResourcesFromAttribute":
                case "Android.NativeLibraryReferenceAttribute":
                case "Java.Interop.JavaLibraryReferenceAttribute":
                    Log.LogCodedError("XA0121", Properties.Resources.XA0121, reader.GetString(assembly.Name), name);
                    break;

                default:
                    break;
                }
            }
            return(false);
        }
Пример #4
0
        string GetTargetFrameworkIdentifier(AssemblyDefinition assembly, MetadataReader reader)
        {
            string targetFrameworkIdentifier = null;

            foreach (var handle in assembly.GetCustomAttributes())
            {
                var attribute = reader.GetCustomAttribute(handle);
                var name      = reader.GetCustomAttributeFullName(attribute);
                switch (name)
                {
                case "System.Runtime.Versioning.TargetFrameworkAttribute":
                    var arguments = attribute.GetCustomAttributeArguments();
                    foreach (var p in arguments.FixedArguments)
                    {
                        // Of the form "MonoAndroid,Version=v8.1"
                        var value = p.Value?.ToString();
                        if (!string.IsNullOrEmpty(value))
                        {
                            int commaIndex = value.IndexOf(",", StringComparison.Ordinal);
                            if (commaIndex != -1)
                            {
                                targetFrameworkIdentifier = value.Substring(0, commaIndex);
                                break;
                            }
                        }
                    }
                    break;

                case "Android.IncludeAndroidResourcesFromAttribute":
                case "Android.NativeLibraryReferenceAttribute":
                case "Java.Interop.JavaLibraryReferenceAttribute":
                    Log.LogCodedError("XA0121",
                                      $"Assembly '{reader.GetString (assembly.Name)}' is using '[assembly: {name}]', which is no longer supported. Use a newer version of this NuGet package or notify the library author.");
                    break;

                default:
                    break;
                }
            }
            return(targetFrameworkIdentifier);
        }
Пример #5
0
        void CheckAssemblyAttributes(AssemblyDefinition assembly, string assemblyName, MetadataReader reader, out string targetFrameworkIdentifier)
        {
            targetFrameworkIdentifier = null;

            foreach (var handle in assembly.GetCustomAttributes())
            {
                var attribute         = reader.GetCustomAttribute(handle);
                var attributeFullName = reader.GetCustomAttributeFullName(attribute);
                switch (attributeFullName)
                {
                case "Java.Interop.DoNotPackageAttribute": {
                    LogCodedWarning("XA0122", Properties.Resources.XA0122, assemblyName, attributeFullName);
                    var arguments = attribute.GetCustomAttributeArguments();
                    if (arguments.FixedArguments.Length > 0)
                    {
                        string file = arguments.FixedArguments [0].Value?.ToString();
                        if (string.IsNullOrWhiteSpace(file))
                        {
                            LogCodedError("XA2008", Properties.Resources.XA2008, assembly.GetAssemblyName().FullName);
                        }
                        do_not_package_atts.Add(Path.GetFileName(file));
                    }
                }
                break;

                case "System.Runtime.Versioning.TargetFrameworkAttribute": {
                    var arguments = attribute.GetCustomAttributeArguments();
                    foreach (var p in arguments.FixedArguments)
                    {
                        // Of the form "MonoAndroid,Version=v8.1"
                        var value = p.Value?.ToString();
                        if (!string.IsNullOrEmpty(value))
                        {
                            int commaIndex = value.IndexOf(",", StringComparison.Ordinal);
                            if (commaIndex != -1)
                            {
                                targetFrameworkIdentifier = value.Substring(0, commaIndex);
                                if (targetFrameworkIdentifier == "MonoAndroid")
                                {
                                    const string match        = "Version=";
                                    var          versionIndex = value.IndexOf(match, commaIndex, StringComparison.Ordinal);
                                    if (versionIndex != -1)
                                    {
                                        versionIndex += match.Length;
                                        string version  = value.Substring(versionIndex, value.Length - versionIndex);
                                        var    apiLevel = MonoAndroidHelper.SupportedVersions.GetApiLevelFromFrameworkVersion(version);
                                        if (apiLevel != null)
                                        {
                                            api_levels [assemblyName] = apiLevel.Value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;

                default:
                    break;
                }
            }
        }