A demo project for changing an app's launcher icon dynamically at runtime.
一個展示「執行階段動態更換 App Icon」的範例專案。
This repo is the companion code for a write-up that explains the why and how in depth (research process, design decisions, and pitfalls):
完整的原理講解、設計思路與踩雷紀錄請見文章:
| App UI | Demo |
|---|---|
The icon is not swapped by changing android:icon at runtime. Instead, the app declares
one <activity-alias> per icon variant (all pointing at the same MainActivity), and toggles
which alias is enabled via PackageManager.setComponentEnabledSetting(). Exactly one
launcher-visible component is enabled at a time.
App Icon 並不是直接在執行階段改 android:icon,而是在 AndroidManifest.xml 為每種 Icon
建立一個 <activity-alias>(都指向同一個 MainActivity),再用
PackageManager.setComponentEnabledSetting() 切換要啟用哪一個 alias。同一時間只會有一個
launcher 可見的 component 被啟用。
詳細狀態切換邏輯(例如為什麼切換時用
COMPONENT_ENABLED_STATE_DEFAULT而非DISABLED)請參考上方文章與AppIconManager。
git clone https://github.com/dh46tw/android-dynamic-app-icon-demo.gitOpen the project in Android Studio, or build and install from the command line:
用 Android Studio 開啟,或以指令建置安裝:
./gradlew installDebugRun on a real device or emulator: tap Activate to enable the feature first, then pick an icon color.
請在實機或模擬器上操作:先按 Activate 啟用功能,再點選想要的 Icon 顏色。
minSdk26 /compileSdk&targetSdk34- Kotlin + View Binding(非 Jetpack Compose)
- After switching to another alias, re-building from the IDE may fail with an "activity not found" error. 切換成其他 alias 後,直接用 IDE 重新 build App 會出現「找不到 Activity」的錯誤。
- Don't remove aliases between releases, or users may crash on update install when the previously-enabled activity no longer exists. 更版時請勿隨意刪除 alias 列表,否則使用者更新安裝後可能因找不到 activity 而閃退。
For more caveats and launcher-specific behavior differences, see the Known Issues section of the article. 更多注意事項與不同 launcher 的行為差異,請見上方文章的「已知問題」章節。
Released under the MIT License.