Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Android duplicated uses-feature from two plugins

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

like image 329
Automatico Avatar asked Dec 29 '15 12:12

Automatico


3 Answers

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
like image 152
CaioVncius Avatar answered Oct 20 '22 09:10

CaioVncius


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

like image 25
Mathieu Castets Avatar answered Oct 20 '22 08:10

Mathieu Castets


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

like image 6
marcozabo Avatar answered Oct 20 '22 09:10

marcozabo