我正在使用 opencv 库在 android studio 中创建一个项目。我的 ndk 是 r23,我的 cmake 是 3.18.1。 这是我的 graddle.build(app):
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.slam3'
compileSdk 33
defaultConfig {
applicationId "com.example.slam3"
minSdk 29
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation project(path: ':openCV')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
我使用 CMakeLists.txt 在我的项目中使用简单的本机库。我的 CMakeLists.txt:
set(pathToProject C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/Slam3)
set(pathToOpenCV C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/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.18.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathToOpenCV}/sdk/native/jni/include)
# Declares and names the project.
project("slam3")
# 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.
slam3
# 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(slam3 ${log-lib} lib_opencv)
当我在设备中运行项目时,我看到此错误:
C/C++: ld: error: found local symbol '__bss_start' in global part of symbol table in file C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/Slam3/app/src/main/jniLibs/arm64-v8a/libopencv_java3.so
我测试这个
但我仍然有同样的错误。我该如何解决?
您可以尝试使用不同的ndk版本。 就我而言,我将 ndkVersion 更改为 21.4.7075529,问题解决了。
build.gradle
android {
compileSdk = 33
ndkVersion = "21.4.7075529"
....
}