Native Android library in Unity throws DllNotFound exception
<p>I have written a chess library in C++ and I have compiled it for Windows (32-bit and 64-bit) and Android (x86 and armebi-v7).</p>

<p>My Windows build works completely correct, But when I build it for Android and when I run the game, I receive the following exception on my <code>adb logcat -s Unity</code> :</p>

<pre><code>11-23 16:39:37.278 22335 22352 I Unity   : DllNotFoundException: MatinChess
11-23 16:39:37.278 22335 22352 I Unity   :   at (wrapper managed-to-native) MatinChess.Net.ExternMethods:Initialize ()
11-23 16:39:37.278 22335 22352 I Unity   :   at MatinChess.Net.MatinChess..ctor () [0x00000] in &lt;filename unknown&gt;:0
11-23 16:39:37.278 22335 22352 I Unity   :   at Model.Awake () [0x00000] in &lt;filename unknown&gt;:0
</code></pre>

<p>And when I unzip my apk file I have:</p>

<pre><code>lib
|_ armeabi-v7a
|  |_ libmain.so
|  |_ libMatinChess.so
|  |_ libmono.so
|  |_ libunity.so
|_ x86
   |_ libmain.so
   |_ libMatinChess.so
   |_ libmono.so
   |_ libunity.so
</code></pre>

<h2>Here are my settings and scripts:</h2>

<p>Based on <a href="https://docs.unity3d.com/Manual/PluginInspector.html" rel="nofollow noreferrer">here</a> and <a href="https://docs.unity3d.com/Manual/PluginsForAndroid.html" rel="nofollow noreferrer">here</a>, I have created <code>Plugins</code> folder in my <strong>Assets</strong> and placed my libraries like this:</p>

<pre><code>Plugins
|_ Android
|  |_ libs
|     |_ armeabi-v7a
|     |  |_ libMatinChess.so
|     |_ x86
|        |_ libMatinChess.so
|_ x64
|  |_ MatinChess.dll
|_ x86
   |_ MatinChess.dll
</code></pre>

<p>And I got sure that the <strong>Platform Settings</strong> in my <strong>Inspector</strong> is configured correctly.</p>

<p>I have used my library in my scripts and based on <a href="http://www.mono-project.com/docs/advanced/pinvoke/#library-names" rel="nofollow noreferrer">here</a> and <a href="https://docs.unity3d.com/Manual/PluginsForAndroid.html" rel="nofollow noreferrer">here</a>, instead of using <code>libMatinChess.so</code> for Android, I have not used <code>lib</code> at the beggining and <code>.so</code> at the end. So it is like this:</p>

<pre><code>class ExternMethods
{
#if UNITY_ANDROID
    const string dll = "MatinChess";
#else
    const string dll = "MatinChess.dll";
#endif

    [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
    public extern static PlayState CheckState();

    [DllImport(dll, CallingConvention = CallingConvention.Cdecl)]
    public extern static void Initialize();

    //
    // other extern methods
    //
}
</code></pre>

<p>So when I build my Unity game for Windows, it works correctly. In order to make it work in Editor, I have followed <a href="https://stackoverflow.com/a/33124250/3511987">this link</a> and I have written the folowing script:</p>

<pre><code>public class Model : MonoBehaviour
{    
    void Awake()
    {
#if UNITY_EDITOR_32
        var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x32";
#elif UNITY_EDITOR_64
        var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x64";
#else // Player
        var dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins";
#endif
        var currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
        if (currentPath != null &amp;&amp; currentPath.Contains(dllPath) == false)
            Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
    }
}
</code></pre>

<p>So it works on Unity Editor as well.</p>