首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Android Binder 客戶端/服務端常見編譯錯誤與解決方案

     編輯:關於C語言

背景

在 Android 系統層開發 Binder 跨行程通訊時,編譯錯誤往往隱藏在介面定義與實作細節中。整理六大高頻錯誤及修正方式。

1. 純虛擬函式未實作導致連結錯誤

// 錯誤:virtual int handleToPlayerid(int, int);  // 缺少 = 0 或實作
// 正確:
virtual int handleToPlayerid(int type, int handle) = 0; // 明確宣告純虛擬
// 或在 BnInterface 衍生類中提供預設實作

錯誤訊息:undefined reference to `android::IPlayerServer::handleToPlayerid(int, int)'

2. Flattenable 介面缺失

需序列化的類別(如 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);

3. 缺少 RefBase 繼承

sp<T> 智慧指標要求 T 繼承 RefBase,提供 incStrong/decStrong:

class PlayerDevice : public Singleton<PlayerDevice>, public RefBase { ... };

4. 內嵌 ARM 組語在 Thumb 模式下編譯失敗

錯誤: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。

5. Android 2.3 結構體變更

// 2.2
struct android_native_window_t { ... };

// 2.3+
typedef struct ANativeWindow android_native_window_t;
// 代碼中直接用 android_native_window_t 即可

6. 預編譯靜態庫缺少 LOCAL_MODULE_TAGS

修改 build/core/definitions.mk 中 include-prebuilt:

define include-prebuilt
    include $(CLEAR_VARS)
    ...
    LOCAL_MODULE_TAGS := eng  # 新增此行
    include $(BUILD_PREBUILT)
endef

作者:andyhuabing

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved