问题:使用Android Studio 引入第三方SO和头文件后运行项目时编译报错。这个问题我在ChatGPT上问过 搞了好半天也都进展
希望帮忙看看 感激不尽 感激不尽,顺利编译通过后 费用给你
项目地址:https://github.com/52932/TestWork 分支:main
SO库文件路径是:/Movies/MTest/app/libs/arm64-v8a/libTest.so。
- 错误信息是:
Build command failed.
Error while executing process /Library/Android/sdk/cmake/3.22.1/bin/ninja with arguments {-C /Movies/MTest/app/.cxx/Debug/4v675r6m/arm64-v8a mtest}
ninja: Entering directory `/Movies/MTest/app/.cxx/Debug/4v675r6m/arm64-v8a'
ninja: error: '/Movies/MTest/app/src/main/cpp/app/libs/arm64-v8a/libTest.so', needed by '/Movies/MTest/app/build/intermediates/cxx/Debug/4v675r6m/obj/arm64-v8a/libmtest.so', missing and no known rule to make it
- cmake文件路径是:/Movies/MTest/app/src/main/cpp/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.22.1)
# Declares and names the project.
project("mtest")
# 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.
mtest
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
CallPluginLib.cpp)
#配置加载头文件
include_directories(../cpp/include)
#动态方式加载
add_library(Test SHARED IMPORTED )
#引入第三方.so库
set_target_properties(Test PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/app/libs/${ANDROID_ABI}/libTest.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( # Specifies the target library.
mtest
Test
# Links the target library to the log library
# included in the NDK.
${log-lib})
- build.gradle文件内容如下:
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.mtest'
compileSdk 33
defaultConfig {
applicationId "com.example.mtest"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-std=c++17'
}
}
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
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.22.1'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}