Android NDK项目CMake配置失败:native-lib未被项目构建
Hey there, let's tackle this CMake error you're facing. The core issue is clear from the error message: CMake can't find the native-lib target because you've commented out the code that defines it in your CMakeLists.txt. Let's walk through the fixes step by step.
1. Restore the native-lib Library Definition
In your current CMakeLists.txt, you've commented out the add_library block for native-lib—this is the code that tells CMake to build your C++ source file into a shared library. Without this, CMake has no idea what native-lib is when you try to link other libraries to it.
2. Restore the log-lib Lookup
You also commented out the find_library block for the Android NDK log library, but you're still referencing ${log-lib} in target_link_libraries. We need to bring that back too so CMake can locate the system log library.
3. Adjust the Order of CMake Commands
CMake requires that you define targets before you link them. So we need to reorder your CMake code to follow this logical flow:
- Set the minimum CMake version
- Define your project's own
native-liblibrary - Look up system libraries (like
log) - Import external
.solibraries - Link all libraries together
Fixed CMakeLists.txt
Here's the corrected version of your CMakeLists.txt with all necessary parts uncommented and ordered properly:
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html cmake_minimum_required(VERSION 3.4.1) # Sets the minimum version of CMake required to build the native library. # Import header file include_directories(src/main/jniLibs/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). native-lib.cpp ) ## 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 ) # Declare the import file to change the directory variable ARM_DIR, which uses the relative directory with the system, because using the relative path does not seem to work set(ARM_DIR C:/Users/Username/AndroidStudioProjectsFolder/ExamplAppName/app/src/main/jniLibs) # Add an example of so library. add_library(avdevice SHARED IMPORTED) set_target_properties(avdevice PROPERTIES IMPORTED_LOCATION ${ARM_DIR}/arm64-v8a/libavdevice.so) # Adding other so libraries in the same format as above (make sure to define them before linking) add_library(avformat SHARED IMPORTED) set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${ARM_DIR}/arm64-v8a/libavformat.so) # Link library target_link_libraries( native-lib avdevice avformat ${log-lib} )
4. Quick Checks to Avoid Other Issues
- Verify
.soPaths: Double-check that the paths inset_target_propertiesexactly match where your.sofiles are stored injnilibs/arm64-v8a/. - Java
loadLibraryOrder: Keep your Java static block as is—load external libraries first, then yournative-lib:static { System.loadLibrary("avdevice"); System.loadLibrary("avformat"); System.loadLibrary("native-lib"); } - ABI Consistency: Your
build.gradlespecifiesabiFilters 'arm64-v8a', so ensure all your imported.sofiles are built for this ABI (no mismatches like x86 or armeabi-v7a).
After making these changes, clean your project (Build > Clean Project) and rebuild—this should resolve the CMake configuration failure.
内容的提问来源于stack exchange,提问作者yejafot




