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
22
votes
1 answer

Algebraic data types in Kotlin

I am trying to figure out how to use algebraic data types in Kotlin, so I'm trying to implement a basic BinaryTree type the following way. sealed class Tree{ class Node(val left: Tree, val right: Tree): Tree() class Leaf(val…
pjozsef
  • 815
  • 8
  • 14
22
votes
2 answers

What is the purpose of `external` keyword in Kotlin?

What exactly is the purpose of the external keyword in Kotlin? I guess it's for JNI like native in Java, but I can't seem to find any actual reference or documentation on this.
maciekjanusz
  • 4,702
  • 25
  • 36
22
votes
1 answer

What's the difference between () -> Unit and (Unit) -> Unit types?

I have following functions: fun process(t: T, call: (U) -> Unit, map: (T) -> U) = call(map(t)) fun processEmpty(t: T, call: () -> Unit) = process(t, call, {}) // error but the processEmpty is not compiling. The error message is Type…
netimen
  • 4,199
  • 6
  • 41
  • 65
22
votes
1 answer

Kotlin - Intermittent "bad class file" error

Starting today, when I attempt to build my Kotlin Android app, I am met with the following error in my Gradle build: Error:cannot access Baz bad class file:…
John D.
  • 2,521
  • 3
  • 24
  • 45
22
votes
1 answer

Resolving Accidental Override errors in Kotlin

I've recently started expirementing with Kotlin and started a Spring Boot pet project using Kotlin. I'm trying to integrate a custom User domain object to Spring Security and thus want to implement the UserDetails inteface. Given my domain User…
feugatos
  • 663
  • 6
  • 15
22
votes
3 answers

Kotlin: Method reference not working?

It seems I'm unable to use a method reference of an object in Kotlin. This feature exists in Java. For example in Java if I was looping through a string to append each character to a writer: string.forEach(writer::append); But in Kotlin using the…
Jire
  • 9,680
  • 14
  • 52
  • 87
22
votes
3 answers

Kotlin: Initialize class attribute in constructor

I create a Kotlin-class with a class attribute, which I want to initialize in the constructor: public class TestClass { private var context : Context? = null // Nullable attribute public constructor(context : Context) { this.context…
Christopher
  • 9,682
  • 7
  • 47
  • 76
22
votes
1 answer

Lazy list in kotlin?

How can I achieve in a simple way a Lazy List in Kotlin? (For example, integers lazy list). I've been seeking official documentation, I've been googling for that without consistent results. Maybe the best tutorial I've found is here, but I wonder if…
loloof64
  • 5,252
  • 12
  • 41
  • 78
22
votes
4 answers

Accessing Kotlin class object from Java

I have a Kotlin class which has a class object, e.g. public class Foo { public class object { public val SomeValue : Int = 0 } } If I'm using this class from Java, how do I access SomeValue inside the class object? If it were a…
Wilka
  • 28,701
  • 14
  • 75
  • 97
21
votes
1 answer

Plugin incompatible with the new build found: Kotlin

I was going to upgrade from Android Studio Bumblebee to Android Studio Dolphin. However, I seem to get an unexpected warning from Android Studio: As you can see the text in red, it shows that newer Android Studio's build does not support Kotlin!…
MYJ World
  • 820
  • 1
  • 5
  • 16
21
votes
1 answer

How to check if a method was not invoked with mockk?

I need to check if a method was not invoked in my unit tests. This is an example test I did that checks if the method was invoked and it works perfectly fine: @Test fun viewModel_selectDifferentFilter_dispatchRefreshAction() { val selectedFilter…
Pierre Vieira
  • 2,252
  • 4
  • 21
  • 41
21
votes
5 answers

Missing Feature{name=auth_api_credentials_begin_sign_in, version=6}

I am trying to use the Google authentication method (One tap sign in) for my application. However, after I clicked on the sign button, I faced the following problems: W/GoogleApiManager: com.google.android.gms.internal.auth-api.zbaz could not…
21
votes
2 answers

How to verify android app links on android 12 and higher?

Problem is opening application over link in android 12 or higher. Everything works fine on lower versions of android. When I look at my “App Info” -> “Open by default” screen. I see unapproved links. When I turn on that link as approved inside…
iWizard
  • 6,816
  • 19
  • 67
  • 103
21
votes
3 answers

Any difference between String.getOrElse() and String.elementAtOrElse()?

As the title states: Is there any difference between String.getOrElse() and String.elementAtOrElse()? From a functional point of view they seem completely identical, maybe some performance difference? Same question accounts to String.getOrNull() and…
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
21
votes
2 answers

LazyColumn is slower than Column with vertical scroll

I have product cell which I want to display on the list, I've used LazyColumn but performance was terrible, I couldn't find why it is so slow. Then I've switched LazyColumn to Column and all of the sudden scrolling is super smooth LazyColumn…
1 2 3
99
100