Xamarin.Android: How do you link a static library? -


i trying link static library (foo.a) - contains c++ code - in xamarin.android project following directions found in xamarin's docs. neither "path sniffing method," nor "abi element within project file" method seems work.

using either method unhandled exceptions when attempt call library functions:

i/mono( 2591): [error] fatal unhandled exception: system.entrypointnotfoundexception: ... 

i should mention have had no trouble linking , calling library (built armv7, armv7s) xamarin.ios project using "additional mtouch arguments" -cxx method described here. of dllimports same across platforms...

[dllimport(import.lib, callingconvention=callingconvention.cdecl )] internal static extern intptr foomethodname(args); 

so, missing?

fyi: using xamarin studio 4.0.5 (build 4), xamarin.android 4.6.4 (business edition)

i realize question on year old, since had this, , hit head in same spot, i'll have go @ anyway...

tl;dr: cannot link static libraries xamarin android, can link dynamic libraries (.so)

steps:

  • if library written in c can skip first step. if lib written in c++ must first declare publicly exported functions c-functions in code (i.e. have put them inside #extern "c" { } block). if don't function names subjected c++ name mangling , xamarin not able find them.
extern "c" {   void my_function(bool someparameter); } 
  • using android ndk, compile library dynamic link library (.so). static library (.a) not do. hit head, not clear documentation. adding confusion, on ios it's exact opposite: can link static .a libs only there (as per appstore policies).
  • do each processor architecture wish support (typically, armeabi, armeabi-v7a , x86)
  • from here on assume have set of .so libraries, 1 each processor architecure. libraries named libx.so, x project name of library. e.g. "libmylibrary.so".
  • in xamarin android project, add folder "libs" below project root. (you can use name if wish)
  • below folder "libs", create 3 child folders, named "armeabi", "armeabi-v7a" , "x86" respectively. copy 1 of .so files each of these child folders (the 1 corresponding same processor architecture). enables xamarin "path sniffing" feature select right library right processor architecture.
  • include the 3 .so files in project , each 1 set "build action" property "androidnativelibrary"
  • now add entry functions static methods xamarin class proper dllimport attributes:
namespace myapp {     public static class mylibrary     {        [dllimport("libmylibrary", entrypoint = "my_function")]        public static extern void myfunction(boolean someparameter);     } } 
  • that's it. should able call mylibrary.myfunction() xamarin c# code

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -