錯誤訊息:
Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_launcher').
原因:**appcompat-v7:21.0.0+ 需要 Android 5.0 (API 21) 編譯環境**,但專案 compileSdkVersion 可能設為 19 或更低,導致資源找不到。
android {
compileSdkVersion 34 // 或最新穩定版
defaultConfig {
targetSdkVersion 34
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1' // 現代 AndroidX 取代舊 support library
}
注意:AndroidX 要求 compileSdkVersion 28+,且專案需遷移至 AndroidX (Refactor → Migrate to AndroidX)。
android {
compileSdkVersion 19
defaultConfig {
targetSdkVersion 19
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:19.1.0' // 固定 19.x 版本
}
compile 'com.android.support:appcompat-v7:21.0.0' 搭配 compileSdkVersion 19 → **必報錯**。android.support.* 與 androidx.* → 需完整遷移或統一回舊版。File → Invalidate Caches / Restart 或刪除 .gradle、build 目錄重建。核心原則:**compileSdkVersion ≥ 依賴庫要求的最低 API 等級**,且支援庫版本與編譯版本對應(v7:19.x 對應 API 19,v7:21.x+ 對應 API 21+,AndroidX 對應 API 28+)。