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

Crash on startup for default MAUI Blazor app on Android release mode #4262

Closed
SteveSandersonMS opened this issue Jan 21, 2022 · 6 comments · Fixed by dotnet/android#6675
Closed
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) platform/android 🤖 t/bug Something isn't working

Comments

@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Jan 21, 2022

Description

A default MAUI Blazor application built in Release mode crashes immediately on startup in Android. I've checked this in the Android emulator, and others have reported it for physical devices. The problem doesn't occur in debug builds.

Using adb logcat I see:

01-21 15:58:46.644  7783  7837 W nyname.mauiapp: Attempt to remove index outside index area (9 vs 10-10)
01-21 15:58:46.644  7783  7837 W nyname.mauiapp: JNI WARNING: DeleteLocalRef(0x91) failed to find entry
01-21 15:58:46.701  7867  7867 F crashpad: dlopen: dlopen failed: library "libandroidicu.so" not found: needed by /system/lib64/libharfbuzz_ng.so in namespace (default)
01-21 15:58:46.702  7783  7837 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x4 in tid 7837 (ThreadPoolForeg), pid 7783 (nyname.mauiapp2)
01-21 15:58:46.770  7870  7870 I crash_dump64: obtaining output fd from tombstoned, type: kDebuggerdTombstone
01-21 15:58:46.771   284   284 I tombstoned: received crash request for pid 7837
01-21 15:58:46.772  7870  7870 I crash_dump64: performing dump of process 7783 (target tid = 7837)
01-21 15:58:46.777  7870  7870 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
01-21 15:58:46.777  7870  7870 F DEBUG   : Build fingerprint: 'google/sdk_gphone_x86_64/generic_x86_64_arm64:11/RSR1.201211.001/7027799:user/release-keys'
01-21 15:58:46.777  7870  7870 F DEBUG   : Revision: '0'
01-21 15:58:46.777  7870  7870 F DEBUG   : ABI: 'x86_64'
01-21 15:58:46.778  7870  7870 F DEBUG   : Timestamp: 2022-01-21 15:58:46+0000
01-21 15:58:46.778  7870  7870 F DEBUG   : pid: 7783, tid: 7837, name: ThreadPoolForeg  >>> com.companyname.mauiapp2 <<<
01-21 15:58:46.778  7870  7870 F DEBUG   : uid: 10155
01-21 15:58:46.778  7870  7870 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x4
01-21 15:58:46.778  7870  7870 F DEBUG   : Cause: null pointer dereference
01-21 15:58:46.778  7870  7870 F DEBUG   :     rax 000070fc876bf820  rbx 000070fc876bfdc0  rcx 0000000000000000  rdx 000070fc876bf820
01-21 15:58:46.778  7870  7870 F DEBUG   :     r8  fffffffffffffdd0  r9  000070fc876bfe40  r10 0000000000000000  r11 000070fc8c34bb30
01-21 15:58:46.779  7870  7870 F DEBUG   :     r12 000070fc876bfbe0  r13 000070fce442b550  r14 000070fe54b03098  r15 0000000000000000
01-21 15:58:46.779  7870  7870 F DEBUG   :     rdi 000070fc876bfdc0  rsi 0000000000000000
01-21 15:58:46.779  7870  7870 F DEBUG   :     rbp 0000000000000000  rsp 000070fc876bfba0  rip 000070fce49048fc
01-21 15:58:46.838  7870  7870 F DEBUG   : backtrace:
01-21 15:58:46.838  7870  7870 F DEBUG   :       #00 pc 000000000065d8fc  /apex/com.android.art/lib64/libart.so (void art::StackVisitor::WalkStack<(art::StackVisitor::CountTransitions)0>(bool)+156) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
01-21 15:58:46.838  7870  7870 F DEBUG   :       #01 pc 000000000069b25d  /apex/com.android.art/lib64/libart.so (art::Thread::GetCurrentMethod(unsigned int*, bool, bool) const+157) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
01-21 15:58:46.838  7870  7870 F DEBUG   :       #02 pc 0000000000430fed  /apex/com.android.art/lib64/libart.so (art::JNI<false>::FindClass(_JNIEnv*, char const*)+765) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
01-21 15:58:46.838  7870  7870 F DEBUG   :       #03 pc 0000000000047e5a  /data/app/~~0Qm6D1S0sO3f1lwfakN0PA==/com.companyname.mauiapp2-08UokVCH5k_PlbZEH_hhkA==/split_config.x86_64.apk!libmono-android.release.so (offset 0x11e000) (java_interop_jnienv_find_class+26) (BuildId: 3d04f8b946590175e97b89aee2e3b19ceed4b524)
01-21 15:58:46.838  7870  7870 F DEBUG   :       #04 pc 00000000000128ac  <anonymous:41640000>
01-21 15:58:47.095   284   284 E tombstoned: Tombstone written to: /data/tombstones/tombstone_03

