Luca96 / android-face-landmarks

Android app that localizes facial landmarks in nearly real-time
MIT License
68 stars 27 forks source link

Build exception failure #14

Closed Spike87 closed 4 years ago

Spike87 commented 4 years ago

The app fails to build with the following error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process C:\Users\Spike\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe with arguments {--build D:\ProgettiAndroid\apps\android-face-landmarks-master\app\.externalNativeBuild\cmake\debug\x86_64 --target native-lib}
  [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
  [2/2] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libnative-lib.so
  FAILED: ../../../../build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so 
  cmd.exe /C "cd . && C:\Users\Spike\AppData\Local\Android\Sdk\ndk\21.1.6352462\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=x86_64-none-linux-android21 --gcc-toolchain=C:/Users/Spike/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/Spike/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -frtti -fexceptions -std=c++11 -frtti -fexceptions -fexceptions -std=c++11 -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  ../../../../src/main/cppLibs/opencv/x86_64/libopencv_java4.so ../../../../src/main/cppLibs/dlib/lib/x86_64/libdlib.so -llog -latomic -lm && cd ."
  D:/ProgettiAndroid/libs/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/mat.inl.hpp:501: error: undefined reference to 'cv::error(int, cv::String const&, char const*, char const*, int)'
  D:/ProgettiAndroid/libs/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/mat.inl.hpp:514: error: undefined reference to 'cv::error(int, cv::String const&, char const*, char const*, int)'
  ../../../../src/main/cppLibs/dlib/include\dlib/opencv/cv_image.h:37: error: undefined reference to '_IplImage::_IplImage(cv::Mat const&)'
  D:/ProgettiAndroid/libs/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:602: error: undefined reference to 'cv::String::allocate(unsigned long)'
  D:/ProgettiAndroid/libs/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv::String::deallocate()'
  D:/ProgettiAndroid/libs/OpenCV-android-sdk/sdk/native/jni/include\opencv2/core/cvstd.hpp:656: error: undefined reference to 'cv::String::deallocate()'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s

What I did:

Luca96 commented 4 years ago

Could you post your CMakeLists.txt as well as app/gradle.build?

Spike87 commented 4 years ago

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"
    }
}
Luca96 commented 4 years ago

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.

Spike87 commented 4 years ago

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?

Luca96 commented 4 years ago

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.

Luca96 commented 4 years ago

@Spike87, have you solved the issue?

YasserHawass commented 1 year ago

Yes, it fixed it Luca. Thanks for your hardwork sir.