从 android_assets 文件夹动态加载 dll [英] Dynamically load dll from android_assets folder

查看:55
本文介绍了从 android_assets 文件夹动态加载 dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在资产文件夹中使用 Assembly.Load 加载 dll?

我有 Windows 和 android 平台特定的 dll.我尝试使用 android 特定的 dll 作为资产(也在构建操作中),但是当我尝试将它加载到我的应用程序中时,它没有找到(我使用了 路径:file:///android_asset/mydll.dll),这只会发生,如果我将应用程序构建为发行版,在调试中我会获得加载库的必要路径,但不会从资产文件夹中获得.

我能做什么?还有其他地方可以让我从我的应用程序加载我的 dll 吗?

解决方案

下面是一个将程序集写入 Plugins 文件夹,然后在运行时读取和加载的示例.

确保您将要转到 Assets 文件夹的程序集设置为 AndroidAsset 的构建操作.请参阅下面的屏幕截图.

请注意:您可能需要将扩展​​名更改为 .mp3.请参阅),但我不确定您可以为动态加载的库做太多事情.

is it possible to load an dll with Assembly.Load when it's in the asset folder?

I have platform specific dll's for windows and android. I tried to use the android specific dll as asset (also in the build action), but when I try to load it into my app it's not found (I used the path: file:///android_asset/mydll.dll), and this only happens, if i build the app as release, in debug i get the necessary paths to load the library, but not from the assets folder.

What can I do? Is there another place, where I can put my dll to be loaded from my app?

解决方案

Here is an example of writing out assemblies to a Plugins folder and then reading them in and loading at runtime.

Make sure that you have your assemblies that you want to have go to the Assets folder set to a Build Action of AndroidAsset. See screenshot below.

Please Note: You might need to change the extension to .mp3. See here. I didn't have this issue though.

Once you do that, you should be able to get the assets by using the Asset Manager. You can load them up or do whatever with them. Here is a sample of reading them into memory and the writing out the name.

const String pluginPath = "Plugins";

var pluginAssets = Assets.List(pluginPath);
foreach (var pluginAsset in pluginAssets)
{
    var file = Assets.Open(pluginPath + Java.IO.File.Separator + pluginAsset);

    using (var memStream = new MemoryStream())
    {
        file.CopyTo(memStream);

        //do something fun.
        var assembly = System.Reflection.Assembly.Load(memStream.ToArray());
        Console.WriteLine(String.Format("Loaded: {0}", assembly.FullName));
    }

}

In release mode, please be mindful that the Mono for Android is going to perform a static analysis of your libraries to perform a size optimization. If you are loading assemblies after this, then you may not have features that should have been included. The screenshot below shows the standard linking configuration for a release build. There are some flags and configurations that you can add to your code to help prevent linking (Xamarin Docs on Linking), but I am not sure that there will be too much that you can do for dynamically loaded libraries.

这篇关于从 android_assets 文件夹动态加载 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