MCP 是 Anthropic 推出的开放协议,用于连接 AI 助手与外部工具。MCP Server 将特定工具包装成标准化接口,让 AI 能够理解和调用。
架构如下:
1
Claude Desktop/API → MCP Server → ADB Commands → Android Device
实现步骤
初始化项目
1234
mkdir adb_mcp
cd adb_mcp
npm init -ynpm install @modelcontextprotocol/sdk
配置 package.json
123456789101112131415161718
{"name":"adb-mcp-server","version":"1.0.0","description":"MCP Server for Android Debug Bridge (ADB) operations","main":"adb-mcp-server.js","bin":{"adb-mcp-server":"./adb-mcp-server.js"},"scripts":{"start":"node adb-mcp-server.js"},"keywords":["mcp","adb","android","debug"],"author":"","license":"MIT","dependencies":{"@modelcontextprotocol/sdk":"^1.0.0"}}
{name:'adb-install',description:'Install APK on connected device',inputSchema:{type:'object',properties:{apkPath:{type:'string',description:'Path to the APK file'}},required:['apkPath']}}
{name:'adb-screenshot',description:'Take a screenshot',inputSchema:{type:'object',properties:{savePath:{type:'string',description:'Path to save screenshot'}},required:['savePath']}}
{name:'adb-top',description:'Get CPU usage',inputSchema:{type:'object',properties:{}}}
添加文件传输
123456789101112
{name:'adb-push',description:'Push file to device',inputSchema:{type:'object',properties:{localPath:{type:'string'},remotePath:{type:'string'}},required:['localPath','remotePath']}}
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:mergeDebugJavaResource'.> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'META-INF/versions/9/OSGI-INF/MANIFEST.MF' from inputs:
Caused by: java.lang.RuntimeException: Duplicate class org.tensorflow.lite.DataType found in
modules jetified-litert-api-1.0.1-runtime (com.google.ai.edge.litert:litert-api:1.0.1) and
jetified-tensorflow-lite-api-2.12.0-runtime (org.tensorflow:tensorflow-lite-api:2.12.0)
解决方案
在 app/build.gradle 中添加依赖替换规则:
12345
configurations.all { resolutionStrategy.dependencySubstitution { substitute module("org.tensorflow:tensorflow-lite") with module("com.google.ai.edge.litert:litert:1.0.1")}}
说明
Google 将 TensorFlow Lite 迁移到新包名 com.google.ai.edge.litert,若项目同时包含新旧包名,会导致类冲突。通过依赖替换强制使用新包解决。
3. Jetifier 与 BouncyCastle 兼容性问题
错误信息
1234
Caused by: java.lang.RuntimeException: Failed to transform
'/Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.bouncycastle/bcprov-jdk18on/1.78/619aafb92dc0b4c6c
c4cf86c487ca48ee2d67a8e/bcprov-jdk18on-1.78.jar' using Jetifier.
Reason: IllegalArgumentException, message: Unsupported class file major version 65.
If you just want to use proxy on a specified repository, don’t need on other repositories. The preferable way is the -c, –config <key=value> option when you git clone a repository. e.g.
../../../your_pub/lib/src/framework.dart:275:26: Warning: Operand of null-aware operation '!' has type'SchedulerBinding' which excludes null.
[] - 'SchedulerBinding' is from 'package:flutter/src/scheduler/binding.dart'('../../../code/flutter_3/packages/flutter/lib/src/scheduler/binding.dart').[]if(SchedulerBinding.instance!.schedulerPhase ==
上面的警告虽然不会影响应用的编译,但是长久来看,还是需要解决的。
原因为何
原因是从 flutter 3 开始, SchedulerBinding.instance返回的是一个 非 null 实例,当我们使用SchedulerBinding.instance!.schedulerPhase 会得到这样的警告Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.