Is there someone working MAUI core that could help us make sense of this? I'm on the Blazor team and am not sure how we can proceed with understanding the error and figuring out whether it's something in BlazorWebView or is due to an issue in MAUI itself. If the fault is (as logged above) library "libandroidicu.so" not found then it seems like something controlled by MAUI core rather than BlazorWebView.

If it is a fault in BlazorWebView, can you give any suggestions on how we'd find out even which part of our code is running to trigger this?

Note: this probably duplicates #3816 and #2246 and #3060, but I'm filing this new issue to try to consolidate what we know in one place.

Steps to Reproduce

  1. Create a new MAUI Blazor application
  2. Change build configuration to Release
  3. Change target device to Android emulator (I'm using Pixel 5 - API 30)
  4. Ctrl+F5 (or just F5) to launch

Version with bug

Preview 12 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

API 30, probably others too

Did you find any workaround?

No. However this issue seems similar to #2246 and #3060, which people said they could work around by adding <AndroidLinkMode>None</AndroidLinkMode>.

However that doesn't seem to work when I try it (it leads to a build error), and in any case, other people were reporting the logged error as java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" which seems to be a long-running Xamarin issue and is not the same as what I saw in the adb logcat output. So it's possible those other issues are unrelated.

Relevant log output

No response

@SteveSandersonMS
Copy link
Member Author

@Eilon Since this has been triaged as area/blazor it's just come back to us. Can you recommend someone who works on the core Android support who might be able to give us some help with tracing into the internals here?

@Eilon Eilon added area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) and removed area-blazor Blazor Hybrid / Desktop, BlazorWebView labels Jan 25, 2022
@Eilon
Copy link
Member

Eilon commented Jan 25, 2022

@jonathanpeppers is who I'd ping.

But I updated the area on this to publishing because I think it's a bit closer to that area. The people applying the labels don't necessarily look at the history of an issue so that's probably why it would otherwise keep ending up tagged as Blazor.

@SteveSandersonMS
Copy link
Member Author

Thanks! It looks like @jonathanpeppers is already investigating at #2246 (comment), so I'll wait for his investigation there.

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Jan 26, 2022
Context: dotnet/maui#4262

`dotnet new maui-blazor` crashes at runtime, if you do:

    dotnet build -t:Run -c Release

You can turn *off* the linker to solve this issue:

    dotnet build -t:Run -c Release -p:AndroidLinkMode=None

(or `PublishTrimmed=false`)

The app crashes in way that you get a native crash:

    backtrace:
    #00 pc 000000000065d8fc  /apex/com.android.art/lib64/libart.so (void art::StackVisitor::WalkStack<(art::StackVisitor::CountTransitions)0>(bool)+156) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
    #1 pc 000000000069b25d  /apex/com.android.art/lib64/libart.so (art::Thread::GetCurrentMethod(unsigned int*, bool, bool) const+157) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
    #2 pc 0000000000430fed  /apex/com.android.art/lib64/libart.so (art::JNI<false>::FindClass(_JNIEnv*, char const*)+765) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
    #3 pc 0000000000047e5a  /data/app/~~0Qm6D1S0sO3f1lwfakN0PA==/com.companyname.mauiapp2-08UokVCH5k_PlbZEH_hhkA==/split_config.x86_64.apk!libmono-android.release.so (offset 0x11e000) (java_interop_jnienv_find_class+26) (BuildId: 3d04f8b946590175e97b89aee2e3b19ceed4b524)
    #4 pc 00000000000128ac  <anonymous:41640000>

