Questions tagged [kotlin]

Kotlin is a cross-platform, statically typed, general-purpose high-level programming language with type inference. This tag is often used alongside additional tags for the different targets (JVM, JavaScript, native, etc.) and libraries/frameworks (Android, Spring, etc.) used by Kotlin developers, if the question relates specifically to those topics.

Kotlin is an open-source, statically typed programming language supported and developed by JetBrains. It supports JVM bytecode, JavaScript, and Native code as compilation targets, and it has been an officially supported first-class language on Android since Google I/O 2017. The goals with Kotlin are to make it concise, safe, versatile, and have it be seamlessly interoperable with existing Java libraries.

On June 9th 2022, The Kotlin team released version 1.7.0 (announcement / github release tag).

The current major version is 1.7.

How to ask

If you are using Kotlin and your question is related to it, then you should add this tag. You should explain what do you intend to achieve, how did you try to achieve it, what the experienced behavior is and how is that different from your expectations.

Kotlin Reference Documentation

Kotlin Books

Development tools

Useful links

91039 questions
388
votes
6 answers

How do I initialize Kotlin's MutableList to empty MutableList?

Seems so simple, but, how do I initialize Kotlin's MutableList to empty MutableList? I could hack it this way, but I'm sure there is something easier available: var pusta: List = emptyList() var cos: MutableList =…
ssuukk
  • 8,200
  • 7
  • 35
  • 47
386
votes
32 answers

What is the difference between var and val in Kotlin?

What is the difference between var and val in Kotlin? I have gone through this link: KotlinLang: Properties and Fields As stated on this link: The full syntax of a read-only property declaration differs from a mutable one in two ways: it starts…
Akshar Patel
  • 8,998
  • 6
  • 35
  • 50
370
votes
9 answers

How to get the current index in for each Kotlin

How to get the index in a for each loop? I want to print numbers for every second iteration For example for (value in collection) { if (iteration_no % 2) { //do something } } In java, we have the traditional for loop for (int i = 0;…
Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45
368
votes
60 answers

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution

All of sudden I start getting this error, and I am not getting idea why if anyone just let me know where this error is, will be enough helpful. As much I am able to get is this because of new update of android studio. Detailed summary of error I am…
Shubham Tater
  • 3,718
  • 2
  • 13
  • 10
364
votes
15 answers

Constants in Kotlin -- what's a recommended way to create them?

How is it recommended to create constants in Kotlin? And what's the naming convention? I've not found that in the documentation. companion object { //1 val MY_CONST = "something" //2 const val MY_CONST = "something" //3 val…
Jodimoro
  • 4,355
  • 3
  • 11
  • 18
363
votes
2 answers

Sort collection by multiple fields in Kotlin

Let's say I have a list of People which I need to sort by Age first and then by Name. Coming from a C#-background, I can easily achieve this in said language by using LINQ: var list=new List(); list.Add(new Person(25, "Tom")); list.Add(new…
Fire095
  • 3,715
  • 2
  • 11
  • 10
355
votes
20 answers

What do I use now that Handler() is deprecated?

How do I fix the deprecation warning in this code? Alternatively, are there any other options for doing this? Handler().postDelayed({ context?.let { //code } }, 3000)
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
349
votes
14 answers

Extend data class in Kotlin

Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data…
Dmitry
  • 4,232
  • 2
  • 15
  • 13
343
votes
3 answers

Difference between List and Array types in Kotlin

What is the difference between List and Array types? It seems can make same operations with them (loops, filter expression, etc..), is there any difference in behavior or usage? val names1 = listOf("Joe","Ben","Thomas") val names2 =…
Daniel Hári
  • 7,254
  • 5
  • 39
  • 54
339
votes
10 answers

Default interface methods are only supported starting with Android 7.0 (Nougat)

I upgraded to Android Studio 3.1 and I'm getting the following error: Default interface methods are only supported starting with Android N (--min-api 24): void…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
328
votes
11 answers

Kotlin: how to pass a function as parameter to another?

Given function foo : fun foo(m: String, bar: (m: String) -> Unit) { bar(m) } We can do: foo("a message", { println("this is a message: $it") } ) //or foo("a message") { println("this is a message: $it") } Now, lets say we have the following…
mhshams
  • 16,384
  • 17
  • 55
  • 65
327
votes
53 answers

Duplicate class in Kotlin Android

I kept on getting error that there is duplicate error in classes. This is what i have under org.jetbrains.kotlin folder .idea/libraries it seems like the problem is because there is two different dependencies for the kotlin, from stdlib and…
midnighthowlers
  • 3,271
  • 2
  • 3
  • 4
327
votes
9 answers

How to make primary key as autoincrement for Room Persistence lib

I am creating an Entity (Room Persistence Library) class Food, where I want to make foodId as autoincrement. @Entity class Food(var foodName: String, var foodDesc: String, var protein: Double, var carbs: Double, var fat: Double) { @PrimaryKey …
Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
323
votes
7 answers

What is the Kotlin double-bang (!!) operator?

I'm converting Java to Kotlin with Android Studio. I get double bang after the instance variable. What is the double bang and more importantly where is this documented? mMap!!.addMarker(MarkerOptions().position(london).title("Marker in London"))
mbr_at_ml
  • 3,585
  • 2
  • 13
  • 12
321
votes
32 answers

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

I am trying to consume an API using Retrofit and Jackson to deserialize. I am getting the onFailure error No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator.
José Nobre
  • 4,407
  • 6
  • 20
  • 40