title: Calling a java method from c++ in Android
body: <p>I'm trying to get a simple Java method call from C++ while Java calls native method. Here's the Java code:</p>
  
  <pre><code>public class MainActivity extends Activity {
      private static String LIB_NAME = "name";
  
      static {
          System.loadLibrary(LIB_NAME);
      }
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          TextView tv = (TextView) findViewById(R.id.textview);
          tv.setText(this.getJniString());
      }
  
      public void messageMe(String text) {
          System.out.println(text);
      }
  
      public native String getJniString();
  }
  </code></pre>
  
  <p>I'm trying to call <code>messageMe</code> method from native code in the process of <code>getJniString*</code> method call from Java to native.</p>
  
  <p>native.cpp:</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;jni.h&gt;
  
  jstring Java_the_package_MainActivity_getJniString( JNIEnv* env, jobject obj, jint depth ){
  
  //    JavaVM *vm;
  //    JNIEnv *env;
  //    JavaVMInitArgs vm_args;
  //    vm_args.version = JNI_VERSION_1_2;
  //    vm_args.nOptions = 0;
  //    vm_args.ignoreUnrecognized = 1;
  //
  //    // Construct a VM
  //    jint res = JNI_CreateJavaVM(&amp;vm, (void **)&amp;env, &amp;vm_args);
  
      // Construct a String
      jstring jstr = env-&gt;NewStringUTF("This string comes from JNI");
      // First get the class that contains the method you need to call
      jclass clazz = env-&gt;FindClass("the/package/MainActivity");
      // Get the method that you want to call
      jmethodID messageMe = env-&gt;GetMethodID(clazz, "messageMe", "(Ljava/lang/String;)V");
      // Call the method on the object
      jobject result = env-&gt;CallObjectMethod(jstr, messageMe);
      // Get a C-style string
      const char* str = env-&gt;GetStringUTFChars((jstring) result, NULL);
      printf("%s\n", str);
          // Clean up
      env-&gt;ReleaseStringUTFChars(jstr, str);
  
  //    // Shutdown the VM.
  //    vm-&gt;DestroyJavaVM();
  
      return env-&gt;NewStringUTF("Hello from JNI!");
  }
  </code></pre>
  
  <p>After clean compilation app stops with next message:</p>
  
  <pre><code>ERROR/AndroidRuntime(742): FATAL EXCEPTION: main
          java.lang.NoSuchMethodError: messageMe
          at *.android.t3d.MainActivity.getJniString(Native Method)
          at *.android.t3d.MainActivity.onCreate(MainActivity.java:22)
  </code></pre>
  
  <p>Apparently it means that method name is wrong, but it looks OK to me. </p>