Let's try creating a simple program using basic development tools.
- Prerequisite: Kotlin development environment setup
Creating the First Program in Kotlin
$ mkdir first-program && cd $_
$ gradle init
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 4
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Kotlin) [1..2] 2
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
Project name (default: first-program):
Source package (default: first.program): codeandtips.examples
> Task :init
Get more help with your project: https://docs.gradle.org/7.4.1/samples/sample_building_kotlin_applications.html
BUILD SUCCESSFUL in 28s
2 actionable tasks: 2 executed
$ ls
app gradle gradlew gradlew.bat settings.gradle.kts
The boilerplate Kotlin program, app/src/main/kotlin/codeandtips/examples/App.kt
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package codeandtips.examples
class App {
val greeting: String
get() {
return "Hello World!"
}
}
fun main() {
println(App().greeting)
}
$ ./gradlew run
> Task :app:run
Hello World!
BUILD SUCCESSFUL in 676ms
2 actionable tasks: 1 executed, 1 up-to-date
Or, using the -q
flag,
$ ./gradlew -q run
Hello World!
C C++ C# Clojure Crystal D Dart Erlang F# Go Haskell Java Javascript Julia Lua Python Rust Scala Swift Typescript