dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

[AndroidManifest] Add support for `<property>` element via `PropertyAttribute`. #9016

Closed jpobst closed 2 months ago

jpobst commented 3 months ago

Fixes: https://github.com/xamarin/xamarin-android/issues/8729

Adds support for specifying the AndroidManifest.xml <property> element via a new [Property] attribute. This appears to be the exact same as <meta-data> but with a different name, so it is implemented exactly the same.

Example:

[Service (Name = "fooService", ForegroundServiceType = Android.Content.PM.ForegroundService.TypeSpecialUse)]
[Property ("android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE", Value = "explanation_for_special_use")]
public class FooService : Service
{ ... }

creates:

<service android:name="fooService" android:foregroundServiceType="specialUse">
  <property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
      android:value="explanation_for_special_use" />
</service>

This element can be applied to <application>, <activity>, <provider>, <receiver>, and <service> elements.

jonpryor commented 2 months ago

Proposed commit message:

Fixes: https://github.com/xamarin/xamarin-android/issues/8729

API-31 added support for a [`<property/>`][0] element within
`AndroidManifest.xml`, which can be contained within `<activity/>`, 
`<application/>`, `<provider/>`, `<receiver/>`, and `<service/>`
elements.

Add a new `Android.App.PropertyAttribute` custom attribute which will
emit `<property/>` within `AndroidManifest.xml`:

    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class,
            AllowMultiple = true, Inherited = false)]
    public partial class PropertyAttribute : Attribute {

        public string Name      {get;}
        public string? Resource {get;}
        public string? Value    {get;}

        public PropertyAttribute (string name);
    }

Semantics otherwise appear to be identical to
`<meta-data/>`/`MetaDataAttribute`: only `Resource` or `Value` can
be specified, etc.  (This is checked by `aapt2`.)

Using `[assembly:Property(…)]` will result in `<property/>` being
emitted within the `<application/>` element.

For example:

    [Service (Name = "fooService", ForegroundServiceType = Android.Content.PM.ForegroundService.TypeSpecialUse)]
    [Property ("android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE", Value = "explanation_for_special_use")]
    public partial class FooService : Service {
    }

will emit the `AndroidManifest.xml` fragment:

    <service android:name="fooService" android:foregroundServiceType="specialUse">
      <property
          android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
          android:value="explanation_for_special_use"
      />
    </service>

[0]: https://developer.android.com/guide/topics/manifest/property-element
entdark commented 2 months ago

Hi, what version will it appear in?

jpobst commented 2 months ago

It will be in .NET 9 Preview 7.