我在使用原生 OpenCv 2.4.11(3.0.0 也可以)和 Android Studio 1.3+ 以及新的 ndk 支持时遇到问题。所有关于 .mk 文件的教程,但我想将它与新的实验性 gradle 一起使用。
在 Android Studio 中使用 Kiran answer OpenCV
我在 Java 中实现了正常工作。我也改变了
gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
build.gradle(application):
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
构建.gradle(模块):
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.0"
defaultConfig.with {
applicationId = "android.overloaded.nativetestv4"
minSdkVersion.apiLevel = 18
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
android.ndk{
moduleName = "mymodule"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include")}".toString()
cppFlags += "-I${file("C:/DevAndroid/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString()
ldLibs += ["android", "EGL", "GLESv2", "dl", "log", "z"] //added z
stl = "stlport_static"//"gnustl_shared" changed to stlport static
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.pro')
}
}
android.productFlavors { //added all productFlavors code
create("arm") {
ndk.with {
abiFilters += "armeabi"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
}
}
create("armv7") {
ndk.with {
abiFilters += "armeabi-v7a"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath+"\\src\\main\\jniLibs\\armeabi-v7a\\" //"-L" +
ldLibs += libsDir + "libnative_camera_r4.3.0.so"
ldLibs += libsDir + "libopencv_contrib.a"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_info.so"
ldLibs += libsDir + "libopencv_java.so"
ldLibs += libsDir + "libopencv_legacy.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_ts.a"
}
}
create("fat") {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile project(':openCVLibrary2411')
}
通过这些更改,我可以在 jni 文件夹下的 c++ 文件中调用我的本机虚拟函数。CLion#include <opencv2/core/core.hpp>
也支持其他人......当我声明 cv::Mat mat; 给我这个错误:
Linker failed while linking libmymodule.so failed.
C:\DevAndroid\OpenCV-android-sdk\sdk\native\jni\include/opencv2/core/mat.hpp:278: error: undefined reference to 'cv::fastFree(void*)'
C:\DevAndroid\OpenCV-android-sdk\sdk\native\jni\include/opencv2/core/mat.hpp:367: error: undefined reference to 'cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
C++代码:
#include <jni.h>
#include <vector>
#include <string>
#include "helpers.h"
#include <opencv2/core/core.hpp> //This is supported by CLion
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
extern "C" {
JNIEXPORT jstring JNICALL
Java_android_overloaded_nativetestv4_MainActivity_FuncV1(JNIEnv *env, jclass type) {
cv::Mat mat; //causing error
string s = "Cpp v1 - succ ";
return env->NewStringUTF(s.data());
}
}
更新
- 向 ldLibs 添加了“z”
- 添加了 android.productFlavors 块
现在编译成功,应用程序启动第二次,然后崩溃并显示此消息
事件日志:
IllegalArgumentException: Error while starting native debug session: Unable to find process for android.overloaded.nativetestv4 on device huawei-huawei_y530_u00-64a6513ccd8e
调试信息:
Restart ADB integration and try again
Waiting for process: android.overloaded.nativetestv4
DEVICE SHELL COMMAND: run-as android.overloaded.nativetestv4 mkdir -p /data/data/android.overloaded.nativetestv4/lldb/bin
DEVICE SHELL COMMAND: cat /data/local/tmp/lldb-server | run-as android.overloaded.nativetestv4 sh -c 'cat > /data/data/android.overloaded.nativetestv4/lldb/bin/lldb-server; chmod 700 /data/data/android.overloaded.nativetestv4/lldb/bin/lldb-server'
DEVICE SHELL COMMAND: rm /data/local/tmp/lldb-server
DEVICE SHELL COMMAND: cat /data/local/tmp/start_lldb_server.sh | run-as android.overloaded.nativetestv4 sh -c 'cat > /data/data/android.overloaded.nativetestv4/lldb/bin/start_lldb_server.sh; chmod 700 /data/data/android.overloaded.nativetestv4/lldb/bin/start_lldb_server.sh'
DEVICE SHELL COMMAND: rm /data/local/tmp/start_lldb_server.sh
Now Launching Native Debug Session
Error while starting native debug session: java.lang.IllegalArgumentException: Unable to find process for android.overloaded.nativetestv4 on device huawei-huawei_y530_u00-64a6513ccd8e
日志:
A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 3077 (ed.nativetestv4)