Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to add second C++ file. Android JNI: UnsatisfiedLinkError - Can't find native method
    text
    copied!<p>So I'm playing around with JNI to see what its limitations are in terms of system calls. My first C file to just learn how JNI works still runs perfectly. I'm trying to add another C++ file that reads uname and concatenates into a single string then return. However the linker can't actually find the new function definition (getKernelInfo()). I've tried the obvious like double checking the types and the name of the function/java packages. However I can't seem to find the issue.</p> <p>Here are the relevant files:</p> <p>Android.mk</p> <pre><code>LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := example LOCAL_CFLAGS := -Werror LOCAL_SRC_FILES := \ example.c \ kernel.cpp LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) </code></pre> <p>Application.mk</p> <pre><code>APP_STL := stlport_static </code></pre> <p>kernel.cpp</p> <pre><code>#include &lt;jni.h&gt; #include &lt;sys/utsname.h&gt; #include &lt;string&gt; using namespace std; JNIEXPORT jstring JNICALL Java_com_test_myjniapp_MainActivity_getKernelInfo(JNIEnv * env, jobject obj) { struct utsname info; uname(&amp;info); string sysInfo = ""; char** names = (char**)&amp;(info.sysname); for(int i = 0; i &lt; 5; i++, names++) { sysInfo = sysInfo + string(*names); if(i != 4) sysInfo = sysInfo + ", "; } jstring res = env-&gt;NewStringUTF((const char*)sysInfo.c_str()); return res; } </code></pre> <p>MainActivity.java</p> <pre><code>package com.test.myjniapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { static { System.loadLibrary("example"); } private TextView textUID; private Button btnPress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textUID = (TextView) findViewById(R.id.txtUID); btnPress = (Button) findViewById(R.id.btnUID); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void onUIDClick(View v) { textUID.setText(Integer.toString(getUID())); } public void onPIDClick(View v) { textUID.setText(Integer.toString(getPID())); } public void onTimeClick(View v) { textUID.setText(Long.toString(getTime())); } public void onKernelClick(View v) { textUID.setText(getKernelInfo()); } private native int getUID(); private native int getPID(); private native long getTime(); private native String getKernelInfo(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload