1

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

livkonrol
  • 13
  • 4
  • Where are you seeing the message "unresolved reference"? Is it IntelliJ, or is there an error when you run `./gradle assemble` from the command line? Have you loaded the Gradle project in your IDE? https://docs.gradle.org/current/userguide/kotlin_dsl.html#sec:ide_support https://www.jetbrains.com/help/idea/work-with-gradle-projects.html#gradle_refresh_project – aSemy Nov 01 '22 at 18:01

2 Answers2

2

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.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • 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. – livkonrol Nov 01 '22 at 13:34
  • I updated post with my .kts content – livkonrol Nov 01 '22 at 17:38
  • 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. – Tenfour04 Nov 01 '22 at 17:48
  • java-library helped! I didn't know about it. I want java, so I keep java-library it works fine! Thank you. – livkonrol Nov 01 '22 at 17:57
1

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'
Islam Assem
  • 1,376
  • 13
  • 21
  • 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 Nov 01 '22 at 13:34
  • @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 – Joffrey Nov 01 '22 at 13:46
  • @livkonrol it seems you are missing repository, I think your library is in mavenCentral, I updated the answer and added repositories – Islam Assem Nov 01 '22 at 15:03
  • 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). – livkonrol Nov 01 '22 at 17:40