Xamarin Android: Error including native library in application
我正在尝试将本机库导入我的 Xamarin Android 应用程序。我已经构建了 test.so 文件,并且包含了适当的 Dllopen 代码。问题是当我尝试部署应用程序时,我不断收到”DllImport error loading library”错误。这是完整的错误。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
> 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 文件时遇到问题。我该如何解决这个问题?
根据您提到的 xamarin 文档,您可以使用路径嗅探或项目文件让链接器知道什么是什么。
现在,路径嗅探不适用于 Visual Studio。您的 dll 将放在名为 ARM、ARM64 等的目录中。路径”嗅探器”期望路径类似于 armeabi、armeabi-v7a 等。您可以更改每个平台的输出路径,或更改您的项目文件。
如果您选择项目文件(应在外部编辑器中手动编辑),结果应如下所示:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<ItemGroup>
<EmbeddedNativeLibrary Include="..//CD.AndroidDll/ elease//ARM64//libDemo.so"> <Link>lib//arm64//libDemo.so</Link> <ABI>arm64-v8a</ABI> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </EmbeddedNativeLibrary> <EmbeddedNativeLibrary Include="..//CD.AndroidDll/ elease//ARM//libDemo.so"> <Link>lib//arm//libDemo.so</Link> <ABI>armeabi-v7a</ABI> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </EmbeddedNativeLibrary> <EmbeddedNativeLibrary Include="..//CD.AndroidDll/ elease//x64//libDemo.so"> <Link>lib//x64//libDemo.so</Link> <ABI>x86_64</ABI> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </EmbeddedNativeLibrary> <EmbeddedNativeLibrary Include="..//CD.AndroidDll/ elease//x86//libDemo.so"> <Link>lib//x86//libDemo.so</Link> <ABI>x86</ABI> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </EmbeddedNativeLibrary> <None Include="Resources//AboutResources.txt" /> |
剩下的唯一问题是 armeabi 与 armeabi-v7a。该应用程序似乎没有查看 armeabi-v7a 设备上的 armeabi 文件夹。如果将两个 ABI 定义添加到同一个链接库,则仅使用第二个。您也不能将同一个文件链接两次。
您唯一的选择是手动复制库并添加它。或者干脆把 armeabi 排除在外。毕竟,你现在很难找到不支持 armeabi-v7a 的设备。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/269050.html