Questions tagged [kotlin]

Kotlin is an open-source, statically typed programming language supported and developed by JetBrains. Kotlin combines OOP and functional features and is focused on interoperability, safety, clarity, and tooling support. It currently targets the JVM, JavaScript and native targets, and it's an officially supported language on Android.

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 May 5, 2021, The Kotlin team released version 1.5 (announcement / github release tag).

The current major version is 1.5.

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

57697 questions
212
votes
3 answers

How does the reified keyword in Kotlin work?

I'm trying to understand the purpose of the reified keyword, apparently it's allowing us to do reflection on generics. However, when I leave it out it works just as fine. Anyone care to explain when this makes an actual difference?
hl3mukkel
  • 3,369
  • 2
  • 16
  • 21
204
votes
8 answers

What is the difference between launch/join and async/await in Kotlin coroutines

In the kotlinx.coroutines library you can start new coroutine using either launch (with join) or async (with await). What is the difference between them?
Roman Elizarov
  • 20,708
  • 8
  • 52
  • 54
203
votes
22 answers

How can I get a random number in Kotlin?

A generic method that can return a random integer between 2 parameters like ruby does with rand(0..n). Any suggestion?
Yago Azedias
  • 3,170
  • 3
  • 12
  • 28
202
votes
8 answers

What's the Kotlin equivalent of Java's String[]?

I see that Kotlin has ByteArray, ShortArray, IntArray, CharArray, DoubleArray, FloatArray, which are equivalent to byte[], short[], int[],char[], double[], float[] in Java. Now I'm wondering, is there any StringArray equivalent to Java's String[]?
chancyWu
  • 12,898
  • 11
  • 56
  • 71
201
votes
7 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;…
Audi
  • 8,311
  • 7
  • 31
  • 43
195
votes
6 answers

How to convert List to Map in Kotlin?

For example I have a list of strings like: val list = listOf("a", "b", "c", "d") and I want to convert it to a map, where the strings are the keys. I know I should use the .toMap() function, but I don't know how, and I haven't seen any examples of…
LordScone
  • 2,590
  • 2
  • 14
  • 23
194
votes
13 answers

Setting text in EditText Kotlin

I am trying to set text in a EditText but it says: Type mismatch. Required: Editable Found: String My code is as follow: String name = "Paramjeet" val nametxt = findViewById (R.id.nametxt) as EditText nametxt.text = name Don't say to use setText…
Paramjeet Singh
  • 2,170
  • 2
  • 6
  • 14
194
votes
11 answers

How to call a function after delay in Kotlin?

As the title, is there any way to call a function after delay (1 second for example) in Kotlin?
Nguyen Minh Binh
  • 21,451
  • 30
  • 108
  • 156
192
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
  • 74,658
  • 30
  • 167
  • 210
192
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
  • 13,830
  • 15
  • 49
  • 63
189
votes
42 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
  • 1,928
  • 2
  • 9
  • 9
189
votes
10 answers

Accessing Kotlin extension functions from Java

Is it possible to access extension functions from Java code? I defined the extension function in a Kotlin file. package com.test.extensions import com.test.model.MyModel /** * */ public fun MyModel.bar(): Int { return…
Lovis
  • 7,521
  • 4
  • 27
  • 45
187
votes
38 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…
185
votes
3 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
  • 74,658
  • 30
  • 167
  • 210
178
votes
31 answers

Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details

Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details build:gradle(Module:app) buildscript { repositories { maven { url 'https://jitpack.io' url…
Ankur_009
  • 2,841
  • 3
  • 23
  • 44