Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Jun 11, 2024

Fixes: #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.

@jpobst jpobst force-pushed the property-manifest branch 2 times, most recently from 099bb67 to d8bfe6e Compare July 9, 2024 18:55
@jpobst jpobst marked this pull request as ready for review July 10, 2024 14:09
Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really have any concerns except the classes at the bottom of a file. 👍

Comment on lines +1316 to +1322
// Dummy types because we don't want to take a dependency on Mono.Android or Java.Interop
namespace Android.App
{
public class Activity : Java.Interop.IJavaPeerable { }
public class Application : Java.Interop.IJavaPeerable { }
public class Service : Java.Interop.IJavaPeerable { }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put these in a separate file, to make them easier to find? I could see another test wanting to use these at some point, and they are useable within the entire project.

Comment on lines +583 to +586
var properties =
Assemblies.SelectMany (path => PropertyAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path), cache))
.Where (attr => attr != null)
.ToList ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be fixed here, but I wonder if the N Assemblies.SelectMany() calls could be a performance issue.

@jonpryor
Copy link
Member

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

@jonpryor jonpryor merged commit 3ab04df into main Jul 10, 2024
56 of 58 checks passed
@jonpryor jonpryor deleted the property-manifest branch July 10, 2024 19:57
@entdark
Copy link

entdark commented Jul 11, 2024

Hi, what version will it appear in?

@jpobst
Copy link
Contributor Author

jpobst commented Jul 11, 2024

It will be in .NET 9 Preview 7.

grendello added a commit that referenced this pull request Jul 23, 2024
* main: (23 commits)
  Localized file check-in by OneLocBuild Task (#9129)
  [ci] Disable CodeQL on CI/PR pipelines (#9128)
  Refine 16k page alignment support (#9075)
  [build] fix `ConfigureLocalWorkload` target (#9124)
  Bump to NDK r27 (#9020)
  [ci] Use drop service for SDK insertion artifacts  (#9116)
  Fix up all mapping paths (#9121)
  [ci] Fix maestro publishing for stable packages (#9118)
  Bump to dotnet/sdk@2f14fea98b 9.0.100-preview.7.24367.21 (#9108)
  Missing androidx.window.[extensions|sidecar] warnings (#9085)
  [ci] Use sign-artifacts template for macOS signing (#9091)
  [ci] Use DotNetCoreCLI to sign macOS files (#9102)
  [ci] Disable CodeQL on macOS, Linux, non-main jobs (#9111)
  [tests] re-enable `JavaAbstractMethodTest` (#9097)
  [Microsoft.Android.Sdk.ILLink] preserve types with `IJniNameProviderAttribute` (#9099)
  [Mono.Android] Data sharing and Close() overrides (#9103)
  [AndroidManifest] Add `Android.App.PropertyAttribute` (#9016)
  [Mono.Android] Add support for AndroidMessageHandler ClientCertificates (#8961)
  [Mono.Android] Bind and enumify API-35 (#9043)
  Bump to dotnet/java-interop@7a058c0e (#9066)
  ...
@github-actions github-actions bot locked and limited conversation to collaborators Aug 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to specify PROPERTY_SPECIAL_USE_FGS_SUBTYPE via Service Attribute
5 participants