"无法解析对应的JNI功能. Android Studio [英] " Cannot Resolve Corresponding JNI Function" Android Studio
问题描述
本机代码 native.c
The native code native.c
#include <string.h>
#include <stdio.h>
#include <jni.h>
jstring Java_com_lab5_oli_myapplication_MainActivity_helloWorld(JNIEnv* env,jobject obj)
{
return (*env)->NewStringUTF(env,"Hello world");
}
Android.mk文件
Android.mk file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:=ocrex
LOCAL_SRC_FILES:=ndkTest.c
include $(BUILD_SHARED_LIBRARY)
Application.mk文件
Application.mk file
APP_ABI := all
MainActivity中的代码
code in MainActivity
public native String helloWorld();
static{
System.loadLibrary("ocrex");
}
该方法被认为是在本机代码中声明的(在侧栏上注明)
The method is recognised to be declared in the native code(note on side bar)
推荐答案
首先,如果您使用的是Android Studio 2.2及更高版本,请使用Cmake,因为Android Studio的本机库默认构建工具是CMake.但是,如果您需要ndk-build,则android studio仍然支持ndk-build.
First if you are using android studio 2.2 and above use Cmake because Android Studio's default build tool for native libraries is CMake. But if you need the ndk-build android studio still supports the ndk-build.
1)将JNIEXPOT和JNICALL添加到本机方法中,并确保com_lab5_oli_myapplication是MainActivity类的程序包名称.
1) Add JNIEXPOT and JNICALL into the native method and make sure the com_lab5_oli_myapplication is the package name of the MainActivity class.
#include <string.h>
#include <stdio.h>
#include <jni.h>
JNIEXPORT jstring JNICALL Java_com_lab5_oli_myapplication_MainActivity_helloWorld(JNIEnv* env,jobject obj)
{
return (*env)->NewStringUTF(env,"Hello world");
}
2)并且在Android.mk文件中更改源名称,您的c ++名称是native.c,但在Android.mk文件中使用ndkTest.c文件名.
2) And in the Android.mk file change the source name your c++ name is native.c but in the Android.mk file you used ndkTest.c file name.
LOCAL_SRC_FILES:=ndkTest.c
//change it to
LOCAL_SRC_FILES:=native.c
最后,您必须将gradle链接到本机库中. 1)如果您拥有android studio 2.2及更高版本,请右键单击该应用程序,然后找到带有gradle的Link c ++项目.如果使用的是ndk-build,则选择Android.mk文件;如果使用的是Cmake,则选择插入CmakeLists的地址. 2)您还可以手动配置gradle以包括本地库.您需要将externalNativeBuild块添加到模块级别的build.gradle文件中,并使用cmake或ndkBuild块对其进行配置: 如果您使用的是cmake
Finally you have to link the gradle into native library. 1) If you have android studio 2.2 and above right click on the app and there is Link c++ project with gradle. If you are using ndk-build then choose the Android.mk file if you are using Cmake build choose the insert the address of the CmakeLists. 2) You can also manually configure gradle to include the native library.You need to add the externalNativeBuild block to your module-level build.gradle file and configure it with either the cmake or ndkBuild block: If you are using cmake
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
如果您使用的是ndk-build
And if you are using ndk-build
externalNativeBuild {
// Encapsulates your CMake build configurations.
ndkBuild {
// Provides a relative path to your to the Android.mk build script.
path "Android.mk"
}
}
有关android中cmake和ndk的详细信息,请使用此和此.
For detail information about cmake and ndk in android use this and this.
这篇关于"无法解析对应的JNI功能. Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!