Reputation: 724
I am trying to import native library into my Xamarin Android app. I have built the test.so file and I have included the appropriate Dllopen code. The problem is when I attempt to deploy the app I keep getting a "DllImport error loading library" error thrown. Here is the error in full.
> 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'.
I followed the process as described here https://developer.xamarin.com/guides/android/advanced_topics/using_native_libraries/. I confirmed the libtest.so is included in the apk by unzipping it, yet it seems android is having issues locating the .so file. How can I fix this issue?
Upvotes: 0
Views: 2708
Reputation: 985
According the xamarin docs you mention, you can use either path sniffing or the project file to have the linker know what's what.
Now, path sniffing does not really work with visual studio. Your dll's will be put in directories called ARM, ARM64 etc. And the path 'sniffer' expects the path to be like armeabi, armeabi-v7a, and so forth. You can either change the output path for every platform, or change your project file.
In case you go for the project file (which should be edited in manually in an external editor), the result should look something like this:
<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" />
The only problem left is armeabi vs armeabi-v7a. It seems that the application does not look into the armeabi folder on a armeabi-v7a device. If you add both ABI definitions to the same linked library, only the second one is used. Neither can you link the same file twice.
Your only option would be to make a manual copy of the library and add that. Or just leave armeabi out altogether. After all, you would have a hard time finding a device which does not support armeabi-v7a nowadays.
Upvotes: 2