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

[Android] Dynamically loading an assembly results in BadImageFormatException #24221

Closed
MasterMann opened this issue Aug 13, 2024 · 3 comments
Closed
Labels
platform/android 🤖 t/bug Something isn't working

Comments

@MasterMann
Copy link

MasterMann commented Aug 13, 2024

Description

I have a plugin-style assembly located in /data/data/.../<apk name>/files/<assembly name>.dll, that is copied to an asset folder before build and marked as "AndroidAsset" (later copied to that directory on target device).
Loading it dynamically at runtime through Assembly.LoadFrom results in a BadImageFormatException.

My suspicion is that the related native code that produces the error is around here: https://github.com/dotnet/runtime/blob/c788546f9ad43ea17981d5dc9343b00b6f76d98f/src/coreclr/vm/assemblynative.cpp#L200-L208

Steps to Reproduce

  1. Create a new MAUI Android project (either via Avalonia Template Studio, or explicitly MAUI)
  2. Set the target framework for the project to net8.0-android
  3. Create a project directory, mark it and all contents within as AndroidAsset (<AndroidAsset Include="Assets\plugins\**\*" />)
  4. Create a new .NET 8 library, build it and copy into the target plugin directory in the Android project
  5. Add code to copy the plugin library from APK assets to /data/data/.../files/ folder (via AssetManager and StreamReader/Writer)
  6. Compile and run the Android project on the target device, observe the exception at Assembly.LoadFrom call

Link to public reproduction project repository

No response

Version with bug

8.0.61 SR6.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14 (API 34)

Did you find any workaround?

None at the moment. Tried exporting the library for linux-arm64 architecture, same problem.

Relevant log output

[ERROR] FATAL UNHANDLED EXCEPTION: System.BadImageFormatException: Invalid Image: /data/user/0/net.georgeb.my.life/files/plugins/MyLife.App.Plugins.Content.Todo/MyLife.App.Plugins.Content.Todo.dll
File name: '/data/user/0/net.georgeb.my.life/files/plugins/MyLife.App.Plugins.Content.Todo/MyLife.App.Plugins.Content.Todo.dll'
at System.Runtime.Loader.AssemblyLoadContext.InternalLoadFromPath(String assemblyPath, String nativeImagePath)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at MyLife.App.Plugins.Core.Utilities.Plugins.GenericPluginLoader`1[[MyLife.App.Plugins.Core.Services.Features.IFeaturePlugin, MyLife.App.Plugins.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Load(String pluginPath, Object[] constructorArgs) in E:\Dev\MasterManDev\Personal\MyLife\Plugins\MyLife.App.Plugins.Core\Utilities\Plugins\GenericPluginLoader.cs:line 39
at MyLife.App.Shared.Services.Features.BaseFeaturePluginManager.Initialize(IEnumerable`1 pluginFilePaths) in E:\Dev\MasterManDev\Personal\MyLife\MyLife.App.Shared\Services\Features\BaseFeaturePluginManager.cs:line 53
at MyLife.App.Shared.Services.Features.BaseFeaturePluginManager..ctor() in E:\Dev\MasterManDev\Personal\MyLife\MyLife.App.Shared\Services\Features\BaseFeaturePluginManager.cs:line 44
at MyLife.App.Android.MyLifeAndroidApp.InitPlatform() in E:\Dev\MasterManDev\Personal\MyLife\Android\MyLife.App.Android\MyLifeAndroidApp.cs:line 59
at MyLife.App.Shared.MyLifeApp.OnFrameworkInitializationCompleted() in E:\Dev\MasterManDev\Personal\MyLife\MyLife.App.Shared.UI\MyLifeApp.axaml.cs:line 25
at Avalonia.AppBuilder.SetupUnsafe()
at Avalonia.AppBuilder.Setup()
at Avalonia.AppBuilder.SetupWithLifetime(IApplicationLifetime lifetime)
at Avalonia.Android.AvaloniaMainActivity.InitializeAvaloniaView(Object initialContent)
at Avalonia.Android.AvaloniaActivity.OnCreate(Bundle savedInstanceState)
at MyLife.App.Android.Activities.MobileMainActivity.OnCreate(Bundle bundle) in E:\Dev\MasterManDev\Personal\MyLife\Android\MyLife.App.Android\Activities\MainActivity.cs:line 34
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:line 3082
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 121
@MasterMann MasterMann added the t/bug Something isn't working label Aug 13, 2024
Copy link
Contributor

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
Copy link
Contributor

@jonathanpeppers Would this be a runtime issue? This doesn't seem like it would be related to the UI Framework or tooling.

@MasterMann
Copy link
Author

Apologies, it seems like the problem was due to a corrupted file from a bad copying (using StreamReader/Writer seems like a bad idea for binary files).
Issue has been resolved, going to close it.

I'll link to my comments in another repo with the same issue for more details: dotnet/android#9199 (comment)

@github-actions github-actions bot locked and limited conversation to collaborators Sep 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants