apache / cordova-common

Apache Cordova Common Tooling Library
https://cordova.apache.org/
Apache License 2.0
39 stars 46 forks source link

Unable to graft xml at selector "manifest/uses-sdk" #151

Open breautek opened 4 years ago

breautek commented 4 years ago

Bug Report

Problem

What is expected to happen?

<edit-config> to apply changes to AndroidManifest.xml

What does actually happen?

An error occurs and the build fails.

Information

Error stacktrace:

Unable to graft xml at selector "/manifest/uses-sdk" from "C:\Users\norman\development\gradletest\platforms\android\app\src\main\AndroidManifest.xml" during config install
Error: Unable to graft xml at selector "/manifest/uses-sdk" from "C:\Users\norman\development\gradletest\platforms\android\app\src\main\AndroidManifest.xml" during config install
    at ConfigFile_graft_child [as graft_child] (C:\Users\norman\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-common\src\ConfigChanges\ConfigFile.js:120:19)
    at PlatformMunger_apply_file_munge [as apply_file_munge] (C:\Users\norman\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:81:34)
    at munge_helper (C:\Users\norman\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:238:14)
    at PlatformMunger.add_config_changes (C:\Users\norman\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:216:12)
    at C:\Users\norman\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\src\cordova\prepare.js:112:32
    at async Promise.all (index 0)

Command or Code

Use cordova create to build a simple hello world project.

Inside the config.xml add:

<edit-config file="AndroidManifest.xml" mode="overwrite" target="/manifest/uses-sdk">
    <uses-sdk android:minSdkVersion="16" android:maxSdkVersion="23" />
</edit-config>

This is a copied and paste example from the docs. But this appears to occur when having target="/manifest/uses-sdk", regardless of the body of <edit-config> block. This also fails if mode="merge" is set.

Do note that the following does not fail:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest">
    <manifest xmlns:tools="http://schemas.android.com/tools" />
</edit-config>

This leads me to believe perhaps this occurs if the AndroidManifest.xml does not have the target node.

Finally run: cordova build android to observe the failure.

Environment, Platform, Device

Windows 10 Cordova-Android 9

Version information

Windows 10 cordova: 9.0.0 (cordova-lib@9.0.1)

Checklist

erisu commented 4 years ago

I believe we should remove this from documentation.

We don't set the uses-sdk setting anymore and try to keep everything inside the Gradle.

If you try and set the minSDK, like the example you wrote above, Android Studio will complain:

The minSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file. Move minSdkVersion to build file and sync project Affected Modules: app

I think the other settings does not get these sync warnings but managing the settings in Gradle and in the Manifest was maybe a bit excessive maybe? So I believe it was decided to do only Gradle.

breautek commented 4 years ago

Yah, the documentation could have been removed.

But <uses-sdk> tag itself isn't deprecated and there is still some potential use cases for it. Such as overriding libraries via tools:overrideLibrary attribute. Which may be necessary depending libraries being imported into the android project.

bhandaribhumin commented 2 years ago

@breautek facing similar issue Unable to graft xml at selector "/manifest/application/activity[@android:theme='@style/Theme.AppCompat.NoActionBar']" from "/Users/bhuminbhandari/Desktop/projects/app/platforms/android/app/src/main/AndroidManifest.xml" during config install

First time works but second time when we run cordova build android it will show above error.

Script

 <edit-config  mode="merge or try overwrite"   file="app/src/main/AndroidManifest.xml" target="/manifest/application/activity[@android:theme='@style/Theme.AppCompat.NoActionBar']" >
            <activity android:theme="@style/RemoveSystemSplashScreen"></activity>
        </edit-config>
VeaceslavB commented 1 year ago

Have similar issue 11:52:55 Android project created with cordova-android@12.0.0 11:52:55 Unable to graft xml at selector "/manifest/uses-permission[@android:name='android.permission.WRITE_EXTERNAL_STORAGE']" from "/private/var/folders/wv/d7j3q171735794lxc610xqy80000gp/T/tmp.gO8bxWCU/mobile/cordova/platforms/android/app/src/main/AndroidManifest.xml" during config install

XML code: `

    </edit-config>`
jessyefuster commented 1 year ago

@VeaceslavB this could be off topic but by any chance, does this happens to you because of the last release of cordova-plugin-camera ? In my case I just upgraded to 7.0.0 which requires this permission with android:maxSdkVersion attribut set, while other plugins within my project require the permission as is without any attributes. This seems to cause conflicts in the AndroidManifest.xml To resolve the issue and workaround edit-config, you could write a custom hook to manually modify the file and resolve those conflicts

contfedorov commented 8 months ago

Faced similar issue recently. Made the following update in my config.xml several months ago:

<!-- add required XML namespace -->
<edit-config file="app/src/main/AndroidManifest.xml" target="/manifest" mode="merge">
        <manifest xmlns:tools="http://schemas.android.com/tools"/>
</edit-config>

<!-- add replace attr for permission tag -->
<edit-config file="app/src/main/AndroidManifest.xml" target="/manifest/uses-permission[@android:maxSdkVersion='32']" mode="merge">
        <uses-permission tools:replace="android:maxSdkVersion"/>
</edit-config>

Also I have the following string replacement in Android after_prepare hook (as a workaround recommended in this comment):

manifest = manifest.replace(/^(\s)+<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" \/>$/gm, '');

For some time it worked well. Yesterday I've updated my Android SDK, Build Tools and Command-line Tools to latest, and received Unable to graft xml at selector "/manifest/uses-permission[@android:maxSdkVersion='32']" from "/Users/.../platforms/android/app/src/main/AndroidManifest.xml" during config install while executing cordova prepare android.

Rolling back Android-related staff didn't help.