在 Android 12 手機上安裝,會發生 INSTALL_FAILED_VERIFICATION_FAILURE

前言

目前編譯版本 compileSdk 31 ,直接安裝運行在 Android 12 手機或模擬器上會發生錯誤:

Installation did not succeed.
The application could not be installed: INSTALL_FAILED_VERIFICATION_FAILURE

或是

Manifest merger failed : android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for android:exported when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

HKT 講師,相關課程

HKT 講師,相關課程:
🎬 從零開始學 Swift 程式設計 免費講義
https://bit.ly/3ekjsEP
🎬 從零開始學 SwiftUI 程式設計 免費講義
https://bit.ly/3xCbUUp
🎬Android 入門開發實戰:口罩地圖(Kotlin) 免費講義
https://bit.ly/2KKZcju
🎬從零開始學 Dart 程式設計 免費講義
https://bit.ly/2OJW6hl
🎬 Flutter 程式設計入門實戰 免費講義
https://bit.ly/37L47Ij
🎬從零開始學 Java 程式設計 免費講義
https://bit.ly/2WlCn6y
🎬 從零開始學 kotlin 程式設計 免費講義
https://bit.ly/2Kx7GrM

解決方式

這個原因是因為 Android 12,調整了這部分的安全性。
目前只要 andmanifest.xml 包含 <intent-filter> 的 activity 、 service 和 receiver,都需要特別宣告 android:exported 這個屬性值。當然如果你沒有「intent-filter」相關設定,就不會踩到這個升級上來或初建專案的雷。

而其中 android:exported 屬性值為布林值,設定方式:

  • true,表示可以接受外部其他 APP 訪問使用。
  • false,表示不能接受外部其他 APP 訪問使用。

android:exported 設定範例

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

參考資料

Behavior changes: Apps targeting Android 12
https://developer.android.com/about/versions/12/behavior-changes-12

Safer component exporting
https://developer.android.com/about/versions/12/behavior-changes-12#exported

贊助我們

創作不易,知識無價,免費線上教學就像顆種子,希望藉由您的支持與贊助,能夠無後顧之憂的日漸茁壯,努力前行堅持下去。不論捐贈金額的大小,我們都由衷的感謝每位贊助者,都是我們推廣知識、開放共享知識最大的動力!

您的捐贈將用於:請作者喝杯咖啡,鼓勵繼續創作,持續上傳教學影片與更多新技術文章。

Line Pay 打賞


(由 Line Pay 支付平台,提供轉帳服務)

街口打賞


(由街口行動支付平台,提供轉帳服務)

超商代碼繳費打賞

(由綠界科技支付平台,提供超商繳費代碼)

相關連結

HKT 線上教室 每週六日 更新影片
▶ YouTube 頻道
https://goo.gl/3f2pJi
▶ KT 線上教室 臉書粉絲團
https://goo.gl/27H9Li
▶ Udemy 頻道
http://bit.ly/2ZNdnrt
▶ 贊助我們
https://goo.gl/FiKXAu

AndroidStudio / Xcode
Java / Kotlin / Dart / Flutter
APP / AppDevloper
Android / AndroidDevloper
iOS / iOSDeveloper / Swift / SwiftUI
Programer

這個網誌中的熱門文章

[轉貼]傳說中的100句英語可以幫你背7000單詞

[推薦] 翻譯機 (無敵 CD-318、CD-222 電子辭典)

[推薦]目前當代具有相當權威的英語辭典整理收集

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

16天記下7000單字