我是android编程的新手,所以我试着只写一个简单的程序,可以从java调用jni文件夹的原生cpp文件方法。。尽管我尝试过运行OPENCV示例,并尝试过相同的调用方式,但没有一个m成功,甚至OPENCV教程2-混合处理都失败了。它在我的模拟器中启动并崩溃。
com_rukna_myfirsandroidimageapp_MainActivity.h
extern "C"{
* Class: com_rukna_myfirstandroidimageapp_MainActivity
* Method: getStringfromNative
* Signature: (I)Ljava/lang/String;
JNIEXPORT jstring JNICALL Java_com_rukna_myfirstandroidimageapp_MainActivity_getStringfromNative
(JNIEnv *, jobject, jint);
}
CPPCALL.cpp
#include <com_rukna_myfirstandroidimageapp_MainActivity.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
JNIEXPORT jstring JNICALL Java_com_rukna_myfirstandroidimageapp_MainActivity_getStringfromNative (JNIEnv *env , jobject obj, jint src)
{
return (env)->NewStringUTF("HELLO from JNI CPPCALL !!");
}
使用命令提示符,我在MYProject/bin/classpath/javap-s-p MainActivity中也使用了这个
----我有很多这样的东西---
public native java.lang.String getStringfromNative(int);
descriptor: (I)Ljava/lang/String;
但当我从MainActivity调用此方法时,如下所示
package com.rukna.myfirstandroidimageapp;
import library
public class MainActivity extends Activity {
/* some declarations and other functions */
int a = 0;int b = 1;
String s = getStringfromNative( a); /*---> error when call for the cpp method*/
_field.setText(s);
_field.setSelection(_field.getText().toString().length());
public native String getStringfromNative(int a);
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
else{
System.loadLibrary("MyLib");}
}
}
运行应用程序时出现以下错误:
java.lang.UnsatisfiedLinkError: Native method not found: com.rukna.myfirstandroidimageapp.MainActivity.getStringfromNative:(I)Ljava/lang/String;
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=on
#OPENCV_LIB_TYPE:=SHARED
#OPENCV_LIB_TYPE:=STATIC
# OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=STATIC
include D:/OpenCV-2.4.11-android-sdk/sdk/native/jni/OpenCV.mk
#LOCAL_MODULE := CLAHE_test
LOCAL_MODULE := MyLib
LOCAL_SRC_FILES := CPPCALL.cpp
#LOCAL_SRC_FILES := CLAHE_test.cpp
#LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := x86
APP_PLATFORM := android-19
当我检查myproject.apk时,我在MyFirstAndroidImageApp.apk\lib\x86下找到了
libhydrogen.so
libimageutils.so
liblept.so
libMyLib.so
libnative_camera_r2.3.3.so
libnative_camera_r3.0.1.so
libnative_camera_r4.0.3.so
libnative_camera_r4.1.1.so
libnative_camera_r4.2.0.so
libnative_camera_r4.3.0.so
libnative_camera_r4.4.0.so
libopticalflow.so
libtess.so
项目属性->C/C++通用->路径和符号->包含->GNU C++
${NDKROOT}/platforms/android-9/arch-arm/usr/include
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.6/include
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include
${NDKROOT}/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.8/include
D:/OpenCV-2.4.11-android-sdk/sdk/native/jni/include
D:android-ndk-r10dplatformsandroid-19arch-x86usrinclude
帮助我,因为当我在没有任何参数的情况下调用相同的方法时,它会正常运行,我不明白为什么。。。。。我需要给构造函数一种东西吗?如果是,那么在哪里以及如何?
您是否尝试在Application.mk中修改APP_ABI:=armeabi-v7a。此外,如果成功编译了cpp,您应该在libs/armeabi-v7a 中找到这样的文件