Android.mk引入预编译共享库,Android模块编译失败求助
Hey there! I’ve dealt with this exact problem multiple times when working with prebuilt shared libraries on Android, so let’s break down how to fix your Android.mk configuration and troubleshoot common errors.
First: A Correct Android.mk Template
Let’s assume your project structure looks like this (adjust paths to match yours):
your-module/ ├── jni/ │ ├── Android.mk │ ├── Application.mk │ ├── your-source-file.c # Your module's source code │ ├── include/ # Store all third-party .h files here │ │ └── thirdparty.h │ └── libs/ # Architecture-specific .so files │ ├── armeabi-v7a/ │ │ └── libthirdparty.so │ ├── arm64-v8a/ │ │ └── libthirdparty.so │ ├── x86/ │ │ └── libthirdparty.so │ └── x86_64/ │ └── libthirdparty.so
Here’s the properly configured Android.mk for this setup:
LOCAL_PATH := $(call my-dir) # Step 1: Declare the prebuilt shared library include $(CLEAR_VARS) LOCAL_MODULE := thirdparty_prebuilt LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libthirdparty.so # Mark this as a prebuilt shared library include $(PREBUILT_SHARED_LIBRARY) # Step 2: Compile your own module include $(CLEAR_VARS) LOCAL_MODULE := your_custom_module LOCAL_SRC_FILES := your-source-file.c # Replace with your actual source files # Point to the directory containing third-party .h files LOCAL_C_INCLUDES := $(LOCAL_PATH)/include # Link against the prebuilt library we declared above LOCAL_SHARED_LIBRARIES := thirdparty_prebuilt # Build as a shared library (use BUILD_EXECUTABLE if you're making a binary) include $(BUILD_SHARED_LIBRARY)
Troubleshooting Common Errors
Error 1: fatal error: thirdparty.h: No such file or directory
- Root Cause: The compiler can’t find your third-party header files because you didn’t specify their path correctly.
- Fix: Double-check that
LOCAL_C_INCLUDESpoints directly to the folder holding your .h files. If your headers are in a subfolder underinclude, add that path too (e.g.,$(LOCAL_PATH)/include/subfolder).
Error 2: undefined reference to 'some_function_from_thirdparty()'
- Root Causes:
- You forgot to link against the prebuilt library in
LOCAL_SHARED_LIBRARIES. - The .so file you’re using doesn’t match the architecture you’re compiling for (e.g., using an armeabi-v7a .so when building for arm64-v8a).
- You forgot to link against the prebuilt library in
- Fix:
- Ensure
LOCAL_SHARED_LIBRARIESincludes the exact module name you set for the prebuilt library (in the example above, that’sthirdparty_prebuilt). - Verify that
libs/$(TARGET_ARCH_ABI)contains a .so file for the target architecture. You can check the target architecture in your build logs or set specific ABIs inApplication.mk.
- Ensure
Error 3: error: library not found for -lthirdparty_prebuilt
- Root Cause: Either the path to your .so file in
LOCAL_SRC_FILESis wrong, or the module name for the prebuilt library doesn’t match what you’re referencing inLOCAL_SHARED_LIBRARIES. - Fix:
- Confirm
LOCAL_SRC_FILESuses the correct relative path from yourjnidirectory (where Android.mk lives). - Make sure the
LOCAL_MODULEname for the prebuilt library is identical to what’s inLOCAL_SHARED_LIBRARIES.
- Confirm
Bonus: Application.mk Tips
Don’t forget to specify the ABIs you want to support in Application.mk to avoid mismatches:
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 # Adjust to your needs APP_PLATFORM := android-21 # Match the minimum SDK version your library supports
If you were working with a static library (.a) instead of a shared library (.so), just swap include $(PREBUILT_SHARED_LIBRARY) with include $(PREBUILT_STATIC_LIBRARY) and use LOCAL_STATIC_LIBRARIES instead of LOCAL_SHARED_LIBRARIES.
内容的提问来源于stack exchange,提问作者Donato Azevedo

