Issue
I'm trying to setup OpenCv 3.4.14 on Android Studio (4.0.0) project which has c++ support from the beginning, (Native project). I'm getting this error over and over again. I searched the web and didn't found any useful solution for that problem, please help.
I followed this video for setting up OpenCv on Android Studio. It works well in the video, but for some reason it doesn't work for me.
Note:
I've tried to setup OpenCv on projects that don't include c++, and it worked without any problem. What makes me think that the problem here is somewhere in the CMakeLists.txt file.
So i will share here all what i have (after following the video above):
Project structure:
build.gradle (:app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.artest"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation project(path: ':openCVLibrary3414')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
build.gradle (:openCVLibrary3414):
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
CMakeLists.txt:
set(pathToProject E:/ArTest)
set(pathToOpenCv C:/OpenCV-android-sdk)
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathToOpenCv}/sdk/native/jni/include)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
add_library( lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( native-lib ${log-lib} lib_opencv)
HomeActivity.java:
package com.artest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.opencv.android.OpenCVLoader;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
System.loadLibrary("opencv_java3");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
if (!OpenCVLoader.initDebug()) {
tv.setText(tv.getText() + ", OpenCv is not working.");
}
else {
tv.setText(tv.getText() + "\n GREAT!!! OpenCv is working.");
tv.setText(tv.getText() + "\n" + validate(0L, 0L));
}
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String validate(long matAddrGr, long matAddrRgba);
}
native-lib.cpp:
#include <jni.h>
#include <string>
#include <opencv2/core.hpp>
extern "C" JNIEXPORT jstring JNICALL
Java_com_artest_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */)
{
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C"
jstring Java_com_artest_MainActivity_validate(
JNIEnv* env,
jobject, jlong addrGray, jlong addrRgba)
{
cv::Rect();
cv::Mat();
std::string hello = "Hello from validate";
return env->NewStringUTF(hello.c_str());
}
Error:
More than one file was found with OS independent path 'lib/arm64-v8a/libopencv_java3.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
Build results:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:mergeDebugNativeLibs'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
> More than one file was found with OS independent path 'lib/arm64-v8a/libopencv_java3.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
Solution
To anyone who struggle configuring OpenCV in a native c++ Android projects, I've found this video useful, it is the best guide for setting up OpenCV on Android. All the work is done only by the CMake file. It worked for me.
Answered By - Daniel Rotnemer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.