Android NDK C++ JNI(未找到本机的实现...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android NDK C++ JNI (no implementation found for native...)
提问by Patrick Kafka
I'm trying to use the NDK with C++ and can't seem to get the method naming convention correct. my native method is as follows:
我正在尝试将 NDK 与 C++ 一起使用,但似乎无法获得正确的方法命名约定。我的本地方法如下:
extern "C" {
JNIEXPORT void JNICALL Java_com_test_jnitest_SurfaceRenderer_drawFromJni
(JNIEnv* env, jclass c)
{
//
}
}
with a header wrapped in extern "C" {} aslo.
带有用 extern "C" {} 包裹的标头。
Everything compiles fine, creates a .so file and copies to the libs folder under my project, but when I debug and run in Eclipse I keep getting a log cat message that of "no implementation found for native...". Is there something i'm missing as all the NDK examples are in C?
一切都编译得很好,创建了一个 .so 文件并复制到我的项目下的 libs 文件夹,但是当我在 Eclipse 中调试和运行时,我不断收到一条日志消息“没有找到本机的实现......”。由于所有 NDK 示例都在 C 中,我是否遗漏了什么?
Thanks.
谢谢。
回答by fadden
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?
有几件事可能会导致“找不到实现”。一个是函数原型名称错误,另一个是根本无法加载 .so。您确定System.loadLibrary()
在使用该方法之前正在调用它吗?
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.
如果您没有JNI_OnLoad
定义函数,您可能希望创建一个函数并让它输出一条日志消息,以验证该库是否已成功拉入。
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?
您已经避开了最常见的问题——忘记使用extern "C"
——所以要么是上述问题,要么是一些轻微的拼写错误。Java 声明是什么样的?
回答by Patrick Kafka
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:
例如,我想导出一个名为AudioCapture_Ping()
. 这是我在 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:
这是我导入函数的 Java 类:
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:
在删除下划线之前,我无法让 Android 动态链接到我的本机方法:
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!
...
回答by ademar111190
I had the same problem, but to me the error was in the file Android.mk. I had it:
我遇到了同样的问题,但对我而言,错误出在文件 Android.mk 中。我有它:
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.
我希望这有帮助。
回答by DragonLord
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.
按照自动生成的 Studio 示例中提供的方式调用 extern "C",但忘记将文件的整个其余部分(包括以下函数)包装在 {} 括号中。只有第一个功能起作用。
回答by Megha
There is a cpp example under apps in ndk: https://github.com/android/ndk-samples/blob/master/hello-gl2/app/src/main/cpp/gl_code.cpp
在ndk的apps下有一个cpp例子:https: //github.com/android/ndk-samples/blob/master/hello-gl2/app/src/main/cpp/gl_code.cpp
回答by John Twigg
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.
另一个原因:在 android.mk 中使用 LOCAL_WHOLE_STATIC_LIBRARIES 而不是 LOCAL_STATIC_LIBRARIES。这会阻止库优化未使用的 API 调用,因为 NDK 无法从 Java 代码中检测到本机绑定的使用。
回答by oiyio
If your package name includes _ character, you should write 1(one) after _ character as shown below:
如果你的包名包含_字符,你应该在_字符后写1(一),如下所示:
MainActivity.java
主活动.java
package com.example.testcpp_2;
native-lib.cpp
本机-lib.cpp
JNICALL
Java_com_example_testcpp_12_MainActivity_stringFromJNI(
回答by MartinH
Use javah (part of Java SDK). Its the tool exactly for this (generates .h header from .class file).
使用 javah(Java SDK 的一部分)。它正是为此的工具(从 .class 文件生成 .h 头文件)。
回答by user2420449
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.
我尝试了上述所有解决方案,但没有人可以解决我的构建错误(jni java.lang.UnsatisfiedLinkError: No implementation found for...),最后我发现我忘记将我的 verify.cpp 源文件添加到 CMakeList.txt add_library 段(verify.cpp 是通过 Ctrl + Enter 快捷键自动生成的,也许是其他文件名),希望我的回答可以帮助一些人。
my build environment: Gradle + CMake
我的构建环境:Gradle + CMake
回答by Firas Shrourou
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
我遇到了同样的问题,在我的情况下,原因是我在包名称“RFID_Test”中有下划线,我重命名了包并且它起作用了。感谢用户1222021