Manually selecting library variant by forcing attribute doesn't work

The attribute you require is org.gradle.plugin.api.version, but that is set on no variant, as what you mean is org.gradle.plugin.api-version. (dash vs. dot)

And besides of that, the type of org.gradle.plugin.api-version is not String, but GradlePluginApiVersion and you could use GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE directly instead of creating your own attribute.

But the “proper” way you ask about is to not specify it at all.
Gradle automatically uses the variant with the newest version that is still compatible, so the 7.1 variant would automatically be used if you use Gradle 8.