Issue
I am working setting up the NDK for my android project.
I have an android project with a textview that outputs to the emulator
"Output="
I have a native function called somefunc()
that i add to this string and it throws an error stating.
E/AndroidRuntime(2242): java.lang.UnsatisfiedLinkError: Native method not found: com.ndktest.MainActivity.somefunc:()I
I have three files that I believe are needed. MainActivity.java , test.c, and Android.mk.
In test.c I have
#include <string.h>
#include <jni.h>
JNIEXPORT int JNICALL
Java_com_ndktest_MainActivity_somefunc(JNIEnv * env, jobject obj)
{
return 2;
}
And Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILE := test.c
include $(BUILD_SHARED_LIBRARY)
And MainActivity.java
package com.ndktest;
import com.ndktest.R;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
static
{
System.loadLibrary("test");
}
public native int somefunc();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = new TextView(getBaseContext());
tv.setTextSize(30);
String s = "Ouput="+somefunc();
tv.setText(s);
this.setContentView(tv);
}
}
I understand from the various guides online that spelling mistakes are the most common cause of this error. I have tried my best to check for a spelling mistake in the package and in the c function code and so far I don't see it. I also am able to run "ndk-build" on the makefile and it compiles without errors. It produces libtest and i load the library. I have tried a try catch around it and the try passes for the system load library. So i assume there is a spelling mistake or an error in how i named my c function. I have read that the c function should be called in Java_[package with underscores instead dots][java class][function name]. I think i am abiding by this. I apologize i the first half of question looks like a stereotypical "solve this for me" question. I just wanted the code there so it shows what the packages are called and what the make looks like. Thank you
Solution
You can get the correct prototypes for your native functions by calling
javah -classpath bin/classes -d jni com.ndktest.MainActivity
from your source directory. This will create a header com_ndktest_MainActivity.h
which can be included in your C file.
(BTW: if your native file is compiled as C++ you would need extern "C" {...}
)
Answered By - j.holetzeck
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.