目錄 |
場景:
正在按《第一行代碼——Android(第3版)》寫Kotlin代碼,寫了一個 HelloWorld, 點擊“運行”時報錯
錯誤提示:
> Task :app:processDebugAndroidTestManifest FAILED E:\AndroidProject\app\build\intermediates\tmp\manifest\androidTest\debug\tempFile1ProcessTestManifest1257258968881101446.xml Error: 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. E:\AndroidProject\app\build\intermediates\tmp\manifest\androidTest\debug\tempFile1ProcessTestManifest1257258968881101446.xml Error: 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. E:\AndroidProject\app\build\intermediates\tmp\manifest\androidTest\debug\tempFile1ProcessTestManifest1257258968881101446.xml Error: 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.
解決思路:
(1)通過以 --debug 選項運行,重新查看更加詳細的錯誤信息 (點擊下圖 --debug option,將自動再以該選項運行)
(2)得到更加詳細的錯誤提示:
2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] Merging library manifest C:\Users\BensonLaur\.gradle\caches\transforms-3\ee2f476fa0c3550d01ea1b26bf917f27\transformed\monitor-1.3.0\AndroidManifest.xml 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] Merging manifest with lower [androidx.test:monitor:1.3.0] AndroidManifest.xml:17:1-24:12 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] uses-sdk defined in both files... 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] Merging uses-sdk with lower [androidx.test:monitor:1.3.0] AndroidManifest.xml:20:5-22:41 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] Merging library manifest C:\Users\BensonLaur\.gradle\caches\transforms-3\eba7c60eedbe1207821675fbca04aee1\transformed\espresso-idling-resource-3.3.0\AndroidManifest.xml 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] Merging manifest with lower [androidx.test.espresso:espresso-idling-resource:3.3.0] AndroidManifest.xml:17:1-24:12 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] uses-sdk defined in both files... 2021-11-12T07:57:44.384+0800 [INFO] [org.gradle.api.Task] Merging uses-sdk with lower [androidx.test.espresso:espresso-idling-resource:3.3.0] AndroidManifest.xml:20:5-22:41 2021-11-12T07:57:44.385+0800 [ERROR] [org.gradle.api.Task] E:\AndroidProject\app\build\intermediates\tmp\manifest\androidTest\debug\tempFile1ProcessTestManifest1866872685080210847.xml Error: 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. 2021-11-12T07:57:44.385+0800 [ERROR] [org.gradle.api.Task] E:\AndroidProject\app\build\intermediates\tmp\manifest\androidTest\debug\tempFile1ProcessTestManifest1866872685080210847.xml Error: 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. 2021-11-12T07:57:44.385+0800 [ERROR] [org.gradle.api.Task] E:\AndroidProject\app\build\intermediates\tmp\manifest\androidTest\debug\tempFile1ProcessTestManifest1866872685080210847.xml Error: 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. 2021-11-12T07:57:44.385+0800 [INFO] [org.gradle.api.Task] -- Merging decision tree log ---
(3)分析錯誤
錯誤直接來自一個臨時文件:tempFile1ProcessTestManifest1866872685080210847.xml (該文件應該是前序步驟生成的,因為是中間文件,思路上不應該直接修改,因為修改了可能也沒用)
點擊上圖錯誤前的第一個提示的 “AndroidManifest.xml” : espresso-idling-resource-3.3.0\AndroidManifest.xml
進入對應的文件中:
觀察到,多個地方提到 使用的 sdk 的問題(uses-sdk),而且具體提示了:uses-sdk defined in both files...
注意到 espresso-idling-resource-3.3.0\AndroidManifest.xml 里面的 targetSdkVersion 是 28
而我自己項目里的 build.gradle 是指定了 targetSdk 31
(4)推測是 sdk 版本兼容問題,將項目 targetSdk 改為 28
問題解決:
解決思路參考:https://blog.csdn.net/Juice_Lover/article/details/120076991