title: Use your own library with JNA on Android Studio
body: <p>I am currently creating a project in <strong>Android Studio</strong> which calls a Native C++ Library using <strong>JNA</strong>.</p>
  
  <p>I followed the instructions here: <a href="https://github.com/java-native-access/jna" rel="nofollow">https://github.com/java-native-access/jna</a>,</p>
  
  <p>I downloaded the <strong>libjnidispatch.so</strong> from <a href="https://github.com/java-native-access/jna/tree/master/lib/native/android-x86-64.jar" rel="nofollow">https://github.com/java-native-access/jna/tree/master/lib/native/android-x86-64.jar</a>,</p>
  
  <p>I added the <strong>jna.jar</strong> library to my Android Studio project,</p>
  
  <p>I extracted the <strong>libjnidispatch.so</strong> library to put it into the folder <strong>x86_64</strong> on my <strong>jniLibs</strong> directory,</p>
  
  <p>I called the printf() function,</p>
  
  <pre><code>public interface CLibrary extends Library
  {
      //---libjnidispatch---
      void printf(String format, Object... args);
  }
  ...
     CLibrary demo = (CLibrary) Native.loadLibrary("c", CLibrary.class);
     demo.prinft("Hello JNA !");
  ...
  </code></pre>
  
  <p>It ran successfully.</p>
  
  <pre><code>Hello JNA !
  </code></pre>
  
  <p>Then, I wanted to use my own library.</p>
  
  <p>I loaded it the same way like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int somme(int a, int b)
      return a+b;
  </code></pre>
  
  <p>I compiled it like this (showing gcc on linux here):</p>
  
  <pre><code>gcc -c -Wall -Werror -fPIC foo.c
  gcc -shared -o foo.so foo.o
  </code></pre>
  
  <p>Java: </p>
  
  <pre><code>package com.example.guest.essai2;
  
          import com.sun.jna.Library;
          import com.sun.jna.Native;
          import com.sun.jna.Platform;
  
          import android.*;
          import com.google.android.*;
  
  public class MainActivity extends AppCompatActivity {
  
      private GoogleApiClient client;
  
      //--------------JNA----------------------
      public interface Foo extends Library
      {
          //--foo----
          int somme(int a, int b);
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          TextView tv = new TextView(this);
  
          System.setProperty("jna.library.path", "/home/guest/workspace/MyApplication/Essai2/app/src/main/jniLibs/x86_64/");
          NativeLibrary.addSearchPath("foo", "/home/guest/workspace/MyApplication/Essai2/app/src/main/jniLibs/x86_64/");
          Native.register(Foo.class,"foo");
  
          Foo foo = (Foo) Native.loadLibrary("foo", Foo.class);
  
          String msg = ""+foo.somme(2,3);
          tv.setText(msg);
          setContentView(tv);
          client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
      }
  }
  </code></pre>
  
  <p>But it's not working.                 </p>
  
  <p>I then tried to change the jna.library.path with severals attempts : </p>
  
  <pre><code>System.setProperty("jna.library.path", "/home/guest/workspace/MyApplication/Essai2/app/src/main/jniLibs/x86_64/");
  
  NativeLibrary.addSearchPath("foo", "/home/guest/workspace/MyApplication/Essai2/app/src/main/jniLibs/x86_64/");
  
  Native.register(Foo.class,"foo");   
  </code></pre>
  
  <p>But each time, I got the following error:                 </p>
  
  <pre><code>java.lang.UnsatisfiedLinkError: Unable to load library 'foo': Native library (android-x86-64/libfoo.so) not found in resource path (.)
  </code></pre>
  
  <p>In spite of the fact that the <strong>libfoo.so</strong> and the <strong>libjnidispatch.so</strong> libraries are in the same path.</p>
  
  <p>So far, I am stuck. Is there someone out there who managed to make android studio work with JNA and a custom C++ library.</p>
  
  <p>Thank you very much in advance.</p>