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

Android.mk引入预编译共享库,Android模块编译失败求助

Fixing Android Module Compilation Issues with Prebuilt .so & .h Libraries

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_INCLUDES points directly to the folder holding your .h files. If your headers are in a subfolder under include, add that path too (e.g., $(LOCAL_PATH)/include/subfolder).

Error 2: undefined reference to 'some_function_from_thirdparty()'

  • Root Causes:
    1. You forgot to link against the prebuilt library in LOCAL_SHARED_LIBRARIES.
    2. 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).
  • Fix:
    • Ensure LOCAL_SHARED_LIBRARIES includes the exact module name you set for the prebuilt library (in the example above, that’s thirdparty_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 in Application.mk.

Error 3: error: library not found for -lthirdparty_prebuilt

  • Root Cause: Either the path to your .so file in LOCAL_SRC_FILES is wrong, or the module name for the prebuilt library doesn’t match what you’re referencing in LOCAL_SHARED_LIBRARIES.
  • Fix:
    • Confirm LOCAL_SRC_FILES uses the correct relative path from your jni directory (where Android.mk lives).
    • Make sure the LOCAL_MODULE name for the prebuilt library is identical to what’s in LOCAL_SHARED_LIBRARIES.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:12:04