After much investigation... We found that
`Java.IO.InputStreamHandler.GetReadHandler()` was linked away.

This is because:

1. `Mono.Android.dll` has it's Java stubs precompiled into
   `mono.android.jar` and `mono.android.dex`.

2. Since `Mono.Android.dll` doesn't go through `GenerateJavaStubs`,
   the linker has no way to know if the missing method is used -- it
   is only called from Java in the crashing app.

To make the crash better, we need to `try-catch` *all* exceptions in
`RegisterNativeMembers()` and pass them to
`AndroidRuntime.RaisePendingException()`. This is because
`RegisterNativeMembers()` is called directly from Java.

Now the crash shows:

    01-26 12:20:20.290 29264 29264 I MonoDroid: Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
    01-26 12:20:20.290 29264 29264 I MonoDroid:
    01-26 12:20:20.290 29264 29264 I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    01-26 12:20:20.290 29264 29264 I MonoDroid: android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    01-26 12:20:20.290 29264 29264 I MonoDroid: --- End of stack trace from previous location ---
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.FindClass(String )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.AllocObject(String )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
    01-26 12:20:20.290 29264 29264 I MonoDroid:
    01-26 12:20:20.290 29264 29264 I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    01-26 12:20:20.290 29264 29264 I MonoDroid: android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    01-26 12:20:20.290 29264 29264 I MonoDroid: --- End of stack trace from previous location ---
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.FindClass(String )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.AllocObject(String )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    01-26 12:20:20.290 29264 29264 I MonoDroid:    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    01-26 12:20:20.290 29264 29264 I MonoDroid: 	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

This is much easier to reason about, and will save us time in the future.

A fix to the actual linker problem will be coming in another PR.
@jonathanpeppers
Copy link
Member

This appears to be a linker issue Java.IO.InputStreamHandler.GetReadHandler() is removed -- I've first fixed this case where we are actually losing the managed exception, and getting a crash instead:

dotnet/android#6672

Will look into actually fixing the problem next.

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Jan 26, 2022
…asses

Fixes: dotnet/maui#2246
Fixes: dotnet/maui#4262
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1447865

`dotnet new maui-blazor` crashes in Release mode with:

    Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
    --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    at System.Delegate.CreateDelegate(Type , Type , String )
    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    --- End of stack trace from previous location ---
    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    at Android.Runtime.JNIEnv.FindClass(String )
    at Android.Runtime.JNIEnv.AllocObject(String )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
    --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    at System.Delegate.CreateDelegate(Type , Type , String )
    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    --- End of stack trace from previous location ---
    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    at Android.Runtime.JNIEnv.FindClass(String )
    at Android.Runtime.JNIEnv.AllocObject(String )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

It turns out that `Java.IO.InputStream.GetReadHandler()` was linked away.

So we need to preserve:

    <linker>
        <assembly fullname="Mono.Android">
            <type fullname="Android.Runtime.InputStreamInvoker" preserve="methods" />

Long-term, we should consider reworking `mono.android.jar`, so that
the linker *knows* about any C# methods called from Java from this
library.

For now, let's preserve a few additional types that could fall into
the same issue:

    <type fullname="Android.Runtime.InputStreamAdapter" preserve="methods" />
    <type fullname="Android.Runtime.InputStreamInvoker" preserve="methods" />
    <type fullname="Android.Runtime.XmlResourceParserReader" preserve="methods" />
    <type fullname="Android.Runtime.XmlPullParserReader" preserve="methods" />
    <type fullname="Android.Runtime.XmlReaderResourceParser" preserve="methods" />
    <type fullname="Android.Runtime.XmlReaderPullParser" preserve="methods" />

This should hopefully fix issues like this, and hopefully not regress
app size that much.
@jonathanpeppers
Copy link
Member

We should hopefully have this fixed in the next MAUI preview.

The most precise workaround is to create a ILLink.Descriptors.xml file:

