Closed Spike87 closed 4 years ago
Could you post your CMakeLists.txt
as well as app/gradle.build
?
Thank you for your quick reply. Here they are: CMakeLists.txt
# 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.5.1)
# Configure build library name.
set(TARGET_NAME native-lib)
# Build project shared lib
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -std=c++11")
# Path to project: REPLACE WITH YOUR PATH!
set(PROJECT_PATH D:/ProgettiAndroid/apps/android-face-landmarks-master)
# Configure import libs: MAKE SURE TO HAVE A 'cppLibs' DIRECTORY"
set(LIB_DIR ${CMAKE_SOURCE_DIR}/src/main/cppLibs)
# ------------------------------------------------------------------
# -- OPENCV
# ------------------------------------------------------------------
# Path to OpenCV: REPLACE WITH YOUR PATH!
set(OPENCV_PATH D:/ProgettiAndroid/libs/OpenCV-android-sdk)
# Make directories for Opencv
file(MAKE_DIRECTORY ${LIB_DIR}/opencv)
file(MAKE_DIRECTORY ${LIB_DIR}/opencv/${ANDROID_ABI})
add_library(lib_opencv SHARED IMPORTED)
# sets the location of the prebuilt opencv .so
set_target_properties( lib_opencv
PROPERTIES IMPORTED_LOCATION
${LIB_DIR}/opencv/${ANDROID_ABI}/libopencv_java4.so )
include_directories(${OPENCV_PATH}/sdk/native/jni/include)
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# -- DLIB
# ------------------------------------------------------------------
set(DLIB_PATH ${LIB_DIR}/dlib)
# Make directories for Dlib
file(MAKE_DIRECTORY ${LIB_DIR}/dlib)
file(MAKE_DIRECTORY ${DLIB_PATH}/include)
file(MAKE_DIRECTORY ${DLIB_PATH}/lib)
file(MAKE_DIRECTORY ${DLIB_PATH}/lib/${ANDROID_ABI})
add_library(dlib SHARED IMPORTED)
# sets the location of the prebuilt dlib .so
set_target_properties( dlib
PROPERTIES IMPORTED_LOCATION
${DLIB_PATH}/lib/${ANDROID_ABI}/libdlib.so )
include_directories(${DLIB_PATH}/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.
${TARGET_NAME}
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
target_include_directories( ${TARGET_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/include
${DLIB_PATH}/include )
# 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( # Specifies the target library.
native-lib
lib_opencv
dlib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
and app/gradle.build
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.dev.anzalone.luca.facelandmarks"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
externalNativeBuild {
cmake {
cFlags "-O3"
cppFlags "-std=c++11 -frtti -fexceptions"
arguments "-DANDROID_PLATFORM=android-16",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_CPP_FEATURES=rtti exceptions"
}
}
sourceSets {
main {
jniLibs.srcDirs = [
"src/main/cppLibs/dlib/lib",
"src/main/cppLibs/opencv"
]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
packagingOptions {
pickFirst "**/libc++_shared.so"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.27.0-eap13'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'org.apache.commons:commons-compress:1.16'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
kotlin {
experimental {
coroutines "enable"
}
}
It seems everything fine... Have you downloaded (and unzipped, of course) OpenCV within D:/ProgettiAndroid/libs/OpenCV-android-sdk
? (but I guess you've already done it). So, which version of OpenCV do you have? Mine is 4.0.1
.
Mmm... I actually copied the opencv-android-sdk in that folder (version 3.4.3
, which should be the last one). Should I copy OpenCV itself?
Hi! Yes, you should copy the entire opencv folder. The important thing is to match this path, in your should be case D:/ProgettiAndroid/libs/OpenCV-android-sdk/sdk/native/jni/include
. But if you copied only the sdk
folder instead, you could fix this by changing line 38 as follows (i.e. remove /sdk
from the path):
include_directories(${OPENCV_PATH}/native/jni/include)
where OPENCV_PATH
is D:/ProgettiAndroid/libs/OpenCV-android-sdk
.
Anyway, the latest opencv release is the 4.3.0 as you can see here. I suggest you to use the version 4.0.1. as is the same used here, and eventually experiment with the latest release. In general, I don't recommend version 3.4.3
or below because there are some c++ compatibility issues.
This should solve the issue. Let me know if you still have problems.
@Spike87, have you solved the issue?
Yes, it fixed it Luca. Thanks for your hardwork sir.
The app fails to build with the following error:
What I did: