Manually selecting library variant by forcing attribute doesn't work

I’m trying to experiment with the latest Gradle 8.0 nightlies and Kotlin 1.7.20-Beta

I have the following in my buildSrc/build.gradle.kts:

plugins {
    kotlin("jvm") version "1.7.20-Beta"
    id("org.gradle.kotlin.kotlin-dsl") version "3.1.0"
}

repositories {
    mavenCentral()
}

val kotlinVersion = "1.7.20-Beta"

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin-api:$kotlinVersion") {
        // Require variant
        attributes {
            attribute(Attribute.of("org.gradle.plugin.api.version", String::class.java), "7.1")
        }
    }
}

However I get this error:

  • Cause 1
Cause 1: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta.
Required by:
    project :buildSrc > org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.7.20-Beta > org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20-Beta
Caused by: org.gradle.internal.component.IncompatibleVariantsSelectionException: Multiple incompatible variants of org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta were selected:
   - Variant org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta variant gradle70RuntimeElements has attributes {org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.plugin.api-version=7.0, org.gradle.status=release, org.gradle.usage=java-runtime}
   - Variant org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta variant gradle71RuntimeElements has attributes {org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.plugin.api-version=7.1, org.gradle.status=release, org.gradle.usage=java-runtime}
  • Cause 2
Cause 2: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta.
Required by:
    project :buildSrc > org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.7.20-Beta > org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20-Beta
    project :buildSrc > org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.7.20-Beta > org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20-Beta > org.jetbrains.kotlin:kotlin-gradle-plugin-model:1.7.20-Beta
Caused by: org.gradle.internal.component.IncompatibleVariantsSelectionException: Multiple incompatible variants of org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta were selected:
   - Variant org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta variant gradle70RuntimeElements has attributes {org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.plugin.api-version=7.0, org.gradle.status=release, org.gradle.usage=java-runtime}
   - Variant org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta variant gradle71RuntimeElements has attributes {org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.plugin.api-version=7.1, org.gradle.status=release, org.gradle.usage=java-runtime}
  • Cause 3
Cause 3: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.10.
Required by:
    project :buildSrc > org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:3.1.0 > org.gradle.kotlin:gradle-kotlin-dsl-plugins:3.1.0 > org.jetbrains.kotlin:kotlin-sam-with-receiver:1.7.10
Caused by: org.gradle.internal.component.IncompatibleVariantsSelectionException: Multiple incompatible variants of org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta were selected:
   - Variant org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta variant gradle70RuntimeElements has attributes {org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.plugin.api-version=7.0, org.gradle.status=release, org.gradle.usage=java-runtime}
   - Variant org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.7.20-Beta variant gradle71RuntimeElements has attributes {org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.plugin.api-version=7.1, org.gradle.status=release, org.gradle.usage=java-runtime}

What is the proper way to do this?
Thank you =)

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.