Using Native Libraries

PDF for offline use:
Related Samples:

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:

  1. Path "sniffing"
  2. Using a //AndroidNativeLibrary/Abi element 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 "sniffed" as armeabi.

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 2015

If you're using Visual Studio 2015, you don't have to modify your project files (as described above). You can build and debug C++ inside your Xamarin.Android solution, simply by adding a project reference to a C++ shared library project.

Visual Studio C++ developers can see the SanAngeles_NativeDebug sample to try debugging C++ from Visual Studio 2015 with Xamarin.