Search code examples
androiddelphitemplatesobjectshared

delphi XE7 firemonkey android shared object / library to be dynamically loaded - template


Is there a template to create a .so file that can be loaded by another delphi ape file - I have tried starting a blank fire monkey project and changing program to library and build it but the .so file that it produces won't load with dlopen within another delphi project. I have read that in other development environments there is a islibrary setting. I guess more to the point is there an example .so library built with fire monkey - I have found the bare bones link without fire monkey but it uses just jni not androidapi.jni - thanks


Solution

  • the following codes may be useful.

    //-- Code for libMylib.so (MyLib.dproj) --- 
    
        library MyLib; 
    //    uses  SysUtils;
    
    
        function GetSum(a, b: integer ) : integer; cdecl; 
        begin 
          Result := a + b; 
        end;
    
        exports
    
        GetSum        name 'GetSum'; 
    
        begin 
        end. 
    

        //-- Code for using libMylib.so (TestMyLib.dproj) --- 
    
        var 
        DocDir, LibFN: string; 
        GetSum: function(a, b: integer ) : integer; cdecl; 
        hLib: HMODULE; 
    
        begin 
          DocDir := IncludeTrailingPathDelimiter(System.IOUtils.TPath.GetLibrary`enter code here`Path);
          LibFN:=DocDir + 'libMyLib.so';
          hLib := LoadLibrary(LibFN); //ERROR (Segmentation fault (11)) 
          if hLib<>0 then 
          begin 
            GetSum := GetProcAddress(hLib, 'GetSum');//It works
            ShowMessage(IntToStr(GetSum(3, 8)));//It works 
          end; 
        end; 
    

    P.S: you must add compiled libMyLib.so file to Deployment of TestMyLib.dproj.

    P.S2: it gives an error while executing "LoadLibrary", but it Works.

    I couldn't find a solution. Probably it is related with compiler/linker options of MyLib.dproj. Because when I test another '.so' file which is compiled with C++, no problem occurs while calling LoadLibrary.