I have a xamarin forms app that needs to use an android native library.
I created the library as a Dynamic Shared Library (Android) (I am using Visual Studio 2022 v17.9.6).
I build the library for the four different platforms available in visual studio (x86,x64,ARM,ARM64), added the resulting .so files to the following folders in my xamarin.Android project (setting build action to AndroidNativeLibrary):
- .so from x86 added to /lib/x86.
- .so from x64 added to /lib/x64.
- .so from ARM added to /lib/armeabi-v7a.
- .so from ARM64 added to /lib/arm64-v8a.
and in .csproj file in android project I added the following:
<AndroidNativeLibrary Include="libarm64-v8alibRootUtil.so">
<Link>libarm64-v8alibRootUtil.so</Link>
<Abi>arm64-v8a</Abi>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AndroidNativeLibrary>
<AndroidNativeLibrary Include="libarmeabi-v7alibRootUtil.so">
<Link>libarmeabi-v7alibRootUtil.so</Link>
<Abi>armeabi-v7a</Abi>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AndroidNativeLibrary>
<AndroidNativeLibrary Include="libx86_64libRootUtil.so">
<Link>libx86_64libRootUtil.so</Link>
<Abi>x86_64</Abi>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AndroidNativeLibrary>
<AndroidNativeLibrary Include="libx86libRootUtil.so">
<Link>libx86libRootUtil.so</Link>
<Abi>x86</Abi>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AndroidNativeLibrary>
In Main Activity I imported the dll:
[DllImport("libRootUtil_v8l")]
public extern static int CheckRootAccessMethod1();
That works fine with a device with architrcture “aarch64” (Huawei Y 7 Prime 2019)
But with a device with architecture armv8l (for example: Samsung Galaxy Tab A6, I get the following exception:
{System.DllNotFoundException: libRootUtil assembly: type: member:(null)
at (wrapper managed-to-native) CorrespondenceApp.Droid.MainActivity.CheckRootAccessMethod1()
Note: I get the architecture using: Java.Lang.JavaSystem.GetProperty("os.arch")
How can I solve this issue?
Thanks in advance.
I tried to add another folder named /lib/arm64-v8l and add the same .so file resulting from ARM64 and add a new item for it in .csproj file:
<AndroidNativeLibrary Include="libarm64-v8llibRootUtil.so">
<Link>libarm64-v8llibRootUtil.so</Link>
<Abi>arm64-v8l</Abi>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AndroidNativeLibrary>
But didn’t work