Home > Back-end >  gradle where is "API" for kotlin dsl?
gradle where is "API" for kotlin dsl?

Time:11-02

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

CodePudding user response:

I don't recommend switching from Groovy to Kotlin unless you're already very comfortable with Kotlin syntax. Groovy is far more commonly used for Gradle scripts, so almost all the examples you find everywhere are written in Groovy.

Your syntax above is not valid Kotlin, whether or not it's trying to be equivalent to an api call in Groovy. You can't use apostrophes around a String, infix functions always have a space before the argument, and you can't use an infix notation with implicit receiver. It should be api("org.slf4j:slf4j-api:1.7.25")

CodePudding user response:

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")
    }
}
  • Related