Cordova Android duplicated uses-feature from two plugins
Asked Answered
C

12

27

I am using two different plugins into cordova, which both have the same uses-feature, one with android:required="false" and one without.

This results in an error upon build:

processDebugManifest
/path/to/project/platforms/android/AndroidManifest.xml:31:5 Error:
    Element uses-feature#android.hardware.camera at AndroidManifest.xml:31:5 duplicated with element declared at AndroidManifest.xml:27:5
/path/to/project/platforms/android/AndroidManifest.xml:32:5 Error:
    Element uses-feature#android.hardware.camera.autofocus at AndroidManifest.xml:32:5 duplicated with element declared at AndroidManifest.xml:28:5
/path/to/project/platforms/android/AndroidManifest.xml:0:0 Error:
    Validation failed, exiting
:processDebugManifest FAILED
.....
ERROR building one of the platforms: Error: /path/to/project/platforms/android/cordova/build: Command failed with exit code 1
You may not have the required environment or OS to build this project

The compiled manifest has the following when built:

...
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
    <uses-feature android:name="android.hardware.camera.flash" android:required="false" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
...

Is there anything I can do to fix this?


cordova version 5.4.1

Cariecaries answered 29/12, 2015 at 12:8 Comment(0)
N
15

None of the above solutions were satisfying because you need to modify some generated output or tweak some plugins. There are only "temporary" fixes. We should be able to solve this issue by only modifying our source code.

Since Cordova 6.4+, we can take advantage of the edit-config tag in the config.xml file. The following solution is also great for CI and automated builds.

It has been tested for a similar scenario where two cordova plugins defined the same <uses-feature> name:

/Users/me/dev/wkspace/project/cordova/platforms/android/app/src/main/AndroidManifest.xml:55:5-66 Error:
        Element uses-feature#android.hardware.location.gps at AndroidManifest.xml:55:5-66 duplicated with element declared at AndroidManifest.xml:50:5-90
/Users/me/dev/wkspace/project/cordova/platforms/android/app/src/main/AndroidManifest.xml Error:
        Validation failed, exiting

AndroidManifest.xml snippet:

    <!-- ... -->
    <uses-feature android:name="android.hardware.location.gps" android:required="true" />
    <uses-feature android:name="android.hardware.location.gps" />
    <!-- ... -->

1st solution:

Within config.xml:

   <platform name="android">
        <!-- ... -->
        <edit-config file="app/src/main/AndroidManifest.xml" mode="overwrite" target="/manifest/uses-feature[@android:name='android.hardware.location.gps']">
            <uses-feature android:name="android.hardware.location.gps" />
        </edit-config>
   </platform>

When building, errors are gone.

So, as for the issue described by author, you could try to add the following in the config.xml:

<edit-config file="app/src/main/AndroidManifest.xml" mode="overwrite" target="/manifest/uses-feature[@android:name='android.hardware.camera']">
    <uses-feature android:name="android.hardware.camera" />
</edit-config>

<edit-config file="app/src/main/AndroidManifest.xml" mode="overwrite" target="/manifest/uses-feature[@android:name='android.hardware.camera.autofocus']">
    <uses-feature android:name="android.hardware.camera.autofocus" />
</edit-config>

Drawback (thanks to @jamsandwich for pointing this out): you still get a warning when opening the AndroidManifest.xml file as you now have two duplicate nodes. But it's only a warning and build should now passed.

2nd solution:

Simply fork the cordova-plugin-googlemaps and comment the section that defines the <use-feature tag within the plugin.xml file:

<uses-feature android:name="android.hardware.location.gps"/>

becomes

<!--      Comment android.hardware.location.gps as build fails when used along with cordova-plugin-geolocation -->
<!--      <uses-feature android:name="android.hardware.location.gps"/>-->

Then, do not forget to update your package.json to link to our foked version.

-- Both solutions have been successfully tested with cordova-android@8 and cordova-android@9

Niveous answered 24/11, 2020 at 21:49 Comment(2)
I get the following error when attempting to use your fix described above: Unable to graft xml at selector "/manifest/uses-feature[@android:name='android.hardware.location.gps']" from ".../platforms/android/app/src/main/AndroidManifest.xml" during config install did you encounter this?Tauro
In my experience this only overwrites the first matching node, and so there is still a duplicate.Obtrude
N
14

I had the same issue with cordova-plugin-camera and phonegap-plugin-barcodescanner. My fix:

ionic cordova platform rm android
ionic cordova platform rm ios    
ionic cordova plugin rm phonegap-plugin-barcodescanner
rm -r plugins
rm -r node_modules
rm package-lock.json

Next remove the phonegap-plugin-barcodescanner of the package.json. Run:

npm install
ionic cordova platform add android

Next do a new build:

ionic cordova run android

Next add the plugin again:

ionic cordova plugin add phonegap-plugin-barcodescanner
Newby answered 11/1, 2018 at 17:24 Comment(0)
H
7

1.Open plugins/[your plugin name]/plugin.xml

2.remove this line:

`<uses-feature android:name="android.hardware.camera" android:required="false" />`

3. rebuild your project

