Android NDK C + + JNI (没有原生的实现...)

我试着在 c + + 中使用 NDK,但是似乎不能得到正确的方法变数命名原则。我的原始方法如下:

extern "C" {
JNIEXPORT void JNICALL Java_com_test_jnitest_SurfaceRenderer_drawFromJni
(JNIEnv* env, jclass c)
{
//
}
}

用外部“ C”{}包装头。

一切都编译得很好,创建了一个。因此,文件和副本到我的项目下的 libs 文件夹,但是当我在 Eclipse 中调试和运行时,我总是得到一个 log cat 消息,即“没有为本机... ... 找到实现... ...”。我是不是漏掉了什么,因为所有 NDK 的例子都在 C 里?

谢谢。

107005 次浏览

There are a couple of things that can lead to "no implementation found". One is getting the function prototype name wrong, another is failing to load the .so at all. Are you sure that System.loadLibrary() is being called before the method is used?

If you don't have a JNI_OnLoad function defined, you may want to create one and have it spit out a log message just to verify that the lib is getting pulled in successfully.

You already dodged the most common problem -- forgetting to use extern "C" -- so it's either the above or some slight misspelling. What does the Java declaration look like?

Use javah (part of Java SDK). Its the tool exactly for this (generates .h header from .class file).

I had the same problem, but to me the error was in the file Android.mk. I had it:

LOCAL_SRC_FILES := A.cpp
LOCAL_SRC_FILES := B.cpp

but should have this:

LOCAL_SRC_FILES := A.cpp
LOCAL_SRC_FILES += B.cpp

note the detail += instead :=

I hope that helps.

An additional reason: Use LOCAL_WHOLE_STATIC_LIBRARIES instead of LOCAL_STATIC_LIBRARIES in android.mk. This stops the library from optimizing out unused API calls because the NDK cannot detect the use of the native bindings from java code.

An additional cause for this error: your undecorated native method name must not contain an underscore!

For example, I wanted to export a C function named AudioCapture_Ping(). Here is my export declaration in C:

JNI_EXPORT int Java_com_obsidian_mobilehashhost_MainActivity_AudioCapture_Ping(JNIEnv *pJniEnv, jobject object);  //Notice the underscore before Ping

Here was my Java class importing the function:

package com.obsidian.mobileaudiohashhost;
...
public class MainActivity extends Activity {
private native int AudioCapture_Ping();  // FAILS
...

I could not get Android to dynamically link to my native method until I removed the underscore:

JNI_EXPORT int Java_com_obsidian_mobilehashhost_MainActivity_AudioCapturePing(JNIEnv *pJniEnv, jobject object);


package com.obsidian.mobileaudiohashhost;
...
public class MainActivity extends Activity {
private native int AudioCapturePing();  // THIS WORKS!
...

Called extern "C" as provided in the automatically-generated Studio example, but forgot to wrap the entire rest of the file, including following functions, in {} brackets. Only the first function worked.

I try all above solutions, but no one can solved my build error(jni java.lang.UnsatisfiedLinkError: No implementation found for...), at last I found that I forget to add my verify.cpp source file to CMakeList.txt add_library segement(verify.cpp is auto generate by Ctrl + Enter short key, maybe other file name), hope my response can help some one.

my build environment: Gradle + CMake

I Faced the same problem, and in my case the reason was that I had underscore in package name "RFID_Test" I renamed the Package and it worked. Thanks user1222021

If your package name includes _ character, you should write 1(one) after _ character as shown below:

MainActivity.java

package com.example.testcpp_2;

native-lib.cpp

JNICALL
Java_com_example_testcpp_12_MainActivity_stringFromJNI(

I faced the same problem twice. It happened, that the phone I tried to start the app from Android Studio used an API level that I haven't downloaded yet in Android Studio.

  1. Upgrade Android Studio to the latest version
  2. Download the necessary API from within Android Studio