Skip to content

Commit d2abb10

Browse files
committed
[Mono.Android] Generate default interface members for API-30+ (#4353)
Context: dotnet/java-interop#509 Use `generator --lang-features=default-interface-methods` so that we start binding [Java interface default methods][0] within `android.jar` and `Mono.Android.dll`, for API-R. *Note*: `Microsoft.DotNet.ApiCompat.exe` does not currently understand C#8 [default interface members][1], which we use to bind Java interface default methods. As such, it reports an error for *every added* default interface member, e.g. InterfacesShouldHaveSameMembers : Interface member 'Android.App.Application.IActivityLifecycleCallbacks.OnActivityPostCreated(Android.App.Activity, Android.OS.Bundle)' is present in the implementation but not in the contract. We are investigating improving `Microsoft.DotNet.ApiCompat.exe` so that it doesn't report such changes as incompatible. [0]: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html [1]: https://github.com/dotnet/csharplang/blob/f7952cdddf85316a4beec493a0ecc14fcb3241c8/proposals/csharp-8.0/default-interface-methods.md
1 parent af728f1 commit d2abb10

File tree

5 files changed

+261
-4
lines changed

5 files changed

+261
-4
lines changed

src/Mono.Android/Android.Telephony.Mbms/IGroupCallCallback.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Android.Telephony.Mbms
77
{
8-
public interface IGroupCallCallback : IJavaObject
8+
public partial interface IGroupCallCallback : IJavaObject
99
{
1010
// This is not generated because all the methods in this
1111
// interface are default interface mthods, and the interface

src/Mono.Android/Mono.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<SignAssembly>true</SignAssembly>
1212
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
1313
<NoWarn>0618;0809;0108;0114</NoWarn>
14+
<LangVersion>latest</LangVersion>
1415
</PropertyGroup>
1516
<Import Project="..\..\Configuration.props" />
1617
<PropertyGroup>

src/Mono.Android/Mono.Android.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@
9898
<_Api>$(IntermediateOutputPath)mcw\api.xml</_Api>
9999
<_Dirs>--enumdir=$(IntermediateOutputPath)mcw</_Dirs>
100100
<_FullIntermediateOutputPath>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)'))</_FullIntermediateOutputPath>
101+
<_LangFeatures Condition="$(AndroidApiLevel) &gt;= 30">--lang-features=default-interface-methods</_LangFeatures>
101102
</PropertyGroup>
102103
<Exec
103-
Command="$(ManagedRuntime) $(ManagedRuntimeArgs) $(Generator) $(_GenFlags) $(_ApiLevel) $(_Out) $(_Codegen) $(_Fixup) $(_Enums1) $(_Enums2) $(_Versions) $(_Annotations) $(_Assembly) $(_TypeMap) $(_Dirs) $(_Api)"
104+
Command="$(ManagedRuntime) $(ManagedRuntimeArgs) $(Generator) $(_GenFlags) $(_ApiLevel) $(_Out) $(_Codegen) $(_Fixup) $(_Enums1) $(_Enums2) $(_Versions) $(_Annotations) $(_Assembly) $(_TypeMap) $(_LangFeatures) $(_Dirs) $(_Api)"
104105
/>
105106
<ItemGroup>
106107
<Compile Include="$(_FullIntermediateOutputPath)\mcw\**\*.cs" KeepDuplicates="False" />

src/Mono.Android/metadata

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,11 @@
13871387
everything optional after API Level 26, but we cannot follow that yet.-->
13881388
<remove-node path="/api/package/interface/implements[@name='java.lang.AutoCloseable']" />
13891389

1390-
1391-
<attr path="/api/package[@name='android.media']/interface[@name='MediaCas.EventListener']" name="argsType">MediaCasEventArgs</attr>
1390+
<!-- When there's only one method on the Interface (pre-API-30), this attribute goes on the Interface.
1391+
When there's more than one method on the Interface (API-30+), the attribute needs to go on the Method instead. -->
1392+
<attr api-until="30" path="/api/package[@name='android.media']/interface[@name='MediaCas.EventListener']" name="argsType">MediaCasEventArgs</attr>
1393+
<attr api-since="30" path="/api/package[@name='android.media']/interface[@name='MediaCas.EventListener']/method[@name='onEvent']" name="argsType">MediaCasEventArgs</attr>
1394+
13921395
<attr path="/api/package[@name='android.media']/interface[@name='MediaCas.EventListener']/method[@name='onEvent']/parameter[@name='MediaCas']" name="managedName">mediaCas</attr>
13931396

13941397
<!-- Until API Level 26, android.graphics.Color was static. In API Level 26,
@@ -1551,4 +1554,10 @@
15511554
<remove-node api-since="30" path="/api/package[@name='java.util']/interface[@jni-signature='Ljava/util/Map;']/method[@name='of' and count(parameter)=16]" />
15521555
<remove-node api-since="30" path="/api/package[@name='java.util']/interface[@jni-signature='Ljava/util/Map;']/method[@name='of' and count(parameter)=18]" />
15531556
<remove-node api-since="30" path="/api/package[@name='java.util']/interface[@jni-signature='Ljava/util/Map;']/method[@name='of' and count(parameter)=20]" />
1557+
1558+
<!-- Work around a generator bug where having multiple Listener methods with the same name cause multiple conflicting EventArgs classes to be created. -->
1559+
<!-- These are "default" method versions, so it's ok to remove them from an interface. -->
1560+
<remove-node api-since="30" path="/api/package[@name='android.animation']/interface[@name='Animator.AnimatorListener']/method[@name='onAnimationStart' and count(parameter)=2]" />
1561+
<remove-node api-since="30" path="/api/package[@name='android.animation']/interface[@name='Animator.AnimatorListener']/method[@name='onAnimationEnd' and count(parameter)=2]" />
1562+
15541563
</metadata>

0 commit comments

Comments
 (0)