3

How I can write this code in kotlin dsl?

dependencies{
  api'org.slf4j:slf4j-api:1.7.25'
}

I can't find for what I need to change groovy "api" (in dependencies block) in kotlin dsl. For example I want to use org.slf4j, I want to declare it like API, but I checked migration docs and found analogies only for implementation, compile, etc. I use intellij idea.

I tried this:

plugins {
    id("java")
}

group = "com.myapp"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    api("org.slf4j:slf4j-api:1.7.36")
}

But is says: "Unresolved reference: api"

I checked this: https://docs.gradle.org/current/userguide/migrating_from_groovy_to_kotlin_dsl.html https://docs.gradle.org/current/userguide/kotlin_dsl.html

1

2 Answers 2

5

The syntax should be api("org.slf4j:slf4j-api:1.7.25").

You need to use the java-library plugin instead of just java to have access to the “api” configuration, regardless of whether your script is in Groovy or Kotlin.

Sign up to request clarification or add additional context in comments.

4 Comments

I wanna learn Kotlin, that's why I want to use it in gradle scripts too. My provided code is for groovy dsl. But when I try "api("org.slf4j:slf4j-api:1.7.25")" in kotlin dsl, it does not compile. It says "unresolved reference: api". Meanwhile other kotlin dsl functions work fine.
I updated post with my .kts content
Replace id("java") with id("java-library") to make api available. This would be required even in a Groovy file. You could also use a bare java-library call surrounded by backticks (it's a synthetic Kotlin property). Or you can replace that whole line with kotlin("jvm") version "1.7.20" if you want to use Kotlin in this project anyway.
java-library helped! I didn't know about it. I want java, so I keep java-library it works fine! Thank you.
2

In build.gradle.kts app module file

in the end of the file add

dependencies {
    implementation(kotlin("stdlib-jdk8", "1.4.30"))
    api("org.slf4j:slf4j-api:1.7.25")
}

of course no need to add implementation line, it's just for sample usage of implementation with kotlin library like stdlib

also in app settings.gradle.kts you need to add repositories

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven("https://dl.bintray.com/kotlin/kotlin-eap")
        maven("https://plugins.gradle.org/m2/")
    }
}

or you can add it in your project build.gradle.kts

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven("https://dl.bintray.com/kotlin/kotlin-eap")
        maven("https://plugins.gradle.org/m2/")
        maven("https://jitpack.io")
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30")
        classpath("com.android.tools.build:gradle:7.0.2")
        classpath("de.mannodermaus.gradle.plugins:android-junit5:1.7.0.0")
        classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.5.30")
    }
}

allprojects {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven("https://dl.bintray.com/kotlin/kotlin-eap")
        maven("https://plugins.gradle.org/m2/")
        maven("https://jitpack.io")
    }
}

also use java-library instead of java

apply plugin: 'java-library'

4 Comments

When I try "api("org.slf4j:slf4j-api:1.7.25")" in kotlin dsl, it does not compile. It says "unresolved reference: api". Meanwhile other kotlin dsl functions work fine.
@livkonrol these functions are actually the names of some configurations (in Gradle jargon). The available configurations depend on which plugins you add, so make sure you applied the same plugins as in Groovy. If you still have issues, please post the complete build script that reproduces the problem
@livkonrol it seems you are missing repository, I think your library is in mavenCentral, I updated the answer and added repositories
Thank you for suggestion. I updated my post with my .kts content. About ur suggestion with repositories, but other dependencies work fine (i mean if I use implementation/compileOnly/etc, they work fine. only api configuration does not work).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.