在 Android 系統層開發 Binder 跨行程通訊時,編譯錯誤往往隱藏在介面定義與實作細節中。整理六大高頻錯誤及修正方式。
// 錯誤:virtual int handleToPlayerid(int, int); // 缺少 = 0 或實作
// 正確:
virtual int handleToPlayerid(int type, int handle) = 0; // 明確宣告純虛擬
// 或在 BnInterface 衍生類中提供預設實作
錯誤訊息:undefined reference to `android::IPlayerServer::handleToPlayerid(int, int)'
需序列化的類別(如 Player)必須實作:
// 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);
sp<T> 智慧指標要求 T 繼承 RefBase,提供 incStrong/decStrong:
class PlayerDevice : public Singleton<PlayerDevice>, public RefBase { ... };
錯誤:Error: selected processor does not support `smull r6,r1,r3,r1'
解法:在 Android.mk 加入
LOCAL_CFLAGS += -mno-thumb-interwork -mno-thumb -marm
或在模組首行加 ALWAYS_BUILD_AS_ARM。
// 2.2
struct android_native_window_t { ... };
// 2.3+
typedef struct ANativeWindow android_native_window_t;
// 代碼中直接用 android_native_window_t 即可
修改 build/core/definitions.mk 中 include-prebuilt:
define include-prebuilt
include $(CLEAR_VARS)
...
LOCAL_MODULE_TAGS := eng # 新增此行
include $(BUILD_PREBUILT)
endef
作者:andyhuabing