我应该说我是第一次使用ndk在android studio中。
环境是 Windows 10 操作系统上的 Android Studio 3.1.4 和 OpenCV 3.4 。
我使用有关“ Facetracker”链接位于以下的opencv的开源软件
https://github.com/kylemcdonald/FaceTracker
我更改了一些代码并想在android studio上构建
所以我草拟了CMakeList.txt
,Android.mk
,Application.mk
,build.gradle
。
与gradle同步可以正常工作,但是在生成,生成签名的APK时出现以下错误。
Build command failed.
Error while executing process C:\Users\Duru\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\ProgramFiles\Android\Projects\OpencvTest\app\.externalNativeBuild\cmake\release\x86_64 --target PAW}
[1/1] **Linking CXX shared library** ..\..\..\..\build\intermediates\cmake\release\obj\x86_64\libPAW.so
FAILED: cmd.exe /C "cd . && C:\Users\Duru\AppData\Local\Android\Sdk\ndk-bundle\toolchains\x86_64-4.9\prebuilt\windows-x86_64\bin\x86_64-linux-android-g++.exe --sysroot=C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/sysroot/usr/include/x86_64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -std=gnu++11 -O2 -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libPAW.so -o ..\..\..\..\build\intermediates\cmake\release\obj\x86_64\libPAW.so CMakeFiles/PAW.dir/src/main/jni/src/lib/PAW.cc.o -latomic -lm "C:/Users/Duru/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86_64/libgnustl_static.a" && cd ."
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv::String::deallocate()'
**D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/cvstd.hpp:648: error: undefined reference to 'cv::String::deallocate()'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2686: error: undefined reference to 'cv::MatConstIterator::seek(long, bool)'
D:\ProgramFiles\Android\Projects\OpencvTest\app\src\main\jni\src\lib/PAW.cc:146: error: undefined reference to 'cv::Mat::zeros(int, int, int)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2591: error: undefined reference to 'cv::MatConstIterator::seek(int const*, bool)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2591: error: undefined reference to 'cv::MatConstIterator::seek(int const*, bool)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:2686: error: undefined reference to 'cv::MatConstIterator::seek(long, bool)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:717: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp:717: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
D:\ProgramFiles\Android\Projects\OpencvTest\app\src\main\jni\src\lib/PAW.cc:167: error: undefined reference to 'cv::remap(cv::_InputArray const&, cv::_OutputArray const&, cv::_InputArray const&, cv::_InputArray const&, int, int, cv::Scalar_<double> const&)'
collect2.exe: error: ld returned 1 exit status**
ninja: build stopped: subcommand failed.
我的android.mk
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
include $(CLEAR_VARS)
CVROOT := D:/ProgramFiles/Android/Projects/OpenCV-android-sdk/sdk/native/jni
LOCAL_MODULE := FaceTracker
LOCAL_SRC_FILES := libFaceTracker.a #$(wildcard /src/lib/*.cc)
#LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -g -O3 -ffast-math -funroll-loops -march=armv7-a -mfloat-abi=softfp -mfpu=neon
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
OPENCV_INSTALL_MODULES:=on
include $(CVROOT)/OpenCV.mk
LOCAL_MODULE := opencv_java3
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_C_FLAGS+= -DNDEBUG -Wall -pedantic -g -O3 -ffast-math -funroll-loops -march=armv7-a -mfloat-abi=softfp -mfpu=neon
#LOCAL_C_INCLUDES+= $(NDK_APP_PROJECT_PATH)/stasm4.1.0/stasm
#LOCAL_SRC_FILES += $(NDK_APP_PROJECT_PATH)/libstasm.a
#LOCAL_SRC_FILES += $(wildcard $(NDK_APP_PROJECT_PATH)/stasm/*.cpp)
LOCAL_LDLIBS += -lm -llog -landroid
#LOCAL_SHARED_LIBRARIES += libandroid
LOCAL_STATIC_LIBRARIES += android_native_app_glue
LOCAL_STATIC_LIBRARIES += FaceTracker
LOCAL_ARM_NEON := true
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
endif
我的build.gradle
import org.apache.tools.ant.taskdefs.condition.Os
def getNdkBuildPath() {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def command = properties.getProperty('ndk.dir')
if(Os.isFamily(Os.FAMILY_WINDOWS)) {
command +="\\ndk-build.cmd"
} else {
command +="/ndk-build"
}
return command
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.duru.opencvtest"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
arguments '-DANDROID_TOOLCHAIN=gcc',
'-DANDROID_STL=gnustl_static'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
implementation project(':openCVLibrary342')
}
我的application.mk
APP_PLATFORM := android-28
APP_ABI := armeabi-v7a
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++11
APP_OPTIM := release
和CMakeList.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.4.1)
set(pathOPENCV D:/ProgramFiles/Android/Projects/OpenCV-android-sdk)
set(pathPROJECT D:/ProgramFiles/Android/Projects/OpencvTest)
set(pathLIBOPENCV_JAVA ${pathPROJECT}/app/src/main/JniLibs/${ANDROID_ABI}/libopencv_java3.so)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathOPENCV}/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).
src/main/jni/native-lib.cpp)
add_library( lib_opencv SHARED IMPORTED )
add_library( Tracker SHARED src/main/jni/src/lib/Tracker.cpp )
add_library( PDM SHARED src/main/jni/src/lib/PDM.cc )
add_library( PAW SHARED src/main/jni/src/lib/PAW.cc )
add_library( Patch SHARED src/main/jni/src/lib/Patch.cc )
add_library( IO SHARED src/main/jni/src/lib/IO.cc )
add_library( FDet SHARED src/main/jni/src/lib/FDet.cc )
add_library( FCheck SHARED src/main/jni/src/lib/FCheck.cc )
add_library( CLM SHARED src/main/jni/src/lib/CLM.cc )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathLIBOPENCV_JAVA})
# 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
Tracker
PDM
PAW
Patch
IO
FDet
FCheck
CLM
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
opensource由7个标头,6个.cc文件和.cpp文件组成。