在手機上跑 AI 模型——不需要連網,資料不出裝置——這不是概念,Gemma 4 現在就能做到。E2B 和 E4B 這兩個小模型就是專門為手機設計的。這篇教學把 Android 和 iOS 的部署方式都講清楚。
手機能跑哪些模型?
不是每個 Gemma 4 模型都塞得進手機:
| 模型 | 參數量 | 記憶體需求 | Android | iOS | 推薦? |
|---|---|---|---|---|---|
| Gemma 4 E2B | 2B | ~3 GB | 可以 | 可以 | 大部分手機首選 |
| Gemma 4 E4B | 4B | ~5 GB | 可以 | 可以 | 僅旗艦機 |
| Gemma 4 1B | 1B | ~2 GB | 可以 | 可以 | 最快但品質一般 |
| Gemma 4 4B | 4B | ~5 GB | 勉強 | 勉強 | 緊巴巴 |
| Gemma 4 12B+ | 12B+ | ~9 GB+ | 不行 | 不行 | 手機跑不了 |
E2B 和 E4B 是專門最佳化過的「邊緣」模型,在能塞進手機的大小下同時支援文字、圖片和音訊。模型可以從下載安裝完整攻略裡的任意管道取得,詳細的記憶體和儲存需求看硬體設定指南。
Android 部署
Android 生態最成熟,Google 自家的整合做得很到位。
方案一:Google AI Edge SDK
Google 官方的手機端 Gemma 方案:
// build.gradle.kts
dependencies {
implementation("com.google.ai.edge:ai-edge-sdk:0.3.0")
}
// 在 Activity 或 ViewModel 中
import com.google.ai.edge.InferenceSession
import com.google.ai.edge.ModelConfig
class GemmaViewModel : ViewModel() {
private var session: InferenceSession? = null
fun initModel(context: Context) {
val config = ModelConfig.builder()
.setModelPath("gemma-4-e2b-it.task")
.setMaxTokens(1024)
.setTemperature(0.7f)
.build()
session = InferenceSession.create(context, config)
}
fun generateResponse(prompt: String): String {
return session?.generateResponse(prompt) ?: "模型未載入"
}
}方案二:AICore(Pixel 和三星)
AICore 內建在新款 Pixel 和三星 Galaxy 手機裡,提供系統級 AI 加速:
// 檢查 AICore 是否可用
val aiCoreAvailable = AICore.isAvailable(context)
if (aiCoreAvailable) {
// AICore 處理模型管理和最佳化
val session = AICore.createSession(
model = "gemma-4-e2b-it",
options = AICore.Options.builder()
.setAccelerator(AICore.Accelerator.GPU)
.build()
)
val response = session.generate("簡單解釋一下光合作用")
}AICore 的好處是模型可能已經在裝置上快取了,使用者不需要單獨下載 2-3GB。
方案三:MediaPipe LLM Inference API
MediaPipe 更靈活,相容更多 Android 裝置:
dependencies {
implementation("com.google.mediapipe:tasks-genai:0.10.20")
}
// 初始化 LLM
val options = LlmInference.LlmInferenceOptions.builder()
.setModelPath("/data/local/tmp/gemma-4-e2b-it.bin")
.setMaxTokens(1024)
.setTopK(40)
.setTemperature(0.7f)
.setRandomSeed(42)
.build()
val llmInference = LlmInference.createFromOptions(context, options)
// 產生文字
val response = llmInference.generateResponse("什麼是機器學習?")
// 串流輸出
llmInference.generateResponseAsync(prompt) { partialResult, done ->
// 逐 token 更新 UI
textView.append(partialResult)
}iOS 部署
方案一:AI Edge Gallery App
最簡單的方式——從 App Store 下載 AI Edge Gallery。Apple 平台的具體最佳化技巧可以看專門的 iPhone 指南。
- 安裝 AI Edge Gallery
- 瀏覽可用模型
- 下載 Gemma 4 E2B 或 E4B
- 開始聊天——完全離線
適合個人使用和測試,但沒辦法嵌入你自己的 App。
方案二:LiteRT(TensorFlow Lite Runtime)
要在自己的 iOS App 裡整合 Gemma 4:
import LiteRT
class GemmaModel {
private var interpreter: Interpreter?
func loadModel() throws {
guard let modelPath = Bundle.main.path(
forResource: "gemma-4-e2b-it",
ofType: "tflite"
) else {
throw GemmaError.modelNotFound
}
var options = Interpreter.Options()
options.threadCount = 4
// 用 GPU 加速
let gpuDelegate = MetalDelegate()
interpreter = try Interpreter(
modelPath: modelPath,
options: options,
delegates: [gpuDelegate]
)
}
func generate(prompt: String) throws -> String {
let tokens = tokenize(prompt)
try interpreter?.allocateTensors()
try interpreter?.copy(tokens, toInputAt: 0)
try interpreter?.invoke()
let output = try interpreter?.output(at: 0)
return decode(output)
}
}方案三:MediaPipe for iOS
MediaPipe 同樣支援 iOS:
import MediaPipeTasksGenAI
let options = LlmInference.Options()
options.modelPath = Bundle.main.path(
forResource: "gemma-4-e2b-it",
ofType: "bin"
)!
options.maxTokens = 1024
options.temperature = 0.7
let llm = try LlmInference(options: options)
let response = try llm.generateResponse(inputText: "你好!")效能預期
對手機 AI 的速度要有合理預期:
| 裝置 | 模型 | 速度 (tok/s) | 首 token (ms) | 記憶體佔用 |
|---|---|---|---|---|
| Pixel 9 Pro | E2B | ~15-20 | ~800 | ~3 GB |
| Pixel 9 Pro | E4B | ~8-12 | ~1500 | ~5 GB |
| 三星 S24 Ultra | E2B | ~15-18 | ~900 | ~3 GB |
| iPhone 15 Pro | E2B | ~12-15 | ~1000 | ~3 GB |
| iPhone 16 Pro | E2B | ~15-18 | ~800 | ~3 GB |
| iPhone 16 Pro | E4B | ~8-10 | ~1500 | ~5 GB |
比電腦慢,但用來互動式聊天完全夠了。首 token 會稍微慢一點因為模型需要初始化。
續航和發熱
AI 推論是運算密集型任務,要注意這些:
| 關注點 | 實際情況 | 緩解方法 |
|---|---|---|
| 耗電 | 使用中約 5-8%/小時 | 限制最大產生長度 |
| 發熱 | 推論時手機會變燙 | 長對話之間加冷卻間隔 |
| 背景執行 | 系統可能終止程序 | 只在需要時載入模型 |
| 儲存空間 | 每個模型 2-5 GB | 讓模型下載作為選擇性功能 |
// 好的做法:不用時釋放模型
override fun onPause() {
super.onPause()
session?.close()
}
override fun onResume() {
super.onResume()
if (session == null) initModel()
}離線才是殺手鐧
手機端 AI 最大的優勢是不需要連網。想想這些情境:
- 旅行:飛航模式下 AI 助手照樣能用
- 隱私敏感任務:健康問題、個人日記、私密程式碼——資料完全不出裝置
- 網路差的地方:鄉下、捷運、訊號不好的區域
- 速度:沒有網路延遲,回覆馬上開始
- 成本:模型下載後就是免費的,沒有 API 費用
這是雲端 API 根本做不到的事。Gemma 4 跑在你手機上,你的資料就留在你手機上。就這麼簡單。
手機端 vs 雲端 API
| 因素 | 裝置端 (Gemma 4 E2B) | 雲端 API (Gemini) |
|---|---|---|
| 速度 | ~15 tok/s | ~50-100 tok/s |
| 品質 | 不錯 (2B 模型) | 很好 (大模型) |
| 隱私 | 完全保護 | 資料傳到伺服器 |
| 離線 | 支援 | 不支援 |
| 成本 | 下載後免費 | 按 token 計費 |
| 續航影響 | 大 | 很小 |
| 設定 | 需要下載模型 | 只需 API key |
最理想的方案是混合使用:隱私敏感和離線任務用裝置端 Gemma 4,連網時需要更高品質就回退到雲端 API。
下一步
- 想在 iPhone 上跑? 看專門的 iPhone 指南 了解 Apple 平台的特定最佳化
- 不確定選哪個模型? 看 模型選擇指南 了解完整陣容
- 想了解電腦的硬體需求? 看 硬體指南 了解桌機和筆電的設定建議
手機 AI 還在早期,但已經能用了。從 E2B 模型開始,在你的手機上試一試。一個有用的 AI 完全跑在你口袋裡的手機上——不需要連網、不需要 API key、不需要月費——想想還是挺厲害的。
Stop reading. Start building.
~/gemma4 $ Get hands-on with the models discussed in this guide. No deployment, no friction, 100% free playground.
Launch Playground />


