Android Studio 不在 apk 中包含 .so 库:
Android Studio is not including the .so library in the apk:
我是这样调用本地方法的:
public native String callTest(String mCurrentPhotoPath);
static {
System.loadLibrary("jni_part");
}
这是我的 cpp
#include <jni.h>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
const char* toGray(const char* path){
IplImage* img = NULL;
if ((img = cvLoadImage(path))== 0){
printf("cvLoadImage failed\n");
}
return path;
}
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_MainActivity_callTest(JNIEnv* env, jobject, jstring param) {
const char* path = env->GetStringUTFChars(param, 0);
return env->NewStringUTF(toGray(path));
}
}
我试过将 toGray 设置在 extern "C" 之前和之后(当我第一次在 extern "C" 之前声明它然后在下面设置它的功能之后)。
崩溃错误:
03-05 15:12:37.930 13775-13775/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Couldn't load jni_part from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example-1, /vendor/lib, /system/lib]]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:359)
at java.lang.System.loadLibrary(System.java:514)
at com.example.MainActivity.<clinit>(MainActivity.java:56)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access0(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
include /home/radibg2/OpenCV-2.4.10-android-sdk/sdk/native/jni//OpenCV.mk
LOCAL_MODULE := jni_part
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
问题是 NDK 在 libs 文件夹中生成库,但它们必须在 jniLibs 文件夹中。
我是这样调用本地方法的:
public native String callTest(String mCurrentPhotoPath);
static {
System.loadLibrary("jni_part");
}
这是我的 cpp
#include <jni.h>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
const char* toGray(const char* path){
IplImage* img = NULL;
if ((img = cvLoadImage(path))== 0){
printf("cvLoadImage failed\n");
}
return path;
}
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_MainActivity_callTest(JNIEnv* env, jobject, jstring param) {
const char* path = env->GetStringUTFChars(param, 0);
return env->NewStringUTF(toGray(path));
}
}
我试过将 toGray 设置在 extern "C" 之前和之后(当我第一次在 extern "C" 之前声明它然后在下面设置它的功能之后)。 崩溃错误:
03-05 15:12:37.930 13775-13775/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Couldn't load jni_part from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example-1, /vendor/lib, /system/lib]]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:359)
at java.lang.System.loadLibrary(System.java:514)
at com.example.MainActivity.<clinit>(MainActivity.java:56)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2223)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access0(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
include /home/radibg2/OpenCV-2.4.10-android-sdk/sdk/native/jni//OpenCV.mk
LOCAL_MODULE := jni_part
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
问题是 NDK 在 libs 文件夹中生成库,但它们必须在 jniLibs 文件夹中。