title: Calling java code from C++: exception java.lang.NoSuchMethodError
body: <p>I am trying to call some java code from a JNI C++ function and I get an exception: java.lang.NoSuchMethodError</p>
  
  <p>The C++ code where the java code is called is a callback function called from another native library.
  This code was working perfectly fine when I used the setup described in 
  <a href="http://morgwai.pl/ndkTutorial/" rel="nofollow">here</a>:</p>
  
  <p>Basically an APK that was using a JNI and that JNI was making calls to a native libray compiled elsewhere).</p>
  
  <p>But then I wanted to compile all my code in the AOSP, so I put the code of my native library, the JNI and the APK code in vendor/MyCode/MyApp. It compiles fine, native methods called from the java work OK but java code being called from the JNI now crashes all.</p>
  
  <p>Here is the java code of the method I want to call from the JNI:</p>
  
  <pre><code>package com.android.mycode.myapp;
  
  import android.app.Activity;
  
  public class MainActivity extends Activity implements OnClickListener
  {
      private native int powerOn();
  
      ...
  
  public void ndefRead(int tech, int protocol, byte[] ndef)
  {
      Log.d(tag, "ndefRead()");
      Message msg = mHandler.obtainMessage();
      msg.what = MSG_NDEF_READ_RECEIVED;
      msg.obj = ndef;
      msg.arg1 = tech;
      msg.arg2 = protocol;
      mHandler.sendMessage(msg);
  }
  
  ...
  }
  </code></pre>
  
  <p>My JNI code where I register the java data:</p>
  
  <pre><code>JNIEXPORT jint JNICALL
  Java_com_android_mycode_myapp_MainActivity_powerOn(JNIEnv * env, jobject obj)
  {
      LOGD("calling powerOn()"); //Or ANDROID_LOG_INFO, ...
  
      env-&gt;GetJavaVM(&amp;javaVM);
      jclass cls = env-&gt;GetObjectClass(obj);
      activityClass = (jclass) env-&gt;NewGlobalRef(cls);
      activityObj = env-&gt;NewGlobalRef(obj);
      ...
      return status;
  }
  </code></pre>
  
  <p>The JNI code of the callback function where the java code is called:</p>
  
  <pre><code>void EventCallback(UINT8 event, tEVT_CBACK_DATA* eventData)
  {
  LOGD("EventCallback() - event: 0x%x", event);
  
  switch (event)
  {
      case NDEF_READ_EVT:
      {
          LOGD("EventCallback() - NDEF_READ_EVT - data length: 0x%x", eventData-&gt;ndefReadEvt.length);
  
          JNIEnv *env;
          javaVM-&gt;AttachCurrentThread(&amp;env, NULL);
  
          jmethodID ndefReadID = env-&gt;GetMethodID(activityClass, "ndefRead", "(II[B)V");
          if (ndefReadID == 0)
          {
              LOGD("Function ndefRead() not found");
              return;
          }
  
          jbyteArray result = env-&gt;NewByteArray(eventData-&gt;ndefReadEvt.length);
          if (result != NULL)
          {
              env-&gt;SetByteArrayRegion(result, 0, eventData-&gt;ndefReadEvt.length, (jbyte *) eventData-&gt;ndefReadEvt.p_ndef);
          }
  
          env-&gt;CallVoidMethod(activityObj, ndefReadID, eventData-&gt;ndefReadEvt.tech, eventData-&gt;ndefReadEvt.protocol, result);
          javaVM-&gt;DetachCurrentThread();
      }
          break;
  }
  </code></pre>
  
  <p>The makefile of the JNI looks like this:</p>
  
  <pre><code>LOCAL_MODULE    := libinterface
  LOCAL_SRC_FILES := interface.cpp
  LOCAL_LDLIBS := -llog
  LOCAL_SHARED_LIBRARIES := libnfc-nci
  </code></pre>
  
  <p>And when executing this code I get the following exception/error message:</p>
  
  <pre><code>Pending exception java.lang.NoSuchMethodError thrown by 'unknown throw location'
  java.lang.NoSuchMethodError: no non-static method "Lcom/android/mycode/myapp/MainActivity;.ndefRead(II[B)V"
  </code></pre>
  
  <p>I have looked at several other problems of this type reported on this site but could not quite found something that is similar to mine. If someone has an idea it would be greatly appreciated.</p>
  
  <p>Edited to add code of ndefRead() function and makefile of JNI.</p>