- Create a Connected App in Salesforce instance where we maintain namespace.
- Create a first-generation managed package (1GP) and add the connected app. It’s fine if the connected app is the only component in the package. Always use the same namespace as the 2GP package for the 1GP package.
- Take note of the version number of the connected app; this number is needed later.
- Upload the 1GP Package to create a package version.
- Promote the 1GP version to the released state.
- Promoting the 1GP version allows the connected app to be included in a second-generation managed package. We don’t need to install the 1GP version into an org.
- Now in our source code navigate to folder "connectedApps" where we are trying to generate 2GP Package
and the body of the XML file should be as shown below.
<ConnectedApp xmlns="http://soap.sforce.com/2006/04/metadata">
<developerName><namespace>__<connected_app_Name></developerName>
<label>A Connected App</label>
<version>1.0</version>
</ConnectedApp>
Now generate a 2GP package and promote. Then the connected app is automatically added to your 2GP Package.
The version specified in the source file is the version number of the connected app. Use decimal formatting when specifying the version number. The version number must match the version number of the connected app before it was added to the 1GP managed package.
Note:
When you add a connected app to a 1GP package and upload the package, the version number of the connected app is auto-incremented. For example, when version 1.0 of a connected app is added to a 1GP package, the package version increments the version number of the connected app from 1.0 to 2.0. When creating the source file for your 2GP package, specify the version number of the connected app before it was uploaded into a 1GP package, in this case, 1.0.
Reference:
https://developer.salesforce.com/docs/atlas.en-us.224.0.sfdx_dev.meta/sfdx_dev/sfdx_dev_dev2gp_connected_app.htm