<linker>
    <assembly fullname="Mono.Android">
        <type fullname="Android.Runtime.InputStreamAdapter" preserve="methods" />
        <type fullname="Android.Runtime.InputStreamInvoker" preserve="methods" />
    </assembly>
</linker>

Put this in your .csproj as well:

<EmbeddedResource Include="ILLink.Descriptors.xml">
  <LogicalName>ILLink.Descriptors.xml</LogicalName>
</EmbeddedResource>

Details about the file format here.

@SteveSandersonMS
Copy link
Member Author

Excellent - thanks @jonathanpeppers!

jonpryor pushed a commit to dotnet/android that referenced this issue Jan 27, 2022
Context: dotnet/maui#4262
Context: #6675

If you run the `maui-blazor` template in a Release build:

	dotnet build -t:Run -c Release

it crashes at runtime:

	D monodroid-assembly: typemap: type with token 33555274 (0x200034a) in module {C7B4CC8F-7A03-4A3F-A34A-DC66EDC548B9} (Mono.Android) corresponds to Java type 'android/runtime/JavaProxyThrowable'
	…
	F DEBUG   : backtrace:
	F DEBUG   : #00 pc 000000000065d8fc  /apex/com.android.art/lib64/libart.so (void art::StackVisitor::WalkStack<(art::StackVisitor::CountTransitions)0>(bool)+156) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
	F DEBUG   : #1 pc 000000000069b25d  /apex/com.android.art/lib64/libart.so (art::Thread::GetCurrentMethod(unsigned int*, bool, bool) const+157) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
	F DEBUG   : #2 pc 0000000000430fed  /apex/com.android.art/lib64/libart.so (art::JNI<false>::FindClass(_JNIEnv*, char const*)+765) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
	F DEBUG   : #3 pc 0000000000047e5a  /data/app/~~0Qm6D1S0sO3f1lwfakN0PA==/com.companyname.mauiapp2-08UokVCH5k_PlbZEH_hhkA==/split_config.x86_64.apk!libmono-android.release.so (offset 0x11e000) (java_interop_jnienv_find_class+26) (BuildId: 3d04f8b946590175e97b89aee2e3b19ceed4b524)
	F DEBUG   : #4 pc 00000000000128ac  <anonymous:41640000>

The crash can be avoided by disabling the linker:

	dotnet build -t:Run -c Release -p:AndroidLinkMode=None
	# -or-
	dotnet build -t:Run -c Release -p:PublishTrimmed=false

However, let us return to the crash: *why* is it crashing?
This isn't a "good debugging experience"; we have no useful context.

Lots of investigation later -- all hail printf debugging -- and we
found that the cause of the crash was an unhandled exception:

 1. `Mono.Android.dll` has it's Java Callable Wrappers generated
    from the *unlinked* assembly, into `mono.android.jar` and
    `mono.android.dex` files.  The Java Callable Wrapper for
    `Android.Runtime.InputStreamAdapter` thus includes *all*
    `Read()` method overloads.

 2. When the app is built in Release configuration, linking is
    enabled, and *some* of the `InputStreamAdapter.Read()` methods
    are removed by the linker, along with the
    `Java.IO.InputStream.Read()` methods that were overridden.

 3. At runtime, we perform [Java Type Registration][0] for the
    `Android.Runtime.InputStreamAdapter` type, which eventually calls
    `AndroidTypeManager.RegisterNativeMembers()`, which eventually
    attempts to *effectively* do:

        Delegate.CreateDelegate (
	        typeof(Func<Delegate>),
	        typeof(InputStreamAdapter),
	        "GetReadHandler");

 4. Because of (2), `Java.IO.InputStream.GetReadHandler()`
    *does not exist*, and thus `Delegate.CreateDelegate()` throws an
    `ArgumentException`.

So far, so reasonable, but…

 5. `AndroidTypeManager.RegisterNativeMembers()` didn't catch any
    exceptions, nor did any other method between the original Java
    `Runtime.register()` invocation and
    `AndroidTypeManager.RegisterNativeMembers()`.  The result is that
    a C# exception was "in flight", and Mono then proceeded to
    *tear down the stack frame* as it unwound the callstack looking
    for `catch` handlers.

