gradle where is "API" for kotlin dsl?

2.4k Views Asked by At

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

2

There are 2 best solutions below

4
Tenfour04 On BEST ANSWER

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.

4
Islam Assem On

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'