Using Native Libraries
Xamarin.Android supports the use of native libraries via the standard PInvoke mechanism. You can also bundle additional native libraries which are not part of the OS into your .apk.
To deploy a native library with a Xamarin.Android application, add the library binary to the project and set its Build Action to AndroidNativeLibrary.
To deploy a native library with a Xamarin.Android library project, add the library binary to the project and set its Build Action to EmbeddedNativeLibrary.
Note that since Android supports multiple Application Binary Interfaces (ABIs), Xamarin.Android must know which ABI the native library is built for. There are two ways this can be done:
- Path "sniffing"
- By using an
AndroidNativeLibrary/Abielement within the project file
With path sniffing, the parent directory name of the native library is used
to specify the ABI that the library targets. Thus, if you add
lib/armeabi/libfoo.so to the project, then the ABI will be
Alternatively, you can edit your project file to explicitly specify the ABI to use:
<ItemGroup> <AndroidNativeLibrary Include="path/to/libfoo.so"> <Abi>armeabi</Abi> </AndroidNativeLibrary> </ItemGroup>
For more information about using native libraries, see Interop with native libraries.
Debugging Native Code with Visual Studio 2017
If you're using Visual Studio 2017 or greater, you don't have to modify your project files as described above. You can build and debug C++ inside your Xamarin.Android solution by adding a project reference to a C++ Dynamic Shared Library (Android) project.
To debug native C++ code in your project, follow these steps:
- Double-click project Properties and select the Android Options page.
- Scroll down to Debugging options.
- In the Debugger dropdown menu, select C++ (instead of the default .Net (Xamarin)).