Unable to graft xml at selector "manifest/uses-sdk"
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 ([email protected])
Checklist
- [x] I searched for existing GitHub issues
- [x] I updated all Cordova tooling to most recent version
- [x] I included all the necessary information above
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.
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.
@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>
Have similar issue 11:52:55 Android project created with [email protected] 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 file="app/src/main/AndroidManifest.xml" mode="overwrite" target="/manifest/uses-permission[@android:name='android.permission.WRITE_EXTERNAL_STORAGE']"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" /> </edit-config>
@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
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.
Adding update to my previous comment.
Since I first faced this issue, I did several updates (macOS, Node.js, Cordova, cordova-android, etc.) Not sure what helped, but now I sometimes don't see Unable to graft xml at selector error. <edit-config> in config.xml works as expected. I've noted that removing platforms and plugins folders (not platforms or platforms/android only) leads to correct generation of Android project. Otherwise error may occure again.
Here is my setup (partial cordova info output).
Cordova Packages:
cli: 12.0.0
common: 5.0.0
create: 5.0.0
lib: 12.0.2
common: 5.0.0
fetch: 4.0.0
serve: 4.0.1
Project Installed Platforms:
android: 13.0.0
Environment:
OS: macOS Sequoia 15.0.1 (24A348) (darwin 24.0.0) x64
Node: v18.17.0
npm: 9.6.7