Skip to content

Conversation

@jonathanpeppers
Copy link
Member

Context: NuGet/NuGet.Client#6239

.NET 10 Preview 1 enables $(RestoreEnablePackagePruning)=true by default.

This produces multiple warnings as errors in dotnet/android:

external\Java.Interop\tests\Java.Interop.Dynamic-Tests\Java.Interop.Dynamic-Tests.csproj :
error NU1510: Warning As Error: PackageReference Microsoft.CSharp will not be pruned. Consider removing this package from your dependencies, as it is likely unnecessary.

The Microsoft.CSharp package does seem like it can just be removed.

Context: NuGet/NuGet.Client#6239

.NET 10 Preview 1 enables `$(RestoreEnablePackagePruning)=true` by
default.

This produces multiple warnings as errors in dotnet/android:

    external\Java.Interop\tests\Java.Interop.Dynamic-Tests\Java.Interop.Dynamic-Tests.csproj :
    error NU1510: Warning As Error: PackageReference Microsoft.CSharp will not be pruned. Consider removing this package from your dependencies, as it is likely unnecessary.

The `Microsoft.CSharp` package does seem like it can just be removed.
@jonathanpeppers jonathanpeppers merged commit 9369d4f into main Feb 11, 2025
4 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/peppers/nuget/prunes branch February 11, 2025 18:26
jonpryor pushed a commit to dotnet/android that referenced this pull request Feb 12, 2025
Changes: dotnet/java-interop@6bc87e8...57f7bc8

  * dotnet/java-interop@57f7bc84: [generator] Avoid non-blittable types in native callback methods (dotnet/java-interop#1296)
  * dotnet/java-interop@9369d4fd: [tests] fix `NU1510` warning as error (dotnet/java-interop#1306)

Context: dotnet/java-interop@57f7bc8
Context: dotnet/java-interop#1027

dotnet/java-interop@57f7bc84 updated
`generator --codegen-target=XAJavaInterop1` marshal methods to
contain only blittable types in parameter and return types.
Instead of `bool`, use `sbyte`; instead of `char`, use `ushort`.

This change causes warnings when `MarshalMethodsClassifier` verifies
that the marshal types match the native types:

	…/Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')
	…/Xamarin.Android.Common.targets(1502,3): Unable to find native callback method 'n_Equals_Ljava_lang_Object_' in type 'Java.Lang.Object', matching the 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' signature (jniName: 'equals') [Arch: X86_64; Assembly: obj/Release/android-x64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Method 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' will be registered dynamically [Arch: X86_64; Assembly: obj/Release/android-x64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')
	…/Xamarin.Android.Common.targets(1502,3): Unable to find native callback method 'n_Equals_Ljava_lang_Object_' in type 'Java.Lang.Object', matching the 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' signature (jniName: 'equals') [Arch: Arm64; Assembly: obj/Release/android-arm64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Method 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' will be registered dynamically [Arch: Arm64; Assembly: obj/Release/android-arm64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Type 'Android.Runtime.JavaObject, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' will register some of its Java override methods dynamically. This may adversely affect runtime performance. See preceding warnings for names of dynamically registered methods.
	…/Xamarin.Android.Common.targets(1502,3): [Arm64] Number of methods in the project that will be registered dynamically: 1
	…/Xamarin.Android.Common.targets(1502,3): [X86_64] Number of methods in the project that will be registered dynamically: 1

These warnings occur because the marshal method signature now differs
from the public API signature.  Consider:

	namespace Java.Lang;

	partial class Object {
	    static sbyte n_Equals_Ljava_lang_Object_ (IntPtr jnienv, IntPtr native__this, IntPtr native_obj) => …

	    [Register ("equals", "(Ljava/lang/Object;)Z", "GetEquals_Ljava_lang_Object_Handler")]
	    public virtual unsafe bool Equals (Java.Lang.Object? obj) => …
	}

`MarshalMethodsClassifier` uses the 3rd `connector` parameter to
`RegisterAttribute` to determine the name of the marshal method --
string processing `GetEquals_Ljava_lang_Object_Handler` to obtain
`n_Equals_Ljava_lang_Object_` -- and then checks that the parameter
and return types match between the two methods.

Now that we're emitting blittable types, they *don't* match, thus the
warning.

Fix this by treating our blittable types as equivalent to their
native types in `MarshalMethodsClassifier`.

Additionally, `Mono.Android` has "built-in delegates" that marshal
`bool` types, e.g. `_JniMarshal_PP_Z`.  Because `generator` no longer
creates these delegates -- it will now create `_JniMarshal_PP_B`
instead -- we need to add them manually.
jonathanpeppers pushed a commit to dotnet/android that referenced this pull request Feb 12, 2025
Changes: dotnet/java-interop@6bc87e8...57f7bc8

  * dotnet/java-interop@57f7bc84: [generator] Avoid non-blittable types in native callback methods (dotnet/java-interop#1296)
  * dotnet/java-interop@9369d4fd: [tests] fix `NU1510` warning as error (dotnet/java-interop#1306)

Context: dotnet/java-interop@57f7bc8
Context: dotnet/java-interop#1027

dotnet/java-interop@57f7bc84 updated
`generator --codegen-target=XAJavaInterop1` marshal methods to
contain only blittable types in parameter and return types.
Instead of `bool`, use `sbyte`; instead of `char`, use `ushort`.

This change causes warnings when `MarshalMethodsClassifier` verifies
that the marshal types match the native types:

	…/Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')
	…/Xamarin.Android.Common.targets(1502,3): Unable to find native callback method 'n_Equals_Ljava_lang_Object_' in type 'Java.Lang.Object', matching the 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' signature (jniName: 'equals') [Arch: X86_64; Assembly: obj/Release/android-x64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Method 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' will be registered dynamically [Arch: X86_64; Assembly: obj/Release/android-x64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')
	…/Xamarin.Android.Common.targets(1502,3): Unable to find native callback method 'n_Equals_Ljava_lang_Object_' in type 'Java.Lang.Object', matching the 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' signature (jniName: 'equals') [Arch: Arm64; Assembly: obj/Release/android-arm64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Method 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' will be registered dynamically [Arch: Arm64; Assembly: obj/Release/android-arm64/linked/Mono.Android.dll]
	…/Xamarin.Android.Common.targets(1502,3): Type 'Android.Runtime.JavaObject, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' will register some of its Java override methods dynamically. This may adversely affect runtime performance. See preceding warnings for names of dynamically registered methods.
	…/Xamarin.Android.Common.targets(1502,3): [Arm64] Number of methods in the project that will be registered dynamically: 1
	…/Xamarin.Android.Common.targets(1502,3): [X86_64] Number of methods in the project that will be registered dynamically: 1

These warnings occur because the marshal method signature now differs
from the public API signature.  Consider:

	namespace Java.Lang;

	partial class Object {
	    static sbyte n_Equals_Ljava_lang_Object_ (IntPtr jnienv, IntPtr native__this, IntPtr native_obj) => …

	    [Register ("equals", "(Ljava/lang/Object;)Z", "GetEquals_Ljava_lang_Object_Handler")]
	    public virtual unsafe bool Equals (Java.Lang.Object? obj) => …
	}

`MarshalMethodsClassifier` uses the 3rd `connector` parameter to
`RegisterAttribute` to determine the name of the marshal method --
string processing `GetEquals_Ljava_lang_Object_Handler` to obtain
`n_Equals_Ljava_lang_Object_` -- and then checks that the parameter
and return types match between the two methods.

Now that we're emitting blittable types, they *don't* match, thus the
warning.

Fix this by treating our blittable types as equivalent to their
native types in `MarshalMethodsClassifier`.

Additionally, `Mono.Android` has "built-in delegates" that marshal
`bool` types, e.g. `_JniMarshal_PP_Z`.  Because `generator` no longer
creates these delegates -- it will now create `_JniMarshal_PP_B`
instead -- we need to add them manually.
@github-actions github-actions bot locked and limited conversation to collaborators Mar 14, 2025
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.

3 participants