I'm trying to test 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 **> Task :compileKotlin** FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileKotlin'. > 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
UPDATE
In reference to the comments received, I did the below:
src/main/libs
: Added the below code to the build.gradle.kts
:
configurations { create("externalLibs") }
dependencies { "externalLibs"(files("src/main/libs/blogiclib.jar")) }
instead of:
task<JavaCompile>("compile") {
source = fileTree(file("src/main/resources/blogiclib.jar"))
}
but still getting the same error :(
You can connect one multiplatform project to another as a dependency. To do this, simply add a project dependency to the source set that needs it. If you want to use a dependency in all source sets, add it to the common one. In this case, other source sets will get their versions automatically.
gradle file which helps Gradle build tool to specify this build is a debug build with certain keyAlias etc. So these building blocks, API, etc are formats to use Gradle in android, hence called Gradle DSL (domain-specific language).
Kotlin DSL brings the simplicity of the Kotlin language syntax and rich API set right into the script files on top of that code completion makes it perfect to work with Gradle script files. We will use this to manage our dependencies and project settings configurations more elegantly.
You can include all jar files in some folders as follows:
dependencies {
implementation(fileTree("libs/compile"))
compileOnly(fileTree("libs/provided"))
}
Alternatively, select specific files:
repositories {
flatDir {
dirs("libs/compile")
dirs("libs/provided")
}
}
dependencies {
implementation(":gson-2.8.5")
compileOnly(":javaee-api-8.0")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With