简体   繁体   English

如何在Android上使用3rd Party库?

[英]how to use 3rd party library on android?

I have hard time fnding out how to load and use compiled .so library in firemonkey application. 我很难找到如何在firemonkey应用程序中加载和使用已编译的.so库的方法。 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. 我设法以一种可编译的方式设置项目,并且库捆绑在apk中,但是在应用程序启动时无法加载。 Is system.LoadLibrary() enough or do I need some java wrapper for the lib? system.LoadLibrary()是否足够,或者我需要为lib提供一些Java包装器?

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) TPath.GetLibraryPath返回库所在位置的正确路径(我用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;

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. 您可以在函数声明中使用Delphi的标准external语法将.so文件指定为外部库,以静态方式链接到.so文件的导出函数。

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. 或者,您可以使用dlopen()函数自己将.so动态加载到内存中,使用dlsym()函数检索指向导出函数的指针,并使用dlclose()函数从内存中释放库。 These are the equivilents of LoadLibrary() , GetProcAddress() and FreeLibrary() on Windows. 这些是Windows上LoadLibrary()GetProcAddress()FreeLibrary()的等效项。

See this discussion for an example: 有关示例,请参见此讨论:

Difficulties with calling an Android NDK function from directly Delphi 直接从Delphi调用Android NDK函数的困难

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM