How can I include a C++ dll in the .apk file

Hi,
I am using a C++ dll file compiled with Visual Studio. When I try to run the game in editor, it works fine and I can use the functions imported from the DLL. I can also build it for windows and run the game without any errors.
But when I build the apk for android and run it on the device I get these errors:

E/Unity   (23601): Unable to find sflib.dll
D/Unity   (23601): Unable to lookup library path for 'sflib.dll', native renderplugin support disabled.
E/Unity   (23601): Unable to find sflib.dll
I/Unity   (23601): DllNotFoundException: sflib.dll
I/Unity   (23601):   at (wrapper managed-to-native) sfish_script:sf_init ()
I/Unity   (23601):
I/Unity   (23601): (Filename: C Line: 0)

Also when I open the apk as an archive, I can not see sflib.dll anywhere. Though not sure if I should be able to see that, since it is not managed code.

Any suggestions?
Thanks.

Unity has extensive support for Plugins, which are libraries of native code written in C, C++, Objective-C, etc. Plugins allow your game code (written in Javascript, C# or Boo) to call functions from these libraries. This feature allows Unity to integrate with middleware libraries or existing C/C++ game code.

Note: On the desktop platforms, plugins are a pro-only feature. For security reasons, plugins are not usable with webplayers.

In order to use a plugin you need to do two things:-

Write functions in a C-based language and compile them into a library.
Create a C# script which calls functions in the library.
The plugin should provide a simple C interface which the C# script then exposes to other user scripts. It is also possible for Unity to call functions exported by the plugin when certain low-level rendering events happen (for example, when a graphics device is created), see the Native Plugin Interface page for details.

Here is a very simple example:
C/C++ File of a Minimal Plugin:

 float FooPluginFunction () { return 5.0F; } 

C# Script that Uses the Plugin:

using UnityEngine;
using System.Runtime.InteropServices;

class SomeScript : MonoBehaviour {

   #if UNITY_IPHONE || UNITY_XBOX360
   
   // On iOS and Xbox 360 plugins are statically linked into
   // the executable, so we have to use __Internal as the
   // library name.
   [DllImport ("__Internal")]

   #else

   // Other platforms load plugins dynamically, so pass the name
   // of the plugin's dynamic library.
   [DllImport ("PluginName")]
    
   #endif

   private static extern float FooPluginFunction ();

   void Awake () {
      // Calls the FooPluginFunction inside the plugin
      // And prints 5 to the console
      print (FooPluginFunction ());
   }
}

Note that when using Javascript you will need to use the following syntax, where DLLName is the name of the plugin you have written, or “__Internal” if you are writing statically linked native code:

@DllImport (DLLName)
static private function FooPluginFunction () : float {};

Creating a Plugin

In general, plugins are built with native code compilers on the target platform. Since plugin functions use a C-based call interface, you must avoid name mangling issues when using C++ or Objective-C.

For further details and examples, see the following pages:

DLL is a windows specific type of shared library. For Androif you’ll have to compile your native code into an .so yourself - Unity looks for the appropriate type of library based on the runtime platform. I cannot load windows specific libraries on any other system.

Aside from the answers already posted, you can export your project as a ‘Google Project’ and import it to eclipse (or equivalent)

Android has the NDK for download which is for that very purpose, of creating and using native code plugins.

Hello everyone,
It’s been about a week since i am having an issue regarding acpal-dll missing, It’s causing an app i frequently use crash, can someone tell how to fix this problem.
Thank you,
(chloe decker)

Thanks for the copied answer from the link, but I don’t see how it is related to the DLLNotFoundException problem.

The problem is not about calling functions from an external DLL, things work just fine on the desktop or the editor. But its the apk file running on the android that can not find the DLL code.

  1. Is it normal that I don’t see the DLL file itself in the apk? Or does it get converted to some other format when publishing the package to apk?

  2. I place the DLL file in the plugins folder and also a copy in the main folder. It still doesn’t solve the problem. What else can I do for the apk file to be able to use the DLL

Martin, I will try that.
I guess Unity will add the .so file from the plugins folder into the apk

Edit: for those looking for a clear description, here is a link from unity: