在 Android 系統層開發中,使用 Binder 進行進程間通訊(IPC)時,編譯錯誤屢見不鮮。本文整理六大典型錯誤及其修復方法,供開發者快速排查。
錯誤:undefined reference to `android::IPlayerServer::handleToPlayerid(int, int)'
原因:介面類中宣告的虛函數未標記為純虛函數(=0),也未提供預設實現。
修復:將虛函數改為純虛函數:
virtual int handleToPlayerid(int type, int handle) = 0;
錯誤:no 'size_t android::Player::getFlattenedSize() const' member function declared
原因:需序列化的類別未繼承 Flattenable 介面並實現其四個虛函數。
修復:在類別中宣告並實現:
// Flattenable interface
virtual size_t getFlattenedSize() const;
virtual size_t getFdCount() const;
virtual status_t flatten(void* buffer, size_t size, int fds[], size_t count) const;
virtual status_t unflatten(void const* buffer, size_t size, int fds[], size_t count);
錯誤:'class android::PlayerDevice' has no member named 'decStrong'
原因:使用 sp<T> 管理的物件必須繼承 RefBase。
修復:
class PlayerDevice : public Singleton<PlayerDevice>, public RefBase { ... };
錯誤:Error: selected processor does not support `smull r6,r1,r3,r1'
原因:Thumb 指令集不支援特定 ARM 指令,且內嵌組語以 inline 宏展開導致編譯失敗。
修復:在 Android.mk 中對該檔案強制使用 ARM 模式:
LOCAL_CFLAGS += -mno-thumb-interwork -mno-thumb -marm
或在模組首行加入 ALWAYS_BUILD_AS_ARM。
變更:typedef struct ANativeWindow android_native_window_t;
修復:將代碼中 struct android_native_window_t 替換為 android_native_window_t。注意 Android 2.2 無此定義,需做版本判斷。
錯誤:找不到 LOCAL_MODULE_TAGS 定義
修復:在 build/core/definitions.mk 的 include-prebuilt 定義中添加:
define include-prebuilt
include $(CLEAR_VARS)
...
LOCAL_MODULE_TAGS := eng # 新增此行
include $(BUILD_PREBUILT)
endef
掌握這些常見坑點,能大幅減少 Binder 開發中的編譯阻塞時間。