事象
CordovaプロジェクトをAndroid用にビルドしようとしたところ、以下のようなエラーに遭遇。
$ cordova build android
...
> Task :app:processDebugMainManifest FAILED
<my-app-dir>/platforms/android/app/src/main/AndroidManifest.xml:17:5-90 Error:
Element uses-permission#android.permission.CAMERA at AndroidManifest.xml:17:5-90 duplicated with element declared at AndroidManifest.xml:16:5-65
<my-app-dir>/platforms/android/app/src/main/AndroidManifest.xml Error:
Validation failed, exiting
See http://g.co/androidstudio/manifest-merger for more information about the manifest merger.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed with multiple errors, see logs
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org`
...
上記エラー文で大事なのはこの部分
Element uses-permission#android.permission.CAMERA at AndroidManifest.xml:17:5-90 duplicated with element declared at AndroidManifest.xml:16:5-65
つまりは "AndroidManifest.xml 内で uses-permission#android.permission.CAMERA の要素が重複してますよ" ってこと。
原因
調べてみたところ、自分のプロジェクトで使用する以下2つのプラグイン内でそれぞれカメラアクセスを求めており、その記述方法が微妙に異なっていたため、両方が AndroidManifest.xml に書き込まれてしまいエラーの原因になっていた。
- cordova-plugin-qrscanner
plugins/cordova-plugin-qrscanner/plugin.xml
<uses-permission android:name="android.permission.CAMERA" android:required="false" />
- cordova-plugin-flashlight
plugins/cordova-plugin-flashlight/plugin.xml
<uses-permission android:name="android.permission.CAMERA" />
このくらいCordova側でうまく調整してほしいが無理みたい。
解決方法
まずは各プラグインのplugin.xmlの記述方法を統一。今回は cordova-plugin-qrscanner を修正。
plugins/cordova-plugin-qrscanner/plugin.xml
<!-- <uses-permission android:name="android.permission.CAMERA" android:required="false" /> -->
<uses-permission android:name="android.permission.CAMERA" />
次に変更を反映させるためにAndroidプラットフォームを一度削除してから再追加。
$ cordova platforms remove android
$ cordova platforms add android
これで改めてビルドすればうまくいくはず。