Adding dependencies to kotlin app using DSL


#1

I’m trying to replicate this by building gradle 5 using kotlin DSL.

I created a lib, and built it as below:

Hasans-Air:blogiclib h_ajsf$ gradle init --type=kotlin-library

Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details

Select build script DSL:

1: groovy

2: kotlin

Enter selection (default: kotlin) [1..2] 2

Project name (default: blogiclib): 

Source package (default: blogiclib): 

**BUILD SUCCESSFUL** in 16s

2 actionable tasks: 2 executed

Hasans-Air:blogiclib h_ajsf$ ls

build.gradle.kts gradlew settings.gradle.kts

gradle gradlew.bat src

Hasans-Air:blogiclib h_ajsf$ code .

Hasans-Air:blogiclib h_ajsf$ gradle build

Then I got the output file generated as: build\libs\blogiclib.jar

The Library.kt file generated is:

package blogiclib

class Library {
    fun someLibraryMethod(): Boolean {
        return true
    }
}

And the build.gradle.kts generated is:

plugins {
    id("org.jetbrains.kotlin.jvm").version("1.3.10")
}

repositories {
    jcenter()
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

Then I generated a kotlin app and tested it as below:

Hasans-Air:gradle h_ajsf$ gradle init --type=kotlin-application

Starting a Gradle Daemon (subsequent builds will be faster)

Select build script DSL:

1: groovy

2: kotlin

Enter selection (default: kotlin) [1..2] 2

Project name (default: gradle): blogic

Source package (default: blogic): 

**BUILD SUCCESSFUL** in 25s

2 actionable tasks: 2 executed

Hasans-Air:gradle h_ajsf$ ls

build.gradle.kts gradlew settings.gradle.kts

gradle gradlew.bat src

Hasans-Air:gradle h_ajsf$ code .

Hasans-Air:gradle h_ajsf$ gradle run
**BUILD SUCCESSFUL** in 6m 4s

3 actionable tasks: 3 executed

Hasans-Air:gradle h_ajsf$ gradle run

**> Task :run**

Hello world.

Then I added the previously generated lib blogiclib.jar to the folder: main\resources

And made my App,kt file as:

package blogic

import blogiclib.LibraryKt

class App {
    val greeting: String
        get() {
            return "Hello world."
        }
}

fun main(args: Array<String>) {
    println("${App().greeting} = someLibraryMethod()")
}

And its build.gradle.kts based on my understanding from here as:

plugins {
    id("org.jetbrains.kotlin.jvm").version("1.3.10")
    application
}

repositories {
    jcenter()
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")

}

application {
    mainClassName = "blogic.AppKt"
}

task<JavaCompile>("compile") {
    source = fileTree(file("src/main/resources/blogiclib.jar"))
}

But at compiling I got the below error:

Hasans-Air:gradle h_ajsf$ gradle run
 
e: /Users/h_ajsf/Documents/gradle/src/main/kotlin/blogic/App.kt: (6, 8): Unresolved reference: blogiclib
 
**&gt; Task :compileKotlin** FAILED
 
FAILURE: Build failed with an exception.
 
* What went wrong:
 
Execution failed for task ':compileKotlin'.
 
&gt; Compilation error. See log for more details
 
* Try:
 
Run with **--stacktrace** option to get the stack trace. Run with **--info** or **--debug** option to get more log output. Run with **--scan** to get full insights.
 
* Get more help at **https://help.gradle.org**
 
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
 
Use '--warning-mode all' to show the individual deprecation warnings.
 
See https://docs.gradle.org/5.0/userguide/command_line_interface.html#sec:command_line_warnings
 
**BUILD FAILED** in 1s
 
2 actionable tasks: 2 executed

#2

I guess the easiest way to solve this would be to use a gradle multi-project build:
https://docs.gradle.org/current/userguide/multi_project_builds.html

But one thing I noticed is that in the original post you had the jar file under “dependencies”, here it’s in the compile task as source? That seems odd to me, but I have not yet made the upgrade to gradle 5, so I could be wrong.