问题
I need to use OpenCV in Android Studio 3.1.2. I have the last version at 05/20/2018 of this IDE and the NDK (17.0.4754217).
I read this post talking about it, and says something about a problem with the NDK version 16, but is not the case.
Most of the guides are for older versions of Android (I saw guides talking about Eclipse IDE for Android development) but no one with the last version or one closest, and it is a problem, since the latest version of AS causes many errors with older projects or the way in which they are made, so they make these guides obsolete.
Maybe is a problem caused by the fact that i don't kown how to work with the NDK and CMake, so i would appreciate if someoanewho knows how or had tried to do this, would tell me how to add OpenCV to Android 3.1.2, and maybe this post can ben helpful to another users for future versions of NDK and AS.
TY so much.
回答1:
1. Make sure you have Android SDK up to date, with NDK installed
2. Download latest OpenCV SDK for Android from OpenCV.org and decompress the zip file.
3. Create a new Android Studio project
- Check Include C++ Support
- Choose empty Activity
- In C++ Support, you can check -fexceptions and -frtti
4. Import OpenCV library module
- New -> Import Module
- Choose the YOUR_OPENCV_SDK/sdk/java folder
- Unckeck replace jar, unckeck replace lib, unckeck create gradle-style
5. Set the OpenCV library module up to fit your SDK
app/build.gradle
opencv/build.gradle
Edit openCVLibrary/build.gradle to match your app/build.gradle e.g:
compileSdkVersion 27
defaultConfig {
minSdkVersion 19
targetSdkVersion 27
}
6. Add OpenCV module dependency in your app module
File -> Project structure -> Module app -> Dependencies tab -> New module dependency -> choose OpenCV library module
7. Make a jni folder by right clicking on app/src/main and click Change Folder Location box after that rename folder fron jni to jniLibs
1st step
2nd step
3rd step
8. Copy all files from your opencv directory YOUR_OPENCV_SDK/sdk/native/libs that you have downloaded and paste them in jniLibs folder
1st step
2nd step
9. Set the app build.gradle
Add abiFilters
externalNativeBuild { cmake { cppFlags "-frtti -fexceptions" abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' } }
10. Configure the CMakeLists.txt file
Copy these three lines and paste after the cmake_minimum_required
include_directories(YOUR_OPENCV_SDK/sdk/native/jni/include) add_library( lib_opencv SHARED IMPORTED ) set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
Go to the end of CMakeLists.txt and write lib_opencv to the target_link_libraries list
All is done now enjoy coding with opencv...
回答2:
Assuming you are using Android Studio, then the initial NDK integration is much easier now then it was previously.
You can simply create a new project in Android Studio, specify that you want NDK support in it and just add a basic activity.
Adding OpenCV NDK (i.e. C++ support) to this project is still tricky - as you say most of the guides and instructions on the Android OpenCV pages are eclipse based at the moment. I went through many blogs, guides etc fro OpenCv on Android Studio with NDK and the one which worked for me most recently (this week) and which seems to be being kept up to date, is the one in this answer here:
- https://stackoverflow.com/a/43886764/334402
I would encourage you to try this approach and to share any issues you have in the comments so that it is updated and kept current, as changes in Android and in OpenCV mean its important to have well supported reference instructions.
来源:https://stackoverflow.com/questions/50433813/how-to-add-opencv-lib-to-as3-1-2-and-ndk-17-0