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
226
votes
15 answers

How to clone or copy a list in kotlin

How to copy list in Kotlin? I'm using val selectedSeries = mutableListOf() selectedSeries.addAll(series) Is there a easier way?
Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45
225
votes
24 answers

Android Room - simple select query - Cannot access database on the main thread

I am trying a sample with Room Persistence Library. I created an Entity: @Entity public class Agent { @PrimaryKey public String guid; public String name; public String email; public String password; public String phone; …
Devarshi
  • 16,440
  • 13
  • 72
  • 125
225
votes
17 answers

How to parse JSON in Kotlin?

I'm receiving a quite deep JSON object string from a service which I must parse to a JSON object and then map it to classes. How can I transform a JSON string to object in Kotlin? After that the mapping to the respective classes, I was using…
AJ_1310
  • 3,303
  • 3
  • 19
  • 26
224
votes
19 answers

The AsyncTask API is deprecated in Android 11. What are the alternatives?

Google is deprecating Android AsyncTask API in Android 11 and suggesting to use java.util.concurrent instead. you can check out the commit here * * @deprecated Use the standard java.util.concurrent or *
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
224
votes
40 answers

IllegalArgumentException: navigation destination xxx is unknown to this NavController

I am having an issue with the new Android Navigation Architecture component when I try to navigate from one Fragment to another, I get this weird error: java.lang.IllegalArgumentException: navigation destination XXX is unknown to this…
223
votes
36 answers

"Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16"

The following error occurs when I tried to build the app: C:/Users/Lenovo/.gradle/caches/transforms-2/files-2.1/32f0bb3e96b47cf79ece6482359b6ad2/jetified-kotlin-stdlib-jdk7-1.5.0.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: Module was compiled…
Saheel Sapovadia
  • 2,755
  • 3
  • 11
  • 22
223
votes
7 answers

Single exclamation mark in Kotlin

What does a single exclamation mark mean in Kotlin? I've seen it a few times especially when using Java APIs. But I couldn't find it in the documentation nor on StackOverflow.
Mibac
  • 8,990
  • 5
  • 33
  • 57
222
votes
11 answers

How to convert String to Long in Kotlin?

So, due to lack of methods like Long.valueOf(String s) I am stuck. How to convert String to Long in Kotlin?
Jerome
  • 2,397
  • 2
  • 12
  • 8
221
votes
10 answers

How to implement switch-case statement in Kotlin

How to implement equivalent of following Java switch statement code in Kotlin? switch (5) { case 1: // Do code break; case 2: // Do code break; case 3: // Do code break; }
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
216
votes
10 answers

How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?

From Android 9 Pie now, requests without encryption will never work. And by default, the System will expect you to use TLS by default.You can read this feature here So if you only make requests via HTTPS you are safe. But what about apps that make…
216
votes
17 answers

How to implement Builder pattern in Kotlin?

Hi I am a newbie in the Kotlin world. I like what I see so far and started to think to convert some of our libraries we use in our application from Java to Kotlin. These libraries are full of Pojos with setters, getters and Builder classes. Now I…
Keyhan
  • 2,370
  • 2
  • 19
  • 22
215
votes
4 answers

What Java 8 Stream.collect equivalents are available in the standard Kotlin library?

In Java 8, there is Stream.collect which allows aggregations on collections. In Kotlin, this does not exist in the same way, other than maybe as a collection of extension functions in the stdlib. But it isn't clear what the equivalences are for…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
214
votes
18 answers

Idiomatic way of logging in Kotlin

Kotlin doesn't have the same notion of static fields as used in Java. In Java, the generally accepted way of doing logging is: public class Foo { private static final Logger LOG = LoggerFactory.getLogger(Foo.class); } Question is what is the…
mchlstckl
  • 3,390
  • 2
  • 21
  • 21
213
votes
6 answers

Example of when should we use run, let, apply, also and with on Kotlin

I wish to have a good example for each function run, let, apply, also, with I have read this article but still lack of an example
UmAnusorn
  • 10,420
  • 10
  • 72
  • 100
203
votes
4 answers

In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

If I have a nullable type Xyz?, I want to reference it or convert it to a non-nullable type Xyz. What is the idiomatic way of doing so in Kotlin? For example, this code is in error: val something: Xyz? = createPossiblyNullXyz() something.foo() //…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227