我正在尝试将本机库导入我的Xamarin Android应用程序。我已经构建了test.so文件,并且我已经包含了相应的Dllopen代码。问题是当我尝试部署应用程序时,我不断收到一个" DllImport错误加载库"错误抛出。这是完整的错误。
> DllImport attempting to load: 'test'. 07-20 11:07:26.428 D/Mono
> (11429): DllImport error loading library
> '/storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest':
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest"
> not found'. 07-20 11:07:26.428 D/Mono (11429): DllImport error
> loading library
> '/storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest.so':
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest.so"
> not found'. 07-20 11:07:26.428 D/Mono (11429): DllImport error
> loading library '/system/lib/libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//system/lib/libtest"
> not found'. 07-20 11:07:26.428 D/Mono (11429): DllImport error
> loading library '/system/lib/libtest.so': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//system/lib/libtest.so"
> not found'. 07-20 11:07:26.428 D/Mono (11429): DllImport error
> loading library 'libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest" not found'.
> 07-20 11:07:26.428 D/Mono (11429): DllImport error loading library
> 'libtest.so': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest.so" not found'.
> 07-20 11:07:26.428 D/Mono (11429): DllImport error loading library
> 'test': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/test" not found'. 07-20
> 11:07:26.438 D/Mono (11429): DllImport error loading library
> '/storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest':
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library
> '/storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest.so':
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest.so"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library '/system/lib/libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//system/lib/libtest"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library '/system/lib/libtest.so': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//system/lib/libtest.so"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library 'libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest" not found'.
> 07-20 11:07:26.438 D/Mono (11429): DllImport error loading library
> 'libtest.so': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest.so" not found'.
> 07-20 11:07:26.438 D/Mono (11429): DllImport error loading library
> 'libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest" not found'.
> 07-20 11:07:26.438 W/Mono (11429): DllImport unable to load library
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest" not found'.
> 07-20 11:07:26.438 D/Mono (11429): DllImport attempting to load:
> 'test'. 07-20 11:07:26.438 D/Mono (11429): DllImport error loading
> library
> '/storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest':
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library
> '/storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest.so':
> 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//storage/emulated/0/Android/data/XammarinDemo.XammarinDemo/files/.__override__/libtest.so"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library '/system/lib/libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//system/lib/libtest"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library '/system/lib/libtest.so': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm//system/lib/libtest.so"
> not found'. 07-20 11:07:26.438 D/Mono (11429): DllImport error
> loading library 'libtest': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest" not found'.
> 07-20 11:07:26.438 D/Mono (11429): DllImport error loading library
> 'libtest.so': 'dlopen failed: library
> "/data/app/XammarinDemo.XammarinDemo-1/lib/arm/libtest.so" not found'.
我按照此处所述的流程https://developer.xamarin.com/guides/android/advanced_topics/using_native_libraries/进行了操作。我确认libtest.so包含在apk中解压缩它,但似乎android有问题找到.so文件。我该如何解决这个问题?
答案 0 :(得分:1)
根据您提到的xamarin文档,您可以使用路径嗅探或项目文件来让链接器知道什么是什么。
现在,路径嗅探并不适用于visual studio。你的dll将放在名为ARM,ARM64等的目录中。路径' sniffer'期望路径像armeabi,armeabi-v7a等等。您可以更改每个平台的输出路径,也可以更改项目文件。
如果您选择项目文件(应在外部编辑器中手动编辑),结果应如下所示:
<ItemGroup>
<EmbeddedNativeLibrary Include="..\CD.AndroidDll\Release\ARM64\libDemo.so">
<Link>lib\arm64\libDemo.so</Link>
<ABI>arm64-v8a</ABI>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedNativeLibrary>
<EmbeddedNativeLibrary Include="..\CD.AndroidDll\Release\ARM\libDemo.so">
<Link>lib\arm\libDemo.so</Link>
<ABI>armeabi-v7a</ABI>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedNativeLibrary>
<EmbeddedNativeLibrary Include="..\CD.AndroidDll\Release\x64\libDemo.so">
<Link>lib\x64\libDemo.so</Link>
<ABI>x86_64</ABI>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedNativeLibrary>
<EmbeddedNativeLibrary Include="..\CD.AndroidDll\Release\x86\libDemo.so">
<Link>lib\x86\libDemo.so</Link>
<ABI>x86</ABI>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedNativeLibrary>
<None Include="Resources\AboutResources.txt" />
唯一的问题是armeabi vs armeabi-v7a。似乎应用程序没有查看armeabi-v7a设备上的armeabi文件夹。如果将两个ABI定义添加到同一链接库,则仅使用第二个。你也不能两次链接同一个文件。
您唯一的选择是制作库的手动副本并添加它。或者只是完全离开armeabi。毕竟,现在很难找到一台不支持armeabi-v7a的设备。