[TrimmableTypeMap] Fix [Export] JNI signature mapping for non-primitive parameter types#11051
[TrimmableTypeMap] Fix [Export] JNI signature mapping for non-primitive parameter types#11051
Conversation
|
@copilot can you continue what you were doing? |
ManagedTypeToJniDescriptor now resolves Java-bound types via their [Register] attribute using TryResolveJniObjectDescriptor, falling back to Ljava/lang/Object; only for types that cannot be resolved. Methods that call it are converted from static to instance to access the assembly cache for type resolution. Agent-Logs-Url: https://github.com/dotnet/android/sessions/78609547-4f51-479a-aa5a-03a5bb54a0b8 Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Done in 768a003. The fix converts |
There was a problem hiding this comment.
Pull request overview
Fixes incorrect JNI signature generation for [Export] (and related [ExportField]) methods when parameters/return types are non-primitive Java-bound managed types, by resolving descriptors via [Register] metadata instead of always falling back to Ljava/lang/Object;. This aligns [Export] signature computation with the existing constructor-signature resolution path.
Changes:
- Update
JavaPeerScanner.ManagedTypeToJniDescriptor()to resolve Java-bound types viaTryResolveJniObjectDescriptor()before falling back toLjava/lang/Object;. - Convert several helper methods from
staticto instance methods to access the scanner’s assembly cache for type resolution. - Add/adjust unit tests to validate correct JNI signatures for
[Export]methods with Java-bound parameter types and for[ExportField]Java type names.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestFixtures/TestTypes.cs | Adds a new fixture type with [Export] methods taking Android.Views.View parameters to exercise Java-bound parameter signature resolution. |
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Scanner/JavaPeerScannerTests.Behavior.cs | Adds a theory verifying resolved JNI signatures for the new [Export] fixture methods. |
| tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/ExportFieldTests.cs | Updates expectation so [ExportField] return type resolves to the actual Java type instead of java.lang.Object. |
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.cs | Implements Java-bound type resolution in [Export] signature mapping and refactors helpers to instance methods to use the assembly cache. |
Problem
The scanner's
ManagedTypeToJniDescriptor()method maps managed types to JNI descriptors for[Export]method signatures. It correctly handles primitives (bool→Z,int→I, etc.) andstring→Ljava/lang/String;, but falls back toLjava/lang/Object;for all other types. This means[Export]methods with parameters likeAndroid.Graphics.Bitmap,Android.Views.View, or any custom Java-bound type get incorrect JNI signatures in the generated JCW, causingNoSuchMethodErrorat runtime.Fix
ManagedTypeToJniDescriptor()now resolves Java-bound types by looking up their[Register]attribute via the existingTryResolveJniObjectDescriptor()infrastructure (already used by the constructor signature path), falling back toLjava/lang/Object;only for types that truly cannot be resolved.Changes Made
ManagedTypeToJniDescriptor— AddedTryResolveJniObjectDescriptorcall before theLjava/lang/Object;fallback; converted fromstaticto instance to access the assembly cache.BuildJniSignatureFromManaged,ParseExportAttribute,ParseExportFieldAsMethod,TryGetMethodRegisterInfo,CollectExportField— Converted fromstaticto instance (transitively need the assembly cache for type resolution).ExportWithJavaBoundParamstest type — Added with three[Export]methods takingAndroid.Views.Viewparameters.Scan_ExportMethod_ResolvesJavaBoundParameterTypestest theory — Verifies correct JNI signatures for[Export]methods with non-primitive Java-bound parameter types.ExportFieldTests— Updated assertion to expect the correctly resolved type name (my.app.ExportFieldExample) instead of the previous incorrectjava.lang.Objectfallback.Testing