← Kotlin / Android Course

ANDROID CORE · 112

Gradle / Build Variant / Install / Logcat / ADB:失敗到底在哪一層?

Android Studio 的 Run 按鈕背後包含 build graph、resource processing、packaging、signing、install、process launch。最後一課把這些 layer 拆開,避免「App 跑不起來」只剩一種模糊問題。

Learning outcomes

1. Build pipeline

Kotlin + resources + manifestGradlecompiled / packaged outputsign/installdevicelaunchapp process

Gradle 是 build system,不是 Android runtime。

2. Build variant

debug
release

不同 variant 可能有不同 signing、minification、build config、backend endpoint、logging。Debug 能跑不代表 Release 一定等價。

3. Build failure

例如 dependency resolution、Kotlin compile、resource linking、manifest merge,都可能在 app 還沒安裝前就失敗。

診斷:如果 APK 都沒有成功產生,就不要先查 runtime permission 或 GPS。

4. Install / launch failure

adb devices
adb install app-debug.apk

裝置是否連線、授權、storage、package/signature conflict 都屬不同層。

5. Logcat:runtime evidence stream

adb logcat

Logcat 可能包含:

真正 debug 時要用 package/process/tag/filter 縮小範圍,不是從幾萬行裡瞎找。

6. ADB:電腦和 device 的控制/觀察通道

adb devices
adb shell
adb logcat
adb install ...

ADB 能操作很多低階功能。只執行你理解的 command,尤其避免破壞性 shell 操作。

7. Trip Meter 的 debugging evidence

Build
  → Gradle task result

Install
  → adb / Android Studio deploy output

Launch
  → process exists?

Permission
  → permission state/result

Location
  → timestamp / accuracy / provider

Domain
  → accepted sample / distance delta

UI
  → rendered TripScreenState

把 evidence 分層後,「手機上數字不動」才能從模糊現象變成可定位問題。

8. Project checkpoint:Trip Meter Android Shell v3

最終 architecture:

Android UI
   ↓ user intent
ViewModel
   ↓
permission state
   ↓
location provider
   ↓ LocationSample
Trip domain / repository
   ↓ TripState
ViewModel StateFlow
   ↓
UI

Build path:
source
 → Gradle
 → APK
 → install
 → app process
 → Logcat evidence

9. 107–112 最後真正學到什麼?

課核心能力
107Kotlin typed domain functions
108null safety / data model / collections
109coroutines / cancellation
110lifecycle / ViewModel / UI state
111permission / location evidence
112build / install / runtime debugging

Knowledge check / Final challenge

  1. 畫出「按 Start → 取得 location → 更新 fare UI」的完整流程。
  2. 每層標出可能 failure 與 evidence。
  3. 模擬 permission denied、location poor accuracy、repository error 三種不同 state。
  4. 解釋 build success 為什麼不等於 runtime success。