Question

I have hard time fnding out how to load and use compiled .so library in firemonkey application. I've managed to setup project in a way that it compiles and library is bundled in apk but can't load it when application starts. Is system.LoadLibrary() enough or do I need some java wrapper for the lib?

edit: my code looks like this now, still can't get past library loading stage. TPath.GetLibraryPath returns correct path to where library is located ( I checked with adb pull)

{$IFDEF MSWINDOWS}
    LIBNAME = 'sunvox.dll';
{$ENDIF}
{$IFDEF ANDROID}
    LIBNAME = 'libsunvox.so';
{$ENDIF}

function sv_load_dll:integer;
var libPath:system.string;
begin
  g_sv_dll:= 0 ;
  libPath:=TPath.Combine(tpath.GetLibraryPath,libname);
  {$IFDEF ANDROID}
  g_sv_dll:=dlopen(MarshaledAString(libPath), RTLD_LAZY);
  {$ENDIF}
  {$IFDEF MSWINDOWS}
  g_sv_dll := LoadLibrary(MarshaledString(libPath));
  {$ENDIF}
  result:=g_sv_dll;
  if( g_sv_dll = 0 )then exit;

  sv_audio_callback:=tsv_audio_callback(import('sv_audio_callback' ));
 ...
end;
Was it helpful?

Solution

You can statically link to an exported function of the .so file using Delphi's standard external syntax on the function declaration, specifying the .so file as the external library.

Or you can dynamically load the .so into memory yourself using the dlopen() function, retrieve a pointer to the exported function using the dlsym() function, and release the library from memory using the dlclose() function. These are the equivilents of LoadLibrary(), GetProcAddress() and FreeLibrary() on Windows.

See this discussion for an example:

Difficulties with calling an Android NDK function from directly Delphi

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top