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.93k stars 530 forks source link

[.net9-preview][android] Multiple AppResources Crash - failed to load bundled assembly pt-PT/ #9200

Closed bcaceiro closed 1 month ago

bcaceiro commented 2 months ago

Description

When creating a new .net maui with VS preview and NET 9 Preview 7 with multiple AppResources the application crashes in Android on startup

D  Compat change id reported: 171228096; UID 10583; state: ENABLED
2024-08-14 07:46:51.033 19787-19787 TabLayout               com.companyname.testnet9             W  MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
2024-08-14 07:46:51.043 19787-19787 Compatibil...geReporter com.companyname.testnet9             D  Compat change id reported: 210923482; UID 10583; state: ENABLED
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  Assembly 'pt-PT/Testnet9.resources' (hash 0x1cab5580a98905b0) not found
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  open_from_bundles: failed to load bundled assembly pt-PT/Testnet9.resources
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  Assembly 'pt-PT/Testnet9.resources' (hash 0x1cab5580a98905b0) not found
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  open_from_bundles: failed to load bundled assembly pt-PT/Testnet9.resources
2024-08-14 07:46:51.057 19787-19787 monodroid-assembly      com.companyname.testnet9             A  Failed to look up image index for hash 0x7357acbda27bdba3
2024-08-14 07:46:51.057 19787-19787 monodroid               com.companyname.testnet9             A  Abort at /Users/runner/work/1/s/xamarin-android/src/native/monodroid/mono-image-loader.hh:120:5 ('static MonoImage *xamarin::android::internal::MonoImageLoader::stash_and_return(MonoImage *, MonoImageOpenStatus, hash_t)')
2024-08-14 07:46:51.058 19787-19787 libc                    com.companyname.testnet9             A  Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 19787 (nyname.testnet9), pid 19787 (nyname.testnet9)

Steps to Reproduce

1 - create new .net MAUI NET9 preview 7 2 - Add AppResources.resx 3 - Add AppResources.pt.resx 4 - Add Translation in a page - https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/localization?view=net-maui-8.0 4 - Run in RELEASE mode - crash

Link to public reproduction project repository

https://github.com/bcaceiro/maui9resources

Version with bug

9.0.0-preview.6.24327.7

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.80 SR8

Affected platforms

Android

Affected platform versions

33,34,35

Did you find any workaround?

Well, deleting the AppResources.pt fixes it, though loosing translations :)

Relevant log output

--------- beginning of main
--------- beginning of system
--------- beginning of crash
2024-08-14 07:46:51.031 19787-19787 Compatibil...geReporter com.companyname.testnet9             D  Compat change id reported: 171228096; UID 10583; state: ENABLED
2024-08-14 07:46:51.033 19787-19787 TabLayout               com.companyname.testnet9             W  MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
2024-08-14 07:46:51.043 19787-19787 Compatibil...geReporter com.companyname.testnet9             D  Compat change id reported: 210923482; UID 10583; state: ENABLED
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  Assembly 'pt-PT/Testnet9.resources' (hash 0x1cab5580a98905b0) not found
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  open_from_bundles: failed to load bundled assembly pt-PT/Testnet9.resources
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  Assembly 'pt-PT/Testnet9.resources' (hash 0x1cab5580a98905b0) not found
2024-08-14 07:46:51.054 19787-19787 monodroid-assembly      com.companyname.testnet9             W  open_from_bundles: failed to load bundled assembly pt-PT/Testnet9.resources
2024-08-14 07:46:51.057 19787-19787 monodroid-assembly      com.companyname.testnet9             A  Failed to look up image index for hash 0x7357acbda27bdba3
2024-08-14 07:46:51.057 19787-19787 monodroid               com.companyname.testnet9             A  Abort at /Users/runner/work/1/s/xamarin-android/src/native/monodroid/mono-image-loader.hh:120:5 ('static MonoImage *xamarin::android::internal::MonoImageLoader::stash_and_return(MonoImage *, MonoImageOpenStatus, hash_t)')
2024-08-14 07:46:51.058 19787-19787 libc                    com.companyname.testnet9             A  Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 19787 (nyname.testnet9), pid 19787 (nyname.testnet9)
github-actions[bot] commented 2 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

drasticactions commented 2 months ago