Herzegovina answered 24/2, 2016 at 9:14 Comment(5)
This is only a temporary fix. Any time I re-install the library, this problem will come back. I actually applied a similar "fix" to my project, but I think this is a problem in cordova where it does not check for attributes within uses-features statements.Cariecaries
I run into this every so often. Even manually removing the entry from plugin.xml doesn't always fix it. It's like it's pulling it live for the build, even though it doesn't update what's in the plugins folder. It's very frustrating.Histidine
Using Cordova 6.2.0, I had to remove duplicate lines from platforms/android/AndroidManifest.xml and platforms/android/android.json to get build working.Slovene
+1 for the recommendation, but this should be resolved by the Cordova plugin install script. We automate builds for several apps and this issue breaks the process.Astylar
I have tried removing that line in plugin.xml, but during the build process it adds it again, and then won't build because it is complaining about a duplicate camera entry in AndroidManifest.xmlPeriodontics
D
7

The following steps helped me solve this problem:

  1. Remove duplicate elements from your config.xml

  2. Remove duplicate objects from platform/android/android.json file.

  3. Remove duplicate elements from platform/android/app/src/main/AndroidManifest.xml file.

  4. Close your IDE/Text Editor (Specially if you are using VS Code).

  5. Now run 'cordova build android'.

Daughterly answered 10/8, 2018 at 14:15 Comment(0)
P
5
  1. In addition to removing duplicate lines in plugins.xml file, go to [your project]/platforms/android/android.json and remove duplicate lines in the file as well.

  2. Reopen your command terminal before compiling the project again.

Pharmacy answered 25/5, 2016 at 17:29 Comment(0)
C
4

Of all the answers here Mathieu Castets answer seemed to provide the best solution where you don't have to make any changes to AndroidManifest file, but unfortunately that didn't work for me, but for the same approach where you only have to modify source code can be acheived using cordova-custom-config:

Solution:

ionic cordova plugin add cordova-custom-config

Once the plugin is added add this to your config.xml:

For Uses-feature:

 <platform name="android">
  ....
 <custom-preference delete="true" name="android-manifest/uses- 
 feature[@android:name='android.hardware.camera']" />
 </platform>

Then run these commands:

ionic cordova prepare android
ionic cordova build android
Cacique answered 9/3, 2021 at 14:0 Comment(0)
B
2

This is how it finally worked for us in our Ionic 3 project. You should remove one of the duplicate entries from platforms/android/AndroidManifest.xml:

<manifest ...>
    ...
    <uses-feature android:name="android.hardware.camera"/>
    ...
    <uses-feature android:name="android.hardware.camera" android:required="true" />
</manifest>

And also from platforms/android/android.json:

{
  "xml": "<uses-feature android:name=\"android.hardware.camera\" />",
  "count": 1
},
...
{
  "xml": "<uses-feature android:name=\"android.hardware.camera\" android:required=\"true\" />",
  "count": 1
}

PS: We do this tweak only when adding the Android platform, not at every build.

Berlyn answered 18/10, 2018 at 16:4 Comment(2)
Not working, even android.json is recreated every time when we build.Hass
yes Chintan android.json is recreated based on AndroidManifest.xml. This is why you have to change both files at once. Did you do that ? Has always worked for me.Berlyn
S
0

This solution worked for me -

Open ~/platforms/android/android.json

Find the duplicate entry of permission and remove from anyone of the plugin there. Then open the AndroidManifest.xml and remove the duplicate entry from there as well.

Then run ionic cordova build android if using Ionic or simply cordova build android.

Hope this helps.

Scotney answered 6/9, 2022 at 13:27 Comment(0)
S
0

I faced similar issues with feature android.hardware.location.gps getting added two times. I fixed it this way -

  • Open <project root>/platforms/android/android.json
  • Search for gps (Name of Duplicate feature) you should see entries in Parent and Application Manifest.xml files there
  • Delete the one from Application keeping parent as is
  • Then open <project root>/platforms/android/app/src/main/AndroidManifest.xml
  • Check for features configured multiple times
  • Remove the duplicate entires

Hope this solves the issue.

Scotney answered 30/9, 2022 at 8:44 Comment(0)
E
0

I encountered the same challenge. I attempted to manually remove the declarations from the plugin.xml files, but that didn't resolve the issue. Ultimately, I used a Cordova after_prepare hook to eliminate the duplicates and created a Node module to handle it.

Learn more here.

Installation

   npm i -D @hollax/cordova-android-dup-perm-remover

Usage

  1. Add the following definition to config.xml to tell Cordova to run afterPrepare.js script before each platform build.
    <hook type="after_prepare" src="scripts/afterPrepare.js" />
  1. Create scripts/afterPrepare.js file and add the following implementation
const {afterPrepareHook} = require('@hollax/cordova-android-dup-perm-remover')
module.exports = afterPrepareHook;
  1. Run the project
   cordova run android
Encrinite answered 25/5, 2024 at 0:2 Comment(0)
O
-1

This is how it finally worked in config.xml just add one line code:

<platform name="android">
        ########################//existing code
        <resource-file src="google-services.json" target="app/google-services.json" />
</platform>
Overblouse answered 1/3, 2021 at 21:43 Comment(0)
T
-2
  1. Open plugins/[your plugin name]/plugin.xml
  2. remove this line: <uses-feature android:name="android.hardware.camera" android:required="false" />
  3. ionic cordova platform rm android
  4. npm install
  5. ionic cordova platform add android
Tonettetoney answered 21/5, 2021 at 17:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.