Cordova构建报错:Manifest合并失败(adobe.phonegap.push插件问题)
问题
在Cordova项目中使用phonegap-plugin-push 2.3.0版本时,遇到Manifest合并失败问题。为满足Android要求给service添加android:exported="true"属性后,构建时出现服务重复声明的报错。
报错信息
> Task :app:processReleaseManifest FAILED D:\AndroidStudioProjects\recono\platforms\android\app\src\main\AndroidManifest.xml:29:9-33:19 Error: Element service#com.adobe.phonegap.push.FCMService at AndroidManifest.xml:29:9-33:19 duplicated with element declared at AndroidManifest.xml:19:9-23:19 D:\AndroidStudioProjects\recono\platforms\android\app\src\main\AndroidManifest.xml:34:9-38:19 Error: Element service#com.adobe.phonegap.push.PushInstanceIDListenerService at AndroidManifest.xml:34:9-38:19 duplicated with element declared at AndroidManifest.xml:24:9-28:19 D:\AndroidStudioProjects\recono\platforms\android\app\src\main\AndroidManifest.xml Error: Validation failed, exiting
现有config.xml配置
<platform name="android"> <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> <application android:usesCleartextTraffic="true" /> </edit-config> <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> <activity android:exported="true" /> </edit-config> <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> <service android:exported="true" /> </edit-config> </platform> <plugin name="phonegap-plugin-push" source="npm" spec="2.3.0"> <variable name="SENDER_ID" value="656893750954" /> </plugin> <platform name="android"> <resource-file src="google-services.json" target="app/google-services.json" /> </platform> <platform name="ios"> <resource-file src="GoogleService-Info.plist" /> </platform>
构建后AndroidManifest.xml相关片段
<application android:exported="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:usesCleartextTraffic="true"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:exported="true" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize"> <intent-filter android:label="@string/launcher_name"> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" /> </provider> <meta-data android:name="launchnavigator.GOOGLE_API_KEY" android:value="" /> <activity android:exported="true" android:name="com.adobe.phonegap.push.PushHandlerActivity" android:permission="${applicationId}.permission.PushHandlerActivity" /> <receiver android:name="com.adobe.phonegap.push.BackgroundActionButtonHandler" /> <receiver android:name="com.adobe.phonegap.push.PushDismissedHandler" /> <service android:exported="true" android:name="com.adobe.phonegap.push.FCMService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:exported="true" android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> <service android:name="com.adobe.phonegap.push.FCMService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <service android:name="com.adobe.phonegap.push.PushInstanceIDListenerService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> </intent-filter> </service> </application>
解决方案
1. 删除错误的全局service配置
先移除config.xml中这段全局添加service的edit-config,它会导致新增重复的service节点:
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> <service android:exported="true" /> </edit-config>
2. 精准给插件的service添加exported属性
替换为针对插件自带service的精准定位配置,确保只修改已有节点而非新增:
<platform name="android"> <!-- 保留原有application和activity的edit-config --> <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application/service[@name='com.adobe.phonegap.push.FCMService']"> <service android:exported="true" /> </edit-config> <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application/service[@name='com.adobe.phonegap.push.PushInstanceIDListenerService']"> <service android:exported="true" /> </edit-config> </platform>
3. 清理并重新构建
执行命令清理旧构建文件,重新生成Android项目:
cordova clean android cordova build android --release
核心原因说明
原配置中直接给application节点添加service标签,Cordova会将其解析为新增一个service节点,而非修改插件已经声明的service,最终导致Manifest中出现重复的FCMService和PushInstanceIDListenerService。使用XPath精准定位目标service后,就能实现对原有节点的属性修改,避免重复。
内容的提问来源于stack exchange,提问作者AndriiBal
相关产品推荐
相关产品推荐