At this point, the process is toast: the runtime stack is FUBAR.

This is also why the `backtrace:` is "rooted" in
`JNIEnv::FindClass()`: `JNIEnv::FindClass()` invokes Java static
constructors before returning, which is how the static constructor in
the Java Callable Wrapper for `InputStreamAdapter` called
`Runtime.register()` in the first place.

All of this makes for a miserable debugging experience.

Fixing the "original" linker issue will be done in
#6675.

This hasn't been an issue in "Classic" Xamarin.Android, presumably
because the classic linker isn't as good as the net6 linker.

What we want to do *here* is improve this debugging experience, by
"wrapping" `AndroidTypeManager.RegisterNativeMembers()` in a
`try`/`catch` block, which can then *marshal the thrown exception*
back to Java.  This *prevents* Mono from unwinding the callstack past
a JNI boundary, and avoids the annoying-to-debug app crash.

After this change, we get a much friendlier unhandled exception crash:

	I MonoDroid: Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
	I MonoDroid:
	I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
	I MonoDroid: android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String )
	I MonoDroid:    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
	I MonoDroid: --- End of stack trace from previous location ---
	I MonoDroid:    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
	I MonoDroid:    at Android.Runtime.JNIEnv.FindClass(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.AllocObject(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter..ctor(Stream )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
	I MonoDroid:    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
	I MonoDroid:    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
	I MonoDroid:    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
	I MonoDroid: 	at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
	I MonoDroid: 	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
	I MonoDroid:
	I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
	I MonoDroid: android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String )
	I MonoDroid:    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
	I MonoDroid: --- End of stack trace from previous location ---
	I MonoDroid:    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
	I MonoDroid:    at Android.Runtime.JNIEnv.FindClass(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.AllocObject(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter..ctor(Stream )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
	I MonoDroid:    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
	I MonoDroid:    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
	I MonoDroid:    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
	I MonoDroid: 	at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
	I MonoDroid: 	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

This is much easier to reason about, and will save us time in
the future.

[0]: https://github.com/xamarin/xamarin-android/wiki/Blueprint#java-type-registration
jonathanpeppers added a commit to dotnet/android that referenced this issue Jan 27, 2022
Context: dotnet/maui#4262
Context: #6675

If you run the `maui-blazor` template in a Release build:

	dotnet build -t:Run -c Release

it crashes at runtime:

	D monodroid-assembly: typemap: type with token 33555274 (0x200034a) in module {C7B4CC8F-7A03-4A3F-A34A-DC66EDC548B9} (Mono.Android) corresponds to Java type 'android/runtime/JavaProxyThrowable'
	…
	F DEBUG   : backtrace:
	F DEBUG   : #00 pc 000000000065d8fc  /apex/com.android.art/lib64/libart.so (void art::StackVisitor::WalkStack<(art::StackVisitor::CountTransitions)0>(bool)+156) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
	F DEBUG   : #1 pc 000000000069b25d  /apex/com.android.art/lib64/libart.so (art::Thread::GetCurrentMethod(unsigned int*, bool, bool) const+157) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
	F DEBUG   : #2 pc 0000000000430fed  /apex/com.android.art/lib64/libart.so (art::JNI<false>::FindClass(_JNIEnv*, char const*)+765) (BuildId: 7fbaf2a1a3317bd634b00eb90e32291e)
	F DEBUG   : #3 pc 0000000000047e5a  /data/app/~~0Qm6D1S0sO3f1lwfakN0PA==/com.companyname.mauiapp2-08UokVCH5k_PlbZEH_hhkA==/split_config.x86_64.apk!libmono-android.release.so (offset 0x11e000) (java_interop_jnienv_find_class+26) (BuildId: 3d04f8b946590175e97b89aee2e3b19ceed4b524)
	F DEBUG   : #4 pc 00000000000128ac  <anonymous:41640000>

The crash can be avoided by disabling the linker:

	dotnet build -t:Run -c Release -p:AndroidLinkMode=None
	# -or-
	dotnet build -t:Run -c Release -p:PublishTrimmed=false

However, let us return to the crash: *why* is it crashing?
This isn't a "good debugging experience"; we have no useful context.

Lots of investigation later -- all hail printf debugging -- and we
found that the cause of the crash was an unhandled exception:

 1. `Mono.Android.dll` has it's Java Callable Wrappers generated
    from the *unlinked* assembly, into `mono.android.jar` and
    `mono.android.dex` files.  The Java Callable Wrapper for
    `Android.Runtime.InputStreamAdapter` thus includes *all*
    `Read()` method overloads.

 2. When the app is built in Release configuration, linking is
    enabled, and *some* of the `InputStreamAdapter.Read()` methods
    are removed by the linker, along with the
    `Java.IO.InputStream.Read()` methods that were overridden.

 3. At runtime, we perform [Java Type Registration][0] for the
    `Android.Runtime.InputStreamAdapter` type, which eventually calls
    `AndroidTypeManager.RegisterNativeMembers()`, which eventually
    attempts to *effectively* do:

        Delegate.CreateDelegate (
	        typeof(Func<Delegate>),
	        typeof(InputStreamAdapter),
	        "GetReadHandler");

 4. Because of (2), `Java.IO.InputStream.GetReadHandler()`
    *does not exist*, and thus `Delegate.CreateDelegate()` throws an
    `ArgumentException`.

So far, so reasonable, but…

 5. `AndroidTypeManager.RegisterNativeMembers()` didn't catch any
    exceptions, nor did any other method between the original Java
    `Runtime.register()` invocation and
    `AndroidTypeManager.RegisterNativeMembers()`.  The result is that
    a C# exception was "in flight", and Mono then proceeded to
    *tear down the stack frame* as it unwound the callstack looking
    for `catch` handlers.

At this point, the process is toast: the runtime stack is FUBAR.

This is also why the `backtrace:` is "rooted" in
`JNIEnv::FindClass()`: `JNIEnv::FindClass()` invokes Java static
constructors before returning, which is how the static constructor in
the Java Callable Wrapper for `InputStreamAdapter` called
`Runtime.register()` in the first place.

All of this makes for a miserable debugging experience.

Fixing the "original" linker issue will be done in
#6675.

This hasn't been an issue in "Classic" Xamarin.Android, presumably
because the classic linker isn't as good as the net6 linker.

What we want to do *here* is improve this debugging experience, by
"wrapping" `AndroidTypeManager.RegisterNativeMembers()` in a
`try`/`catch` block, which can then *marshal the thrown exception*
back to Java.  This *prevents* Mono from unwinding the callstack past
a JNI boundary, and avoids the annoying-to-debug app crash.

After this change, we get a much friendlier unhandled exception crash:

	I MonoDroid: Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
	I MonoDroid:
	I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
	I MonoDroid: android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String )
	I MonoDroid:    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
	I MonoDroid: --- End of stack trace from previous location ---
	I MonoDroid:    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
	I MonoDroid:    at Android.Runtime.JNIEnv.FindClass(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.AllocObject(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter..ctor(Stream )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
	I MonoDroid:    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
	I MonoDroid:    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
	I MonoDroid:    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
	I MonoDroid: 	at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
	I MonoDroid: 	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
	I MonoDroid:
	I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
	I MonoDroid: android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
	I MonoDroid:    at System.Delegate.CreateDelegate(Type , Type , String )
	I MonoDroid:    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
	I MonoDroid: --- End of stack trace from previous location ---
	I MonoDroid:    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
	I MonoDroid:    at Android.Runtime.JNIEnv.FindClass(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.AllocObject(String )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
	I MonoDroid:    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter..ctor(Stream )
	I MonoDroid:    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
	I MonoDroid:    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
	I MonoDroid:    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
	I MonoDroid:    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
	I MonoDroid: 	at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
	I MonoDroid: 	at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
	I MonoDroid: 	at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

This is much easier to reason about, and will save us time in
the future.

[0]: https://github.com/xamarin/xamarin-android/wiki/Blueprint#java-type-registration
jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Jan 27, 2022
…asses

Fixes: dotnet/maui#2246
Fixes: dotnet/maui#4262
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1447865

`dotnet new maui-blazor` crashes in Release mode with:

    Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
    --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    at System.Delegate.CreateDelegate(Type , Type , String )
    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    --- End of stack trace from previous location ---
    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    at Android.Runtime.JNIEnv.FindClass(String )
    at Android.Runtime.JNIEnv.AllocObject(String )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
    --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    at System.Delegate.CreateDelegate(Type , Type , String )
    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    --- End of stack trace from previous location ---
    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    at Android.Runtime.JNIEnv.FindClass(String )
    at Android.Runtime.JNIEnv.AllocObject(String )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

It turns out that `Java.IO.InputStream.GetReadHandler()` was linked away.

So we need to preserve:

    <linker>
        <assembly fullname="Mono.Android">
            <type fullname="Android.Runtime.InputStreamAdapter" preserve="methods" />
            <type fullname="Android.Runtime.InputStreamInvoker" preserve="methods" />

Long-term, we should consider reworking `mono.android.jar`, so that
the linker *knows* about any C# methods called from Java from this
library.

This regresses apk size a reasonable amount:

    --"PackageSize": 2680724
    ++"PackageSize": 2697108

Adding ~16kb.

I considered adding:

    <type fullname="Android.Runtime.XmlResourceParserReader" />
    <type fullname="Android.Runtime.XmlPullParserReader" />
    <type fullname="Android.Runtime.XmlReaderResourceParser" />
    <type fullname="Android.Runtime.XmlReaderPullParser" />

But this regressed app size by too much!

    1,141,062 Package size difference 42.57% (of 2,680,724)
jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Jan 27, 2022
…asses

Fixes: dotnet/maui#2246
Fixes: dotnet/maui#4262
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1447865

Revert "[One .NET] Do not preserve IO stream adapter/invoker (dotnet#5449)"

This reverts commit 051cad7.

`dotnet new maui-blazor` crashes in Release mode with:

    Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
    --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    at System.Delegate.CreateDelegate(Type , Type , String )
    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    --- End of stack trace from previous location ---
    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    at Android.Runtime.JNIEnv.FindClass(String )
    at Android.Runtime.JNIEnv.AllocObject(String )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
    --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
    android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
    at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
    at System.Delegate.CreateDelegate(Type , Type , String )
    at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
    --- End of stack trace from previous location ---
    at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
    at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
    at Android.Runtime.JNIEnv.FindClass(String )
    at Android.Runtime.JNIEnv.AllocObject(String )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
    at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
    at Android.Runtime.InputStreamAdapter..ctor(Stream )
    at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
    at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
    at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
    at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
    at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
    at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
    at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
    at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

It turns out that `Java.IO.InputStream.GetReadHandler()` was linked away.

So we need to preserve:

    <linker>
        <assembly fullname="Mono.Android">
            <type fullname="Android.Runtime.InputStreamAdapter" preserve="methods" />
            <type fullname="Android.Runtime.InputStreamInvoker" preserve="methods" />

Long-term, we should consider reworking `mono.android.jar`, so that
the linker *knows* about any C# methods called from Java from this
library.

This regresses apk size a reasonable amount:

    --"PackageSize": 2680724
    ++"PackageSize": 2697108

Adding ~16kb.

I considered adding:

    <type fullname="Android.Runtime.XmlResourceParserReader" />
    <type fullname="Android.Runtime.XmlPullParserReader" />
    <type fullname="Android.Runtime.XmlReaderResourceParser" />
    <type fullname="Android.Runtime.XmlReaderPullParser" />

But this regressed app size by too much!

    1,141,062 Package size difference 42.57% (of 2,680,724)
jonathanpeppers added a commit to dotnet/android that referenced this issue Jan 28, 2022
…asses (#6675)

Fixes: dotnet/maui#2246
Fixes: dotnet/maui#4262
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1447865

Context: b7a368a

Revert "[One .NET] Do not preserve IO stream adapter/invoker (#5449)"

This reverts commit 051cad7.

If you run the `maui-blazor` template in a Release build:

	dotnet build -t:Run -c Release

it crashes at runtime:

	Android.Runtime.JavaProxyThrowable: Exception_WasThrown, Android.Runtime.JavaProxyThrowable
	--- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
	android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
	  at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
	  at System.Delegate.CreateDelegate(Type , Type , String )
	  at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
	--- End of stack trace from previous location ---
	  at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
	  at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
	  at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
	  at Android.Runtime.JNIEnv.FindClass(String )
	  at Android.Runtime.JNIEnv.AllocObject(String )
	  at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
	  at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
	  at Android.Runtime.InputStreamAdapter..ctor(Stream )
	  at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
	  at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
	  at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
	  at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
	  at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
	  at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
	  at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
	  at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)
	--- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
	android.runtime.JavaProxyThrowable: System.ArgumentException: Arg_DlgtTargMeth
	  at System.Delegate.CreateDelegate(Type , Type , String , Boolean , Boolean )
	  at System.Delegate.CreateDelegate(Type , Type , String )
	  at Android.Runtime.AndroidTypeManager.RegisterNativeMembers(JniType , Type , String )
	--- End of stack trace from previous location ---
	  at Java.Interop.JniEnvironment.StaticMethods.CallStaticObjectMethod(JniObjectReference , JniMethodInfo , JniArgumentValue* )
	  at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue* )
	  at Android.Runtime.JNIEnv.CallStaticObjectMethod(IntPtr , IntPtr , JValue[] )
	  at Android.Runtime.JNIEnv.FindClass(String )
	  at Android.Runtime.JNIEnv.AllocObject(String )
	  at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue* )
	  at Android.Runtime.JNIEnv.StartCreateInstance(String , String , JValue[] )
	  at Android.Runtime.InputStreamAdapter..ctor(Stream )
	  at Android.Runtime.InputStreamAdapter.ToLocalJniHandle(Stream )
	  at Android.Webkit.WebResourceResponse..ctor(String , String , Int32 , String , IDictionary`2 , Stream )
	  at Microsoft.AspNetCore.Components.WebView.Maui.WebKitWebViewClient.ShouldInterceptRequest(WebView view, IWebResourceRequest request)
	  at Android.Webkit.WebViewClient.n_ShouldInterceptRequest_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_(IntPtr , IntPtr , IntPtr , IntPtr )
	  at crc64d693e2d9159537db.WebKitWebViewClient.n_shouldInterceptRequest(Native Method)
	  at crc64d693e2d9159537db.WebKitWebViewClient.shouldInterceptRequest(WebKitWebViewClient.java:39)
	  at Rr.a(chromium-TrichromeWebViewGoogle.apk-stable-410410686:16)
	  at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(chromium-TrichromeWebViewGoogle.apk-stable-410410686:2)

It turns out that `Java.IO.InputStream.GetReadHandler()` was
linked away.

So we need to preserve:

	<linker>
	    <assembly fullname="Mono.Android">
	        <type fullname="Android.Runtime.InputStreamAdapter" preserve="methods" />
	        <type fullname="Android.Runtime.InputStreamInvoker" preserve="methods" />

Long-term, we should consider reworking `mono.android.jar`, so that
the linker *knows* about any C# methods called from Java from this
library, or *drop* `mono.android.jar` & `mono.android.dex` and treat
`Mono.Android.dll` as a "normal user assembly"…

This regresses `.apk` size by ~16KB:

	--"PackageSize": 2680724
	++"PackageSize": 2697108

I considered adding:

	<type fullname="Android.Runtime.XmlResourceParserReader" />
	<type fullname="Android.Runtime.XmlPullParserReader" />
	<type fullname="Android.Runtime.XmlReaderResourceParser" />
	<type fullname="Android.Runtime.XmlReaderPullParser" />

But this regressed app size by too much!

	1,141,062 Package size difference 42.57% (of 2,680,724)
@ghost ghost locked as resolved and limited conversation to collaborators Feb 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-publishing Issues with the app packaging/publishing process (ipk/apk/msix/trimming) platform/android 🤖 t/bug Something isn't working
Projects
None yet
4 participants