Skip to content

Failed to install '<plugin>': TypeError: Cannot read properties of undefined (reading 'id') #201

@GiovanniBattista

Description

@GiovanniBattista

Bug Report

Problem

I have created an internal cordova plugin at our company which adds the android:networkSecurityConfig attribute to the /manifest/application/ tag in AndroidManifest.xml via the following <edit-config:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
   <application android:networkSecurityConfig="@xml/uq_network_security_config" />
</edit-config>

This works flawlessly if the plugin is added the first time. But if the plugin is removed and then re-added again an error occurrs.

What is expected to happen?

The plugin should be added again without any issues.

What does actually happen?

If the plugin is re-added, the following error is produced:

Failed to install 'uq-cordova-plugin-settings': TypeError: Cannot read properties of undefined (reading 'id')
    at registerConflict (D:\CordovaApp\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:255:43)
    at D:\CordovaApp\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:283:34
    at Array.forEach (<anonymous>)
    at PlatformMunger._is_conflicting (D:\CordovaApp\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:259:21)
    at PlatformMunger.add_plugin_changes (D:\CordovaApp\node_modules\cordova-common\src\ConfigChanges\ConfigChanges.js:106:59)
    at D:\CordovaApp\node_modules\cordova-common\src\PluginManager.js:120:33
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Cannot read properties of undefined (reading 'id')

Information

The problem is that, upon removal, the platforms/android/android.json looks like the following:

"AndroidManifest.xml": {
  "parents": {
    "/*": [
      {
        "xml": "<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />",
        "count": 1
      },
      {
        "xml": "<queries><intent><action android:name=\"android.media.action.IMAGE_CAPTURE\" /></intent><intent><action android:name=\"android.intent.action.GET_CONTENT\" /></intent><intent><action android:name=\"android.intent.action.PICK\" /></intent><intent><action android:name=\"com.android.camera.action.CROP\" /><data android:mimeType=\"image/*\" android:scheme=\"content\" /></intent></queries>",
        "count": 1
      },
      ...
    ],
    "application": [],
    "/manifest/application": []
  }
}

Notice the empty array for /manifest/application.

The code in ConfigChanges.js registerConflict accesses the first entry in the array, which does not exist in this case. Therefore, the above error is being thrown.
image

With a slight adjustment to the code the above error could be avoided, but it leads to another error ("There was a conflict trying to modify attributes with") which is a different issue, I guess.
image

Command or Code

Create a new cordova project, create a new plugin directory with the <edit-config lines from the beginning of the issue.

cordova create hello com.example.hello HelloWorld
mkdir cordova-plugin-test
# add plugin.xml and package.json to cordova-plugin-test
cd hello
cordova platform add android@12.0.0
cordova plugin add ..\cordova-plugin-test
cordova plugin remove cordova-plugin-test
cordova plugin add ..\cordova-plugin-test
# => produces the error

A minimum viable plugin.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        id="cordova-plugin-test" version="0.0.1">
    <name>Test</name>
    <description>Cordova Device Test</description>
    <license>Apache 2.0</license>
    <keywords>cordova,test</keywords>

    <platform name="android">
      <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:networkSecurityConfig="@xml/uq_network_security_config" />
        </edit-config>
    </platform>
</plugin>

The package.json looks like this:

{
  "name": "cordova-plugin-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Environment, Platform, Device

Environment: Windows 10 with PowerShell

Version information

Cordova-Cli: 12.0.0 (cordova-lib@12.0.1)
Cordova Platform Android: 12.0.0
Windows 10
PowerShell 7

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions