How to add a dependency block in *.gradle.kts as plugin?

In buildSrc, i add a deps.gradle.kts or deps.kt as a gradle plugin. every function in it has a dependency block so i can use the function to add a couple of dependencies.but the implementation key word is error(below in the picture)


I dont know how to make the implementation take effect,any ideas?

in 8.10 and before i use those code ,it works well

import gradle.kotlin.dsl.accessors._a31a802bb49e4298771372939c4853ad.androidTestImplementation
import gradle.kotlin.dsl.accessors._a31a802bb49e4298771372939c4853ad.debugImplementation
import gradle.kotlin.dsl.accessors._a31a802bb49e4298771372939c4853ad.implementation
import gradle.kotlin.dsl.accessors._a31a802bb49e4298771372939c4853ad.testImplementation

dependencies {
  implementation("com.tencent:mmkv-static:1.3.3")
}

while 8.12,it does not.

oh, it’s my fault, i didnt clean cache to let the kotlin-dsl-precompiled-script-plugins generate new accessors. when i cleaned cache and build, it’s ok

The ...accessors._a31a802bb49e4298771372939c4853ad... does not work well and is a big error your IDE might have added through automatically adding imports.

Using those accessors with hash in them is always the wrong thing to do, as the hash will change as soon as there is any change in the classpath.

In a precompile script plugin the accessors will be available if you apply a plugin that adds those configurations. If not, or you are in a .kt plugin, you do not have access to the accessors and need to get the configurations differently like

val implementation by configurations.existing
dependenices {
    implementation(...)
}

or similar.

If all you want to achieve is adding several dependencies as one bunch, I’d recommend you do not use any plugin but simply use a bundle in a version catalog which is exactly designed for that use-case.

Thank you for your great answer!

Actually i’m add a deps folder as a gradle plugin in composite build to add a dependency dsl.

the folder contains a deps.kt file, its content like this

import org.gradle.api.artifacts.dsl.DependencyHandler
class ComposeDeps {
  fun DependencyHandler.mt2() {

    implementation("androidx.compose.material:material")
  }

  fun DependencyHandler.mt3() {
    implementation("androidx.compose.material3:material3")
  }
}

class JetpackDeps {
  fun DependencyHandler.aaasimpleDeps() {
    implementation("androidx.core:core-ktx:${Versions.coreKtx}")
   
    implementation("androidx.activity:activity-ktx:1.8.2")
 
    implementation("org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}")

    implementation("androidx.annotation:annotation:latest.release")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.11.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
  }
}

then i could use like this in my project’s build.gradle.kts file

dependencies {

  jetpackDeps {
    aaacoreDeps()
    aaacompatLifecycleDeps()
    aaaComposeDeps { mt3() }
  }
  implementation(libs.okhttp3)
  implementation(libs.netAwesome)
   
}

i’m not using version catelog bundle because all dependenies in one bundle could use one configuration like implementation. while a function define a dependency block could add ksp,testImplementation dependencies.(i’m lazy, i don’t want to add one more line for testImplementation or others)

Then the quesion is:
the val implementation by configurations.existing exists in plugin-name.gradle.kts as it’s in gradle context
but in my deps folder’s deps.kt file, the configurations does not exist. so i have to use import gradle.kotlin.dsl.accessors._2a9c0e03359a4d9854f3ba65e889cbe0.implementation to access implementation.

i’m lazy, i don’t want to add one more line for testImplementation or others

Sure, do whatever pleases you, you have to live with the consequences. :slight_smile:
For example if you change a single version, the classpath of all tasks in your build will change and thus all tasks will be out of date.
If you would use a version catalog in a TOML file this would not be the case, as they are specifically designed to avoid such things.

You can still keep the versions or even coordinates in the version catalog but have such helper functions that use the entries in the version catalog to have the best of both approaches.

so i have to use import gradle.kotlin.dsl.accessors._2a9c0e03359a4d9854f3ba65e889cbe0.implementation to access implementation .

No, you cannot use such imports, they will constantly bite you in the ass.
Never ever ever ever use them.
add("implementation", ...) is what you are after in the .kt file.

Thank you, i"ll follow your advice :smiley:

1 Like