Search code examples
c#androidmauiruntime-environment

Android debug vs release runtime directory (MAUI)


In my MAUI application, I need to get all assemblies to perform some type reflection at runtime. To do so, following partially this post, I especially use this line of code :

var runtimeAssemblies = new HashSet<string>(Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"));

This work fine on Windows in Debug and Release mode. But on android emulator, my app fail on Release only. I end up seeing a difference between Debug and Release mode on RuntimeEnvironment.GetRuntimeDirectory() that will cause my app to eventually fail. Indeed, in Debug mode, RuntimeEnvironment.GetRuntimeDirectory() return

/data/data/com.compagnyname.myprojectname/files/.__override__

whereas in Release mode it return

/data/user/0/com.compagnyname.myprojectname

instead.

Digging with adb shell I found that both /data/data/com.compagnyname.myprojectname/files/.__override__ and /data/user/0/com.compagnyname.myprojectname/files/.__override__ will be created by running the project in Debug mode where all needed DLL will be copied.

But in Release mode, none are present, though referenced. I suspect the DLL are embedded/packed somehow inside the *.apk (?). But then I can't use a MetadataLoadContext to resolve my used Assembly like mentioned above in the other S.O. post.


Solution

  • I found the Document about MetadataLoadContext said that sometimes it isn't possible to load an assembly into the execution context, for example, because it was compiled for another platform or processor architecture, or it's a reference assembly.

    In release mode for Android, these DLL files will be converted to embed resources and stored directly in the apk/AAB file. These DLL files don't exist in isolation, but are merged and optimized and packaged directly in the final binary.