DllImport - DllNotFoundException with android.so

This is the first time I'm trying to create a C ++ library for Android / iOS.

I am using Visual Studio 2015 - Xamarin.

First I created a project: Visual C ++ -> Cross Platform -> Shared Library. In the guest library I created 2 files.

SayHello.h:

#pragma once #include <string.h> class SayHello { public: SayHello(); ~SayHello(); static char* Hello(); }; 

SayHello.cpp:

 #include "SayHello.h" extern "C" { SayHello::SayHello(){} SayHello::~SayHello(){} char * SayHello::Hello() { return "Hello !"; } } 

Then I generated the libSayHello.so file and created an Android project with xamarin to try to call the hello function. There is my MainActivity.cs:

 [DllImport("libSayHello.so")] static extern String Hello(); protected override void OnCreate(Bundle bundle) { // I paste only my added code : String hello = Hello(); Toast.MakeText(this.ApplicationContext, hello, ToastLength.Long); } 

I have done all the steps in this tutorial , but I have an exception:

System.DllNotFoundException: libSayHello.so

I was looking for this, but I have to be so noob because I did not find anything. How to use my libSayHello.so ?

EDIT:

There is my libSayHello.so with 7zip:

enter image description here

And my project:

enter image description here

+5
source share
2 answers

I think this will be the best sample for you.

All this works as follows:

  • Android supports 7 processor architectures.

    But Xamarin supports 5 of them. Therefore, in the settings of your Xamarin.Android project, check which architectures you will support:

    [Xamarin.Droid.project]->[Properties]->[Android Options]->[Advanced]->[Supported architectures]

enter image description here

Check which arches are needed for your project. Accordingly, your shared library should be compiled for these arches. And you should put your shared libraries in the Xamarin.Droid.project lib folder:

enter image description here

  1. To see them in Solution Explorer, you must mention them in your Xamarin.Android .CSPROJ project.

    Add the following groups of elements there:

    <ItemGroup> <AndroidNativeLibrary Include="lib\{ARCH}\libCLib.so"> <Abi>{ARCH}</Abi> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </AndroidNativeLibrary> </ItemGroup>

    {ARCH} could be: armeabi , armeabi-v7a , arm64-v8a , x86 x86_64 .

  2. Now you can put DllImport in your code:

     [DllImport("libCLib", EntryPoint = "clib_add")] public static extern int Add(int left, int right); 

    I think you should say about the entry point because I had runtime errors without this System.EntryPointNotFoundException statement .

    And don't forget to add the following code:

    using System.Runtime.InteropServices;

+2
source
  • Confirm that the library is placed in the "/ lib / {arch} /" folder.
  • Try [DllImport ("SayHello")]. the engine can automatically add "lib" and ".so".
0
source

Source: https://habr.com/ru/post/1272041/


All Articles