cvanbeek
cvanbeek

Reputation: 1961

Declarative Obfuscation in Xamarin Forms with ConfuserEx

I'm creating an Android App using Xamarin.Forms that I'm obfuscating with ConfuserEx. I wanted to use declarative obfuscation like in this example so I could change the Obfuscation properties for each of my classes.

However, the System.Reflection namespace in Xamarin.Forms doesn't recognize the System.Reflection.ObfuscationAttribute class. Do I need to use another NuGet package or am I missing something?

Otherwise, is there a way to include or exclude obfuscation features in different classes a different way?

Upvotes: 0

Views: 508

Answers (1)

SushiHangover
SushiHangover

Reputation: 74134

ConfuserEx is only looking at the name of the attribute:

if (ca.TypeFullName != "System.Reflection.ObfuscationAttribute")

So I would just create a System.Reflection.ObfuscationAttribute class in the PCL (Xamarin.Forms) project itself.

i.e.

using System.Runtime.InteropServices;

namespace System.Reflection
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false), ComVisible(true)]
    public sealed class ObfuscationAttribute : Attribute
    {
        //
        // Fields
        //
        private bool m_strip = true;

        private bool m_exclude = true;

        private bool m_applyToMembers = true;

        private string m_feature = "all";

        //
        // Properties
        //
        public bool ApplyToMembers
        {
            get
            {
                return this.m_applyToMembers;
            }
            set
            {
                this.m_applyToMembers = value;
            }
        }

        public bool Exclude
        {
            get
            {
                return this.m_exclude;
            }
            set
            {
                this.m_exclude = value;
            }
        }

        public string Feature
        {
            get
            {
                return this.m_feature;
            }
            set
            {
                this.m_feature = value;
            }
        }

        public bool StripAfterObfuscation
        {
            get
            {
                return this.m_strip;
            }
            set
            {
                this.m_strip = value;
            }
        }
    }
}

Re: https://github.com/yck1509/ConfuserEx/blob/3c9c29d9daf2f1259edf69054c5693d5d225a980/Confuser.Core/ObfAttrMarker.cs#L138

Upvotes: 1

Related Questions