@bcaceiro If you try this in a .NET Android application that doesn't implement the MAUI UI framework (dotnet new android), does it also crash? This seems like it will be a runtime issue and not a MAUI UI one.

bcaceiro commented 2 months ago

@drasticactions no idea, tbf not quite familiar with AppResources in a dotnet android app

dellis1972 commented 2 months ago

I tried to repo and Preview 6 of .net 9 and I am unable to do so. I tested both pt and es languages on an emulator.

bcaceiro commented 2 months ago

@dellis1972 - It is on preview7 - the template doesn't have that option available.

I have a reproduction link in the issue. Tested on a real device though

dellis1972 commented 2 months ago

So I can't repo on device or emulator using preview 7. @grendello can you take a look when you get a moment?

bcaceiro commented 2 months ago

Changed the description of the reproduction steps - It only happens in RELEASE mode

tranb3r commented 2 months ago

Any update?

dellis1972 commented 2 months ago

@grendello when you get a moment can you take a look at this one?

grendello commented 2 months ago

@dellis1972 There's a chance c978f35e0d01df2d30e0d4a48e1f7c0156cd0e91 already fixed it, I'll try to take a look this week.

tranb3r commented 2 months ago

This is still happening in net9-rc1.

grendello commented 1 month ago

I've just tested it with the tip of the main branch, and it appears to work. Here's a screenshot of the repro app (system language set to Portuguese/Portugal) screen

jonpryor commented 1 month ago

@grendello: this appears to be due to some difference between Windows and non-Windows host platform builds (?!).

Repro steps:

  1. Download appropriate pre-built Binaries for .NET 9 RC1 from: https://dotnet.microsoft.com/en-us/download/dotnet/9.0

    Note: Binaries, not installers. (Installers may also work, but I used the binaries.)

  2. Extract the binaries "somewhere", then within the extracted directory run:

    ./dotnet workload install maui
  3. Override PATH to include the directory from (2)

    # Windows
    $env:PATH= "C:\Path\TO\\dotnet-sdk-9.0.100-rc.1.24452.12-win-x64;$env:PATH"
    # macOS/Linux
    export PATH=/path/to/dotnet-sdk-9.0.100-rc.1:$PATH
  4. Clone the repro:

    git clone https://github.com/bcaceiro/maui9resources.git
  5. Build, install, run the repro:

    dotnet build -f net9.0-android -c Release
    adb uninstall com.companyname.testnet9
    adb install bin/Release/net9.0-android/com.companyname.testnet9-Signed.apk
    adb shell am start com.companyname.testnet9/crc647c2c3e8dd60ba9cd.MainActivity

When I build from macOS, the .apk runs without crashing.

When I build from Windows, the .apk crashes, as originally described:

