title: Use a simple c++ class in Android NDK
body: <p>I'm trying to learn the basics of Android NDK but I'm stucked when I have to use it with a c++ class.</p>
  
  <p>I understand how to use it with a simple function but what should I do to be able to manipulate the fields and the methods of a c++ class ?</p>
  
  <p>I'm trying to do it with this simple c++ class :</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;jni.h&gt;
  using namespace std;
  
  
  class Point {
     int x, y; // coordonnées du point
  
     public:
        Point() {
           this-&gt;x = 0;
           this-&gt;y = 0;
        }
  
        Point(int x, int y) {
           this-&gt;x = x;
           this-&gt;y = y;
        }
  
        int getX() const {
           return x;
        }
  
        int getY() const {
           return y;
        }
  
        Point symetrique() const {
           return Point(-x, -y);
        }
  
        bool operator ==(const Point &amp;p) const {
           return this-&gt;x == p.getX() &amp;&amp; this-&gt;y == p.getY();
        }
  };
  
  extern "C" {
      JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__
        (JNIEnv *, jobject);
  
      JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__II
        (JNIEnv *, jobject, jint, jint);
  
      JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetX
        (JNIEnv *, jobject, jlong);
  
      JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetY
        (JNIEnv *, jobject, jlong);
  
      JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_nativeSymetrique
        (JNIEnv *, jobject, jlong);
  };
  
  
  JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__(JNIEnv* env, jobject thiz) {
      return (jlong)(new Point());
  }
  
  JNIEXPORT jlong JNICALL Java_com_example_jnipoint_JPoint_createPoint__II(JNIEnv* env, jobject thiz, jint x, jint y) {
      return (jlong)(new Point(x, y));
  }
  
  JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetX(JNIEnv* env, jobject thiz, jlong nativePointer) {
      return ((Point*)nativePointer)-&gt;getX();
  }
  
  JNIEXPORT jint JNICALL Java_com_example_jnipoint_JPoint_nativeGetY(JNIEnv* env, jobject thiz, jlong nativePointer) {
      return ((Point*)nativePointer)-&gt;getY();
  }
  
  jlong Java_com_example_jnipoint_JPoint_nativeSymetrique(JNIEnv* env, jobject thiz, jlong nativePointer) {
      return ((Point*)nativePointer)-&gt;symetrique();
  }
  </code></pre>
  
  <p>I tried to find samples but nothing so far... Maybe I'm not using the right keywords</p>
  
  <p><em><strong></em>* UPDATE <em>*</em></strong></p>
  
  <p>I created a Java wrapper for the c++ Point class and added to the c++ file JNI methods. The code is the following :</p>
  
  <pre><code>public class JPoint {
  
      private long nativePointer;
  
      public JPoint() {
          nativePointer = createPoint();
      }
  
      public JPoint(int x, int y) {
          nativePointer = createPoint(x, y);
      }
  
      public int getX() {
          return nativeGetX(nativePointer);
      }
  
      public int getY() {
          return nativeGetY(nativePointer);
      }
  
      public JPoint symetrique() {
          JPoint tmp = new JPoint();
          tmp.nativePointer = nativeSymetrique(nativePointer);
          return tmp;
      }
  
      // TODO
      /*public boolean equals(Object o) {
          return nativeEquals(o);
      }*/
  
      private native long createPoint(); // Void constructor
      private native long createPoint(int x, int y);
      private native int nativeGetX(long nativePointer);
      private native int nativeGetY(long nativePointer);
      private native long nativeSymetrique(long nativePointer);
      //private native boolean nativeEquals(Object p); TODO
  }
  </code></pre>
  
  <p>Right now I'm stucked with the nativeSymetrique function, it says that I cannot convert 'Point' to 'jlong'. Can anyone help me on this ? Thanks</p>
  
  <p><em><strong></em>* UPDATE 2 <em>*</em></strong></p>
  
  <p>SWIG solved my issues, you don't have to handwrite the wrappers and it seems to be a good choice for big libraries.</p>