You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android项目CMake中引入自研C++库的父目录src文件?

Hey there! Let's get your custom C++ library integrated into your Android app without a hitch. Based on the directory structure you shared, here's a straightforward, step-by-step solution using CMake (the standard tool for native Android development):

Step 1: Configure your App's CMakeLists.txt to locate the library

First, open the CMakeLists.txt in your Android Application/App/ directory. We'll start by defining a variable that points to your C++ library's source folder using a relative path:

# Calculate the path to your C++ library's src directory
set(MY_CPP_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../my_c++_project/src")

Note: Adjust the relative path here if your CMakeLists.txt is in a subfolder (e.g., if it's inside App/src/main/cpp/, use ../../../../my_c++_project/src instead).

Step 2: Add the library's headers to your include path

This ensures your Android native code can find the library's header files:

# Include the C++ library's header directory
include_directories(${MY_CPP_LIB_DIR})

Step 3: Compile the C++ library as part of your Android build

Next, we'll collect all the library's source files and add them as a static or shared library to the build process:

# Collect all .cpp files from your C++ library (you can also list them manually for more control)
file(GLOB_RECURSE MY_CPP_LIB_SOURCES "${MY_CPP_LIB_DIR}/*.cpp")

# Create a static library from your C++ code (use SHARED instead if you want a dynamic library)
add_library(my_cpp_lib STATIC ${MY_CPP_LIB_SOURCES})

Pro tip: If your library's file structure changes often, avoid GLOB_RECURSE and list source files explicitly to prevent build issues.

If your Android app has its own native library (e.g., native-lib.so), link your custom C++ library to it so the app can use its functionality:

# Example: Your existing Android native library
add_library(native-lib SHARED src/main/cpp/native-lib.cpp)

# Link your custom C++ library to the Android native library
target_link_libraries(native-lib PRIVATE my_cpp_lib)

Step 5: Verify your Android Studio build configuration

Make sure your module-level build.gradle file points to the correct CMakeLists location and uses a compatible CMake version:

android {
    ...
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt" // Match the path to your actual CMakeLists file
            version "3.22.1" // Use a version compatible with your Android NDK
        }
    }
}

Bonus: Set C++ standard compatibility

If your library uses a specific C++ standard (like C++17), add this to your CMakeLists.txt to ensure compatibility:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

That's it! Sync your project in Android Studio, and you should be able to call your C++ library's functions from your Android app's native code.

内容的提问来源于stack exchange,提问作者Karim Mohamed Hasebou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:51:09