09-19 14:56:09.234  8622  8622 W monodroid-assembly: Assembly 'pt-PT/Testnet9.resources' (hash 0x1cab5580a98905b0) not found
09-19 14:56:09.234  8622  8622 W monodroid-assembly: open_from_bundles: failed to load bundled assembly pt-PT/Testnet9.resources
09-19 14:56:09.234  8622  8622 W monodroid-assembly: Assembly 'pt-PT/Testnet9.resources' (hash 0x1cab5580a98905b0) not found
09-19 14:56:09.234  8622  8622 W monodroid-assembly: open_from_bundles: failed to load bundled assembly pt-PT/Testnet9.resources
09-19 14:56:09.236  8622  8622 F monodroid-assembly: Failed to look up image index for hash 0x7357acbda27bdba3
09-19 14:56:09.236  8622  8622 F monodroid: Abort at /Users/runner/work/1/s/xamarin-android/src/native/monodroid/mono-image-loader.hh:120:5 ('static MonoImage *xamarin::android::internal::MonoImageLoader::stash_and_return(MonoImage *, MonoImageOpenStatus, hash_t)')
09-19 14:56:09.236  8622  8622 F libc    : Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 8622 (nyname.testnet9), pid 8622 (nyname.testnet9)
…
09-19 14:56:09.277  8655  8655 I crash_dump64: performing dump of process 8622 (target tid = 8622)
09-19 14:56:09.366  8655  8655 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
09-19 14:56:09.366  8655  8655 F DEBUG   : Build fingerprint: 'google/raven/raven:14/AP2A.240805.005.F1/12043167:user/release-keys'
09-19 14:56:09.366  8655  8655 F DEBUG   : Revision: 'MP1.0'
09-19 14:56:09.366  8655  8655 F DEBUG   : ABI: 'arm64'
09-19 14:56:09.366  8655  8655 F DEBUG   : Timestamp: 2024-09-19 14:56:09.282050201-0400
09-19 14:56:09.366  8655  8655 F DEBUG   : Process uptime: 1s
09-19 14:56:09.366  8655  8655 F DEBUG   : Cmdline: com.companyname.testnet9
09-19 14:56:09.366  8655  8655 F DEBUG   : pid: 8622, tid: 8622, name: nyname.testnet9  >>> com.companyname.testnet9 <<<
09-19 14:56:09.366  8655  8655 F DEBUG   : uid: 10432
09-19 14:56:09.366  8655  8655 F DEBUG   : tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
09-19 14:56:09.366  8655  8655 F DEBUG   : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
09-19 14:56:09.366  8655  8655 F DEBUG   : Abort message: 'Failed to look up image index for hash 0x7357acbda27bdba3'
09-19 14:56:09.366  8655  8655 F DEBUG   :     x0  0000000000000000  x1  00000000000021ae  x2  0000000000000006  x3  0000007fe644e310
09-19 14:56:09.366  8655  8655 F DEBUG   :     x4  2e722e302e6a716e  x5  2e722e302e6a716e  x6  2e722e302e6a716e  x7  7f7f7f7f7f7f7f7f
09-19 14:56:09.366  8655  8655 F DEBUG   :     x8  00000000000000f0  x9  0000007af562a350  x10 0000000000000001  x11 0000007af567b170
09-19 14:56:09.366  8655  8655 F DEBUG   :     x12 0000007fe644cc20  x13 00000000000000de  x14 0000007fe644de48  x15 0000003e2efa613a
09-19 14:56:09.366  8655  8655 F DEBUG   :     x16 0000007af56e1fd0  x17 0000007af56cd560  x18 0000007b1573c000  x19 00000000000021ae
09-19 14:56:09.366  8655  8655 F DEBUG   :     x20 00000000000021ae  x21 00000000ffffffff  x22 000000773e5ed5a8  x23 000000784cd6dc8c
09-19 14:56:09.366  8655  8655 F DEBUG   :     x24 000000773e5ed5c0  x25 0000007b14e0aac0  x26 0000007fe644e428  x27 0000007b14e0aac0
09-19 14:56:09.366  8655  8655 F DEBUG   :     x28 0000000000000001  x29 0000007fe644e390
09-19 14:56:09.366  8655  8655 F DEBUG   :     lr  0000007af56648b8  sp  0000007fe644e2f0  pc  0000007af56648e4  pst 0000000000001000
09-19 14:56:09.366  8655  8655 F DEBUG   : 8 total frames
09-19 14:56:09.366  8655  8655 F DEBUG   : backtrace:
09-19 14:56:09.366  8655  8655 F DEBUG   :       #00 pc 000000000005d8e4  /apex/com.android.runtime/lib64/bionic/libc.so (abort+164) (BuildId: 1d36f8ae6e0af6158793abea7d4f4f2b)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #01 pc 00000000000450bc  /data/app/~~UeL3IzhnnKWaWNC3QvJzLQ==/com.companyname.testnet9-JRe5kbYFfMvbdm6sggxl4Q==/lib/arm64/libmonodroid.so (xamarin::android::Helpers::abort_application(bool, std::__ndk1::source_location)+68) (BuildId: 2c6d565f407362f1538c0daa0faadd527d3f394d)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #02 pc 000000000001ffe4  /data/app/~~UeL3IzhnnKWaWNC3QvJzLQ==/com.companyname.testnet9-JRe5kbYFfMvbdm6sggxl4Q==/lib/arm64/libmonodroid.so (xamarin::android::internal::EmbeddedAssemblies::open_from_bundles(void*, _MonoAssemblyName*, char**, void*, _MonoError*)+5672) (BuildId: 2c6d565f407362f1538c0daa0faadd527d3f394d)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #03 pc 0000000000209de0  /data/app/~~UeL3IzhnnKWaWNC3QvJzLQ==/com.companyname.testnet9-JRe5kbYFfMvbdm6sggxl4Q==/lib/arm64/libmonosgen-2.0.so (BuildId: ed96d22c7f8696c1b93010ad15c389fcae5460db)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #04 pc 00000000002072cc  /data/app/~~UeL3IzhnnKWaWNC3QvJzLQ==/com.companyname.testnet9-JRe5kbYFfMvbdm6sggxl4Q==/lib/arm64/libmonosgen-2.0.so (mono_assembly_request_byname+972) (BuildId: ed96d22c7f8696c1b93010ad15c389fcae5460db)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #05 pc 0000000000204de4  /data/app/~~UeL3IzhnnKWaWNC3QvJzLQ==/com.companyname.testnet9-JRe5kbYFfMvbdm6sggxl4Q==/lib/arm64/libmonosgen-2.0.so (BuildId: ed96d22c7f8696c1b93010ad15c389fcae5460db)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #06 pc 0000000000236c8c  /data/app/~~UeL3IzhnnKWaWNC3QvJzLQ==/com.companyname.testnet9-JRe5kbYFfMvbdm6sggxl4Q==/lib/arm64/libmonosgen-2.0.so (BuildId: ed96d22c7f8696c1b93010ad15c389fcae5460db)
09-19 14:56:09.366  8655  8655 F DEBUG   :       #07 pc 0000000000007ee4  <anonymous:7b0493d000>
jonpryor commented 1 month ago

