dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

Crash deploying net6.0-android app with android-arm and android-x64 RIDs #7654

Open gkarabin opened 1 year ago

gkarabin commented 1 year ago

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

VS 2022 17.4.3

Description

The .net 6 template app will crash if you modify it to limit RID support to android-arm and android-x64 when deploying to an android-arm64 device. I have an app that has 32-bit ARM .so files that I reference, but does not have 64-bit versions of the same files for ARM. I do have 32-bit and 64-bit versions for Intel.

For legacy Xamarin Android I could specify <AndroidSupportedAbis>armeabi-v7a;x86_64</AndroidSupportedAbis>. For net6.0, specifying <RuntimeIdentifiers>android-arm;android-x64</RuntimeIdentifiers> results in an app that crashes with:

12-23 14:48:24.156 24884 24884 F mono-rt : [ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Arithmetic operation resulted in an overflow.
12-23 14:48:24.156 24884 24884 F mono-rt :    at System.Runtime.InteropServices.Marshal.ReadIntPtr(IntPtr ptr, Int32 ofs)
12-23 14:48:24.156 24884 24884 F mono-rt :    at System.Runtime.InteropServices.Marshal.ReadIntPtr(IntPtr ptr)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Java.Interop.JniRuntime.CreateInvoker(IntPtr handle)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Java.Interop.JniRuntime..ctor(CreationOptions options)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Android.Runtime.AndroidRuntime..ctor(IntPtr jnienv, IntPtr vm, Boolean allocNewObjectSupported, IntPtr classLoader, IntPtr classLoader_loadClass, Boolean jniAddNativeMethodRegistrationAttributePresent)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Android.Runtime.JNIEnv.Initialize(JnienvInitializeArgs* args)

Steps to Reproduce

From a command prompt with VS 2022 17.4.3:

  1. dotnet new android
  2. Modify .csproj to target net6.0-android (rather than net7.0-android).
  3. Modify .csproj to contain <RuntimeIdentifiers>android-arm;android-x64</RuntimeIdentifiers>
  4. dotnet build -t:Run -f net6.0-android

I expect the app to launch and display "Hello Android!". Instead, the app launches to a white screen with no text.

Did you find any workaround?

Uninstalling the app from the Android device, then modifying the app to target .net7.0-android works as expected when building and running for net7.0-android.

It is a costly workaround - my app requires quite a bit of work to update to .net7.0 due to Android SDK requirement changes for MAUI.

Relevant log output

12-23 14:48:24.156 24884 24884 F mono-rt : [ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Arithmetic operation resulted in an overflow.
12-23 14:48:24.156 24884 24884 F mono-rt :    at System.Runtime.InteropServices.Marshal.ReadIntPtr(IntPtr ptr, Int32 ofs)
12-23 14:48:24.156 24884 24884 F mono-rt :    at System.Runtime.InteropServices.Marshal.ReadIntPtr(IntPtr ptr)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Java.Interop.JniRuntime.CreateInvoker(IntPtr handle)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Java.Interop.JniRuntime..ctor(CreationOptions options)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Android.Runtime.AndroidRuntime..ctor(IntPtr jnienv, IntPtr vm, Boolean allocNewObjectSupported, IntPtr classLoader, IntPtr classLoader_loadClass, Boolean jniAddNativeMethodRegistrationAttributePresent)
12-23 14:48:24.156 24884 24884 F mono-rt :    at Android.Runtime.JNIEnv.Initialize(JnienvInitializeArgs* args)
gkarabin commented 1 year ago

See a discord discussion here, if you are so inclined: https://discord.com/channels/732297728826277939/732297837953679412/1055902191015165992

gkarabin commented 1 year ago

Here is the binlog generated from dotnet build -t:Run -f net6.0-android -bl -p:_FastDeploymentDiagnosticLogging=true msbuild.binlog.zip