-
Notifications
You must be signed in to change notification settings - Fork 538
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
[Mono.Android] Java.Lang.Object.GetObject<T>() implementation #9728
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Context: #9630 Context: dotnet/java-interop#1295 As an alternative to #9630... For NativeAOT support, implement `Java.Lang.Object.GetObject()` using the new `JniRuntime.JniValueManager.GetPeer()` method. This also cleans up a few things if `Java.Lang.Object` introduces an internal `DynamicallyAccessedMemberTypes Constructors` field.
5a208fb
to
310dee1
Compare
Draft commit message: Context: https://github.com/dotnet/android/pull/9630
Context: https://github.com/dotnet/android/pull/9716
Context: https://github.com/dotnet/java-interop/commit/e288589d92afc9edd9070638ebed72f67e310117
Context:
@jonathanpeppers attempted to prototype MAUI startup in a NativeAOT
environment, which promptly crashes similar to:
E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.DllNotFoundException: DllNotFound_Linux, xa-internal-api,
E AndroidRuntime: dlopen failed: library "xa-internal-api" not found
E AndroidRuntime: dlopen failed: library "libxa-internal-api" not found
E AndroidRuntime:
E AndroidRuntime: at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x47
E AndroidRuntime: at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0xe2
E AndroidRuntime: at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
E AndroidRuntime: at Android.Runtime.RuntimeNativeMethods.monodroid_TypeManager_get_java_class_name(IntPtr) + 0x22
E AndroidRuntime: at Java.Interop.TypeManager.GetClassName(IntPtr) + 0xe
E AndroidRuntime: at Java.Interop.TypeManager.CreateInstance(IntPtr, JniHandleOwnership, Type) + 0x69
E AndroidRuntime: at Java.Lang.Object._GetObject[T](IntPtr, JniHandleOwnership) + 0x4e
E AndroidRuntime: at Android.App.Application.n_OnCreate(IntPtr jnienv, IntPtr native__this) + 0x89
E AndroidRuntime: at my.MainApplication.n_onCreate(Native Method)
E AndroidRuntime: at my.MainApplication.onCreate(MainApplication.java:24)
E AndroidRuntime: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1182)
E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6460)
E AndroidRuntime: at android.app.ActivityThread.access$1300(ActivityThread.java:219)
E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)
E AndroidRuntime: at android.os.Looper.loop(Looper.java:214)
E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7356)
E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
because:
1. MAUI apps provide an `Android.App.Application` sublass, and
2. [`Application` is "special"][0], and
3. When `Java.Lang.Object.GetObject<T>()` is hit for an instance
which doesn't already have an instance mapping -- i.e.
`Object.PeekObject()` returns `null` -- then we'd hit
`TypeManager.CreateInstance()`, which is a codepath full of
P/Invokes, and P/Invokes don't currently work on NativeAOT [^1].
The solution is to partially resuscitate PR #9630, but this time have
`Java.Lang.Object.GetObject<T>()` call
`Java.Interop.JniRuntime.JniValueManager.GetPeer()` instead of
`Java.Interop.JniRuntime.JniValueManager.GetValue()`; see also
dotnet/java-interop@e288589d.
This allows `Application` subclasses to be used (along with other
build system changes present in #9716).
This also cleans up a few things if `Java.Lang.Object` introduces an
internal `DynamicallyAccessedMemberTypes Constructors` field.
[0]: https://learn.microsoft.com/en-us/previous-versions/xamarin/android/internals/architecture#java-activation
[^1]: It's *not* that NativeAOT doesn't support P/Invokes; it does.
The problem is that for a P/Invoke to work, we need to know the
name of the native library we're P/Invoking into, and our
NativeAOT sample environment doesn't include any `.so` files
other than the one for the app, i.e. whatever we'd P/Invoke
into doesn't exist. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context: #9630
Context: dotnet/java-interop#1295
As an alternative to #9630...
For NativeAOT support, implement
Java.Lang.Object.GetObject()
using the newJniRuntime.JniValueManager.GetPeer()
method.This also cleans up a few things if
Java.Lang.Object
introduces an internalDynamicallyAccessedMemberTypes Constructors
field.