The best practice is to package up your connected app along with the other components in 2GP.
Now the way you package up the Connected App in 2GP is different, I will go down the steps here,
Create a 1GP Managed package in your org where you have the namespace. This package can include only the Connected App. The package needs to be Managed and Released.
Note down the developer name of the Connected app
<connected app developer name> and the namespace
namespace of your org where you have a 2GP package.
Create a folder named
connectedApps in the source directory.
Create a file in the folder named
connectedApps with the naming convention
<connected app developer name>.connectedApp-meta.xml
Make sure the contents of the file as below
<?xml version="1.0" encoding="UTF-8"?>
<developerName><namespace>__<connected app developer name></developerName>
<label><Connected App Label></label>
Create package and package versions.
The above is also documented here but it is not clear so I had to hands-on dig in. I will reach the team who has it documented to make sure this is correct.
Also make sure to follow the below note carefully
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 4.0 of a connected app is added to a 1GP package, the package version increments the version number of the connected app from 4.0 to 5.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, 4.0.
For Scratch Orgs
Create a 1GP package with a different namespace for testing it. Install the 1GP package in your scratch orgs to use the connected app, you can install using the below command. You need the package Id and use it in the below command.
sfdx force:package:install --package "04t...."