Android Thing 設(shè)備 API 的正確引入方式與依賴配置指南
android things 已于 2020 年正式停止維護(hù),其核心庫 `com.google.android.things`(含 `device` 等子包)僅適用于舊版 android things os 設(shè)備開發(fā),不可用于常規(guī) android 應(yīng)用;正確依賴應(yīng)為 `com.google.android.things:androidthings:1.0`,且需配置 google maven 倉庫并注意平臺兼容性限制。
Android Things 是 Google 曾推出的面向物聯(lián)網(wǎng)嵌入式設(shè)備的操作系統(tǒng)(基于 Android),專為樹莓派、NXP i.MX7D 等硬件設(shè)計(jì)。其 SDK 提供了 com.google.android.things.device 等專用 API(如 PeripheralManager、UartDevice、Gpio),用于直接控制硬件外設(shè)。但需明確:該庫并非普通 Android 應(yīng)用可用的通用工具包,而是 Android Things OS 運(yùn)行時環(huán)境的系統(tǒng)級 API 。
? 正確依賴配置(僅限 Android Things 項(xiàng)目) 若你正在開發(fā)運(yùn)行于 Android Things OS 的固件應(yīng)用(targetSdkVersion ≤ 27,minSdkVersion = 24),請按以下步驟配置:
在項(xiàng)目級 build.gradle 中添加 Google Maven 倉庫 :
復(fù)制 AI寫代碼 1
2
3
4
5
6
7
// build.gradle (Project)
allprojects {
repositories {
google() // 即 https://maven.google.com
mavenCentral()
}
}
在模塊級 build.gradle 中聲明正確依賴 :
復(fù)制 AI寫代碼 1
2
3
4
5
6
7
8
9
10
11
// build.gradle (Module: app)
android {
compileSdk 27 // 必須為 27 或更低(Android Things 1.0 對應(yīng) Android 8.1)
defaultConfig {
minSdkVersion 24
targetSdkVersion 27
// 注意:Android Things 應(yīng)用必須聲明 uses-library
manifestPlaceholders = [thingssdk: "com.google.android.things"]
}
}
dependencies {implementation com.google.android.things:androidthings:1.0}
復(fù)制 AI寫代碼 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
3. **在 `AndroidManifest.xml` 中聲明系統(tǒng)庫依賴**:
```xml
<application ...>
<uses-library
android:name="com.google.android.things"
android:required="true" />
<!-- 主 Activity 需聲明為 Launcher,并指定 Android Things category -->
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.IOT_LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
?? 重要注意事項(xiàng) ? implementation com.google.android.things:device:1.0 是錯誤的坐標(biāo) ——該 artifact 從未發(fā)布,Maven 倉庫中不存在。唯一有效 artifact 是 com.google.android.things:androidthings:1.0。 ? 不支持常規(guī) Android 手機(jī)/平板 :此庫無法在標(biāo)準(zhǔn) Android 設(shè)備上運(yùn)行,調(diào)用 PeripheralManager.getInstance() 將拋出 UnsupportedOperationException 或 SecurityException。 ? 項(xiàng)目已終止維護(hù) :Google 自 2020 年 1 月起停止 Android Things 的更新與支持,不再提供新功能、安全補(bǔ)丁或云服務(wù)(Cloud IoT Core 集成已下線)。新項(xiàng)目請轉(zhuǎn)向 Raspberry Pi OS + libgpiod、ESP-IDF、Arduino 或 Android’s modern Jetpack libraries(如 androidx.core:core-splashscreen 不相關(guān),硬件交互推薦使用廠商 SDK 或 HAL 層封裝)。 ? 若你在文檔中看到 com.google.android.things.device.* 包,它屬于 androidthings-1.0.aar 內(nèi)部結(jié)構(gòu),無需單獨(dú)引入子模塊 ——androidthings:1.0 已完整包含全部 API。 ? 替代方案建議(2024 年起) 場景 推薦方案 樹莓派 GPIO/I2C/UART 控制 使用 libgpiod (C/C++/Python)或 Pi4J v2 (Java) 跨平臺嵌入式開發(fā) ESP-IDF(ESP32)、Zephyr RTOS、Rust embedded-hal Android 設(shè)備有限硬件訪問 android.hardware.usb(USB Host)、android.bluetooth.le(BLE)、廠商定制 HAL(如 Qualcomm QCA4020 SDK)
總之,請確認(rèn)你的目標(biāo)平臺是否為已刷寫 Android Things 1.0 固件的開發(fā)板;否則,引入該依賴不僅無法編譯通過,更會在運(yùn)行時失敗。技術(shù)選型前務(wù)必查閱 Android Things 官方停更公告 及當(dāng)前活躍的開源 IoT 生態(tài)。
“Android Thing 設(shè)備 API 的正確引入方式與依賴配置指南” 的相關(guān)文章