【发布时间】:2017-07-17 09:18:27
【问题描述】:
我正在使用 Android Studio 2.2.3 和 OpenCV SDK。我从默认的 Hello world 应用程序开始(选择了C++ 选项)。该项目被配置(默认)使用CMake。
OpenCV SDK 使用基于 .mk 的构建系统。我为我的应用程序修改了build.gradle,如下(注释3行,为ndk-build添加3行):
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "in.ac.iitb.sc.arms.sample"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
// cmake {
// path "CMakeLists.txt"
// }
ndkBuild{
path 'src/main/cpp/Android.mk'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.1'
testCompile 'junit:junit:4.12'
}
我当前的目录树看起来像:
APP
├── app.iml
├── build.gradle
├── CMakeLists.txt
├── libs
├── proguard-rules.pro
└── src
├── androidTest
│ └── java
│ └── test
│ └── ...
├── main
│ ├── AndroidManifest.xml
│ ├── cpp
│ │ ├── Android.mk
│ │ ├── Application.mk
│ │ └── native-lib.cpp
│ ├── java
│ │ └── test
│ │ └── sc
│ │ └── arms
│ │ └── sample
│ │ └── MainActivity.java
Android.mk 的样子
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= /home/homer/Desktop/OpenCV-android-sdk
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(OPENCVROOT)/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE:=MyLibs
include $(BUILD_SHARED_LIBRARY)
另外我已经将Application.mk文件放在cpp目录下,内容如下
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
而且 Android 也无法解析 stringFromJNI() 函数,如图所示
当我构建应用程序时,它没有显示错误。当我运行该应用程序时,它会立即崩溃并出现 java.lang.UnsatisfiedLinkError: couldn't find "libnative-lib.so" 错误。
libnative-lib.so 应该是由ndk-build 生成的,它没有被构建。我怀疑这是与 Gradle 配置相关的问题。
编辑:
这里是native-lib.cpp的内容:
#include <jni.h>
#include <string>
extern "C"
jstring
Java_e2016_iitb_1projects_vvy_sample_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
【问题讨论】:
标签: android opencv android-ndk java-native-interface ndk-build