Further trying to narrow down how (why?!!!!) macOS and Windows builds would differ, I narrow down upon obj/Release/net9.0-android/android/marshal_methods.arm64-v8a.ll, which is "interesting"; from:

diff -ubi obj/Release/net9.0-android/android/marshal_methods.arm64-v8a.ll windows/marshal_methods.arm64-v8a.ll | grep -i 'pt.*testnet9'

Reordered so that things go together:

--- obj/Release/net9.0-android/android/marshal_methods.arm64-v8a.ll     2024-09-19 11:17:56
+++ windows/marshal_methods.arm64-v8a.ll        2024-09-19 15:07:55
@@ -21,225 +21,225 @@
-   i64 u0x0b3684b793a1337f, ; 19: pt/Testnet9.resources.dll => 0
+   i64 u0x0b3684b793a1337f, ; 16: pt/Testnet9.resources.dll => 0
-   i64 u0x4a77e791b1c18aa8, ; 91: lib-pt-Testnet9.resources.dll.so => 0
+   i64 u0x4a77e791b1c18aa8, ; 94: lib-pt-Testnet9.resources.dll.so => 0
-   i64 u0x7357acbda27bdba3, ; 158: pt/Testnet9.resources => 0
+   i64 u0xd0b0f3f98144eece, ; 285: pt\Testnet9.resources => 0

Note that above is LLVM-IR. ; begins a comment, so the values are:

Things that stick out to me:

  1. The indexes differ?! Under macOS, pt/Testnet9.resources.dll is index 19, while on Windows it's index 16.
  2. We're not normalizing directory separator characters. Compare macOS pt/Testnet9.resources to Windows pt\Testnet9.resources.
  3. The hash values for pt.*Testnet9.resources consequently differs between macOS and Windows.

Additionally, from the crash log:

F monodroid-assembly: Failed to look up image index for hash 0x7357acbda27bdba3

Hash 0x7357acbda27bdba3 is present on the macOS build, but not Windows, apparently because of the directory separator character:

-   i64 u0x7357acbda27bdba3, ; 158: pt/Testnet9.resources => 0
+   i64 u0xd0b0f3f98144eece, ; 285: pt\Testnet9.resources => 0
jonpryor commented 1 month ago

@grendello: I think we need at least:

diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs
index 46faa8d74..e268ee628 100644
--- a/src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs
+++ b/src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs
@@ -986,7 +986,8 @@ namespace Xamarin.Android.Tasks
                        foreach (string name in uniqueAssemblyNames) {
                                // We must make sure we keep the possible culture prefix, which will be treated as "directory" path here
                                string cultureName = Path.GetDirectoryName (name) ?? String.Empty;
-                               string clippedName = Path.Combine (cultureName, Path.GetFileNameWithoutExtension (name));
+                               string clippedName = Path.Combine (cultureName, Path.GetFileNameWithoutExtension (name))
+                                       .Replace (@"\", "/");
                                string inArchiveName;

                                if (cultureName.Length == 0) {
grendello commented 1 month ago

Just tested the fix on Windows, and the app works as expected.