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
201
votes
11 answers

What does ?: do in Kotlin? (Elvis Operator)

I can't figure out what ?: does in for example this case val list = mutableList ?: mutableListOf() and why can it be modified to this val list = if (mutableList != null) mutableList else mutableListOf()
Jorge
  • 2,095
  • 2
  • 8
  • 9
201
votes
7 answers

Difference between fold and reduce in Kotlin, When to use which?

I am pretty confused with both functions fold() and reduce() in Kotlin, can anyone give me a concrete example that distinguishes both of them?
TapanHP
  • 5,969
  • 6
  • 37
  • 66
199
votes
4 answers

Difference between "*" and "Any" in Kotlin generics

I am not sure I fully understand the difference between SomeGeneric<*> and SomeGeneric. I think * represents anything (wild card) and Any represents the object which ALL objects inherit from. So it seems they should be the same, but are they?
Wheel Builder
  • 3,515
  • 5
  • 20
  • 32
197
votes
17 answers

Multiple variable let in Kotlin

Is there any way to chain multiple lets for multiple nullable variables in kotlin? fun example(first: String?, second: String?) { first?.let { second?.let { // Do something just if both are != null } } } I mean,…
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
194
votes
5 answers

Try-with-resources in Kotlin

When I tried to write an equivalent of a Java try-with-resources statement in Kotlin, it didn't work for me. I tried different variations of the following: try (writer = OutputStreamWriter(r.getOutputStream())) { // ... } But neither works.…
Alex
  • 2,916
  • 3
  • 22
  • 27
193
votes
10 answers

Static extension methods in Kotlin

How do you define a static extension method in Kotlin? Is this even possible? I currently have an extension method as shown below. public fun Uber.doMagic(context: Context) { // ... } The above extension can be invoked on an…
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
191
votes
9 answers

What is out keyword in kotlin

I am not able to understand and I couldn't find the meaning of out keyword in kotlin. You can check example here: List If any one can explain the meaning of this. It would be really appreciated.
Akshay Sood
  • 6,366
  • 10
  • 36
  • 59
190
votes
19 answers

How to set compileJava' task ( 11) and 'compileKotlin' task (1.8) jvm target compatibility to the same Java version in build.gradle.kts?

Build.gradle.kts buildscript { repositories { google() mavenCentral() gradlePluginPortal() } dependencies { classpath ("com.android.tools.build:gradle:7.0.2") classpath…
190
votes
5 answers

Why do we use "companion object" as a kind of replacement for Java static fields in Kotlin?

What is the intended meaning of "companion object"? So far I have been using it just to replace Java's static when I need it. I am confused with: Why is it called "companion"? Does it mean that to create multiple static properties, I have to group…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
189
votes
42 answers

Error: Execution failed for task ':app:clean'. Unable to delete file

I'm trying to rebuild my Android Studio Gradle project (containing mostly Kotlin code), but it started to throw an UnableToDeleteFileException during the cleaning/rebuilding process: Execution failed for task ':app:clean'. > Unable to delete file:…
187
votes
14 answers

Kotlin with JPA: default constructor hell

As JPA requires, @Entity classes should have a default (non-arg) constructor to instantiate the objects when retrieving them from the database. In Kotlin, properties are very convenient to declare within the primary constructor, as in the following…
hotkey
  • 140,743
  • 39
  • 371
  • 326
185
votes
15 answers

warning: Kotlin runtime JAR files in the classpath should have the same version

I get the following warning, but I'm not sure where v1.0.6 resides. Is it possible this error comes from a Kotlin library somehow including an old Kotlin version? Any ideas how to fix it or at least how can I follow the suggestion to make…
ycomp
  • 8,316
  • 19
  • 57
  • 95
183
votes
21 answers

MediaSessionCompat:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

I'm trying to update my application to Android SDK 31 but I'm having an issue with MediaSessionCompat. I have a MediaService that extends the MediaBrowserServiceCompat() and in method onCreate of that service I initialise the…
Adelino
  • 2,530
  • 4
  • 20
  • 32
183
votes
8 answers

kotlin version that is used for building with gradle (1.1.2-5) differs from the one bundled into the IDE plugin (1.1.2-4)

Has anyone solved this issue? kotlin version that is used for building with gradle (1.1.2-5) differs from the one bundled into the IDE plugin (1.1.2-4) I am using AS 3.0 Canary 4
raditya gumay
  • 2,951
  • 3
  • 17
  • 24
183
votes
8 answers

How to access "Activity.this" in Kotlin?

I have this piece of Java code: MaterialDialog builder = new MaterialDialog.Builder(MainActivity.this) I want to get the MainActivity object in Kotlin. The automatic conversion breaks at MainActivity.this.
Rado
  • 2,034
  • 2
  • 10
  • 18