title: Pass an object (DisplayMetrics) from C to Java through JNI (Android NDK)
body: <p>I am trying to get the <a href="https://developer.android.com/reference/android/util/DisplayMetrics.html" rel="nofollow">Android DisplayMetrics</a> class in a NativeActivity app using the NDK. The functions I am trying to call are the ones in these lines:</p>
  
  <pre><code>DisplayMetrics metrics = new DisplayMetrics();
  WindowManager wm = this.getWindowManager();
  Display display = wm.getDefaultDisplay();
  display.getMetrics(metrics);
  </code></pre>
  
  <p>So far, I have gotten the objects setup and the method calls ready as follows</p>
  
  <pre><code>JNIEnv *jni; 
  state-&gt;activity-&gt;vm-&gt;AttachCurrentThread(&amp;jni, NULL);
  jclass activityClass = jni-&gt;FindClass("android/app/NativeActivity");
  jclass windowManagerClass = jni-&gt;FindClass("android/view/WindowManager");
  jclass displayClass = jni-&gt;FindClass("android/view/Display");
  jclass displayMetricsClass = jni-&gt;FindClass("android/util/DisplayMetrics");
  
  jmethodID getWindowManager = jni-&gt;GetMethodID(activityClass, "getWindowManager", "()Landroid/view/WindowManager;");
  jmethodID getDefaultDisplay = jni-&gt;GetMethodID(windowManagerClass, "getDefaultDisplay", "()Landroid/view/Display;");
  jmethodID getMetrics = jni-&gt;GetMethodID(displayClass, "getMetrics", "(android/util/DisplayMetrics)L;");
  jmethodID newDisplayMetrics = jni-&gt;GetMethodID(displayMetricsClass, "DisplayMetrics", "()Landroid/util/DisplayMetrics;");
  
  jobject metrics = jni-&gt;CallObjectMethod(displayMetricsClass, newDisplayMetrics);
  jobject windowManager = jni-&gt;CallObjectMethod(state-&gt;activity-&gt;clazz, getWindowManager);
  jobject display = jni-&gt;CallObjectMethod(windowManager, getDefaultDisplay);
  </code></pre>
  
  <p>However, now I do not know how to pass in the metrics jobject to the display.getmetrics() function by reference in order to have the OS adjust it and fill it up correctly, so I can read the fields in it</p>