NDK环境下载,在AS上安装或者官网
然后配置系统环境变量,如:D:\AS\ndk\21.0.6113669;
最好官网下,经过多个版本的测试,最终能解决我问题的还是r16,可是我这两天用方式一也试了r16,搞半天还是不行,周末一搞就OK了,这是天意?!!!NONONO,记得相应的配置的要Sync。
还有一些c++的标准库NDK编译就是有这样那样的兼容问题,不是缺这个就是缺那,即使将header file copy 过去了,还是会有这样那样不符合规范的问题。比如io.h,filesystem,direct.h,fstream 就是不支持能拿它怎样,天真以为2020了,也该…我试错试了很久才晓得。好多c++实现起来就费劲,实现完了发现还不能迁移。
package com.example.hell;
public class MyJni {
static {
System.loadLibrary("MyJni");
}
public native static String getString();
}
javah -jni -classpath F:\xxxxx\app\build\intermediates\javac\debug\classes com.example.hete.Heterotropia
JNIEXPORT jstring JNICALL Java_com_example_hell_MyJni_getString
(JNIEnv *env, jclass jz){
return (env)->NewStringUTF("this is the first time for me to use jni");
}
LOCAL_PATH:= $(call my-dir)
$(info, $(LOCAL_PATH))
# ======================================
# 加载libOpenCV
# ======================================
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include D:\opencv-3.4.6-android-sdk\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk
#include $(CLEAR_VARS)
LOCAL_MODULE := Heterotropia
LOCAL_C_INCLUDES += D:\opencv-3.4.6-android-sdk\OpenCV-android-sdk\sdk\native\jni\include
LOCAL_C_INCLUDES += D:\opencv-3.4.6-android-sdk\OpenCV-android-sdk\sdk\native\jni\include\opencv
LOCAL_C_INCLUDES += D:\opencv-3.4.6-android-sdk\OpenCV-android-sdk\sdk\native\jni\include\opencv2
LOCAL_SRC_FILES += Heterotropia.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
#LOCAL_CFLAGS += -fPIC -pie -fPIE -Wno-deprecated-declarations
LOCAL_LDLIBS += -ldl -llog
#LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(BUILD_SHARED_LIBRARY)
#APP_PLATFORM := android-22
APP_ABI := all
#APP_ABI := arm64-v8a
APP_STL:= gnustl_static
#APP_STL := c++_shared
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions
前面步骤跟NKD差不多,只不过需要将之前jni目录下的.cpp放在…\app\src\main\cpp目录下。然后在CmakeLists.txt里配置相应的东西。(具体以后再补充)
SDK直接Android studio 下载的
NKD配置:官网下载ndk-r16在local.properties
sdk.dir=D\:\\AS\\sdk
ndk.dir=D\:\\AS\\android-ndk-r16b-windows-x86_64\\android-ndk-r16b
CmakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(pathToOpenCV D:/opencv-3.4.6-android-sdk/OpenCV-android-sdk)
include_directories(${pathToOpenCV}/sdk/native/jni/include)
add_library(lib_opencv SHARED IMPORTED)
add_library(lib-handdev SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToOpenCV}/sdk/native/libs/${ANDROID_ABI}/libopencv_java3.so)
set_target_properties(lib-handdev PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libhanddev_algorithms.so)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_library( # Sets the name of the library.
# native-lib
Heterotropia
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# native-lib.cpp
Heterotropia.cpp)
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)
target_link_libraries( # Specifies the target library.
# native-lib
Heterotropia
# Links the target library to the log library
# included in the NDK.
lib_opencv
# lib-handdev
${log-lib})
配置APP中的build.gradle
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
// cppFlags ""
// cppFlags "-std=c++11 -frtti -fexceptions"
// arguments '-DANDROID_STL=c++_shared'
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'arm64-v8a','armeabi-v7a', 'x86', 'x86_64'
arguments "-DANDROID_STL=gnustl_static"
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
// abiFilters 'armeabi-v7a', 'x86'
abiFilters 'arm64-v8a','armeabi-v7a', 'x86', 'x86_64'
stl = "gnustl_static"
}
}
如上配置后,build下就可以在目录app\build\intermediates\cmake\debug\obj
下看到不同生成的so
其他步骤与前面相同
这里需要引入的是开源库 wavelib目录
根目录的CMakeLists.txt
需要添加如下:
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/wavelib)
target_link_libraries(
oct
wavelib
${log-lib})
#cpp/CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/wavelib)
add_library( # Sets the name of the library.
oct
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
oct.cpp
)
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)
target_link_libraries( # Specifies the target library.
oct
wavelib
# Links the target library to the log library
# included in the NDK.
${log-lib})
#cpp/wavelib/CMakeLists.txt
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(SOURCE_FILES conv.c
cwt.c
cwtmath.c
hsfft.c
real.c
wavefilt.c
wavefunc.c
wavelib.c
wtmath.c
)
set(HEADER_FILES conv.h
cwt.h
cwtmath.h
hsfft.h
real.h
wavefilt.h
wavefunc.h
wtmath.h
)
add_library(wavelib STATIC ${SOURCE_FILES} ${HEADER_FILES})
set_property(TARGET wavelib PROPERTY FOLDER "lib")
target_include_directories(wavelib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}../header)
target_link_libraries(wavelib ${log-lib} )######
在这里记录下生成静态 .a 的CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
#################################
## 项目信息
##project (as_oct)
## 查找当前目录下的所有源文件
## 并将名称保存到 DIR_SRCS 变量
#aux_source_directory(. DIR_SRCS)
## 添加头文件路径
#include_directories(./wavelib)
## 添加 math 子目录
#add_subdirectory(wavelib)
## 指定生成目标
#add_executable(as_oct dwt.cpp)
## 添加链接库
##target_link_libraries(as_oct dwt)
##################################
#include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)
#set(jnilibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${jnilibs}/${ANDROID_ABI})
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -DMGULK_LOG_STDERR=1 -Wall -Wextra -Wnon-virtual-dtor -g")
#ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/wavelib)
###################################
# 引入 .a 文件
add_library(wavelib STATIC IMPORTED )
set_target_properties(wavelib PROPERTIES IMPORTED_LOCATION "F:/xxxxxt/app/build/intermediates/cmake/debug/obj/${ANDROID_ABI}/libwavelib.a")
###################################
#include_directories(${CMAKE_SOURCE_DIR}/wavelib)
#ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/wavelib)
###################################
add_library( # Sets the name of the library.
oct
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
oct.cpp
)
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)
target_link_libraries( # Specifies the target library.
oct
wavelib
# Links the target library to the log library
# included in the NDK.
${log-lib})
I1.打包的so有问题 undefined reference to cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::__ndk1::vector
cv::CascadeClassifier::detectMultiScale,
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol “ZN2cv17CascadeClassifier16detectMultiScaleERKNS_11_InputArrayERNSt6__ndk16vectorINS_5Rect_IiEENS4_9allocatorIS7_EEEEdiiNS_5Size_IiEESD” referenced by “/data/app/com.eyetext-RHXAwIq3io7ECCIZsowX0g==/lib/arm64/libHeterotropia.so”…
at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
S:
1.Emphasis ndk版本问题,还是要r16的
2.将APP_STL := c++_shared 换成APP_STL:= gnustl_static(新版的只支持c++_static和c++_shared)
I2.没有添加APP_CPPFLAGS := -std=c++11
In file included from D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/build//…/sources/cxx-stl/gnu-libstdc++/4.9/include\regex:35:
D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/build//…/sources/cxx-stl/gnu-libstdc++/4.9/include\bits/c++0x_warning.h:32:2: error:
This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently
experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
S:
在Application.mk里设置:
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions
I3.Build command failed.
Error while executing process D:\AS\sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C F:\xxxxx\app.cxx\cmake\debug\arm64-v8a oct}
ninja: Entering directory F:\xxxxx\app\.cxx\cmake\debug\arm64-v8a' [1/1] Linking CXX shared library F:\xxxxx\app\build\intermediates\cmake\debug\obj\arm64-v8a\liboct.so FAILED: F:/xxxxxt/app/build/intermediates/cmake/debug/obj/arm64-v8a/liboct.so cmd.exe /C "cd . && D:\AS\android-ndk-r16b-windows-x86_64\android-ndk-r16b\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/sysroot -fPIC -isystem D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,liboct.so -o F:\xxxxx\app\build\intermediates\cmake\debug\obj\arm64-v8a\liboct.so CMakeFiles/oct.dir/oct.cpp.o wavelib/libwavelib.a -llog -latomic -lm "D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a" && cd ." D:/AS/android-ndk-r16b-windows-x86_64/android-ndk-r16b/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64/lib/gcc/aarch64-linux-android/4.9.x\libgcc.a(unwind-dw2-fde-dip.o): In function
_Unwind_Find_FDE’:
/usr/local/google/buildbot/src/android/gcc/toolchain/build/…/gcc/gcc-4.9/libgcc/unwind-dw2-fde-dip.c:461: undefined reference to `dl_iterate_phdr’
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
S:不兼容ABI架构arm64-v8a,build.gradle设置去掉
‘arm64-v8a’
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters'armeabi-v7a', 'x86', 'x86_64'
arguments "-DANDROID_STL=gnustl_static"
}
参考
兼容性问题https://blog.csdn.net/weixin_30408739/article/details/98624782
Opencv问题
https://answers.opencv.org/questions/scope:all/sort:activity-desc/page:1/query:undefined%20reference%20to/
https://github.com/opencv/opencv/issues
NDK问题
https://github.com/android/ndk/issues
引入第三方目录
https://www.cnblogs.com/wuchaodzxx/p/8916009.html
https://www.jianshu.com/p/963db8ceecda
https://blog.csdn.net/s13383754499/article/details/91546697
https://www.cnblogs.com/fnlingnzb-learner/p/7593488.html
https://blog.csdn.net/weixin_40779546/article/details/84852327