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
182
votes
7 answers

Kotlin: Interface ... does not have constructors

I am converting some of my Java code to Kotlin and I do not quite understand how to instantiate interfaces that are defined in Kotlin code. As an example, I have an interface (defined in Java code): public interface MyInterface { void…
Aleph Aleph
  • 5,215
  • 2
  • 13
  • 28
182
votes
15 answers

How to read a text file from resources in Kotlin?

I want to write a Spek test in Kotlin. How to read an HTML file from the src/test/resources folder? class MySpec : Spek( { describe("blah blah") { given("blah blah") { var fileContent: String = "" …
Olaf
  • 3,786
  • 4
  • 25
  • 38
180
votes
5 answers

How to iterate over hashmap in Kotlin?

How to iterate over HashMap in Kotlin? typealias HashMap = HashMap (source)
Nomi
  • 1,981
  • 2
  • 8
  • 10
179
votes
6 answers

Kotlin: How to work with List casts: Unchecked Cast: kotlin.collections.List to kotlin.colletions.List

I want to write a function that returns every item in a List that is not the first or the last item (a via point). The function gets a generic List<*> as input. A result should only be returned if the elements of the list are of the type…
Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53
177
votes
53 answers

How to fix ERROR: No signature of method: build_ap86oam3dut3pxce3x49rdtma.android()?

ERROR: No signature of method: build_ap86oam3dut3pxce3x49rdtma.android() is applicable for argument types: (build_ap86oam3dut3pxce3x49rdtma$_run_closure1) values: [build_ap86oam3dut3pxce3x49rdtma$_run_closure1@47588b04] The build gradle is: apply…
Usman Liaqat
  • 1,771
  • 2
  • 7
  • 6
177
votes
32 answers

Android - How to achieve setOnClickListener in Kotlin?

I wanted to know that how we set basic onClickListener in Kotlin for Android Development.
Anirudh Agarwal
  • 2,044
  • 2
  • 14
  • 8
175
votes
13 answers

Type 'State?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

I'm trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error Type 'State' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate Code @Composable fun…
SNM
  • 5,625
  • 9
  • 28
  • 77
174
votes
9 answers

MutableLiveData: Cannot invoke setValue on a background thread from Coroutine

I'm trying to trigger an update on LiveData from a coroutine: object AddressList: MutableLiveData>() fun getAddressesLiveData(): LiveData> { AddressList.value = listOf() GlobalScope.launch { …
kike
  • 4,255
  • 5
  • 23
  • 41
174
votes
2 answers

Kotlin - Idiomatic way to remove duplicate strings from array?

How to remove duplicates from an Array in kotlin?
jturolla
  • 6,596
  • 7
  • 26
  • 41
174
votes
22 answers

Kotlin-android: unresolved reference databinding

I have following fragment class written in Java using the new databinding library import com.example.app.databinding.FragmentDataBdinding; public class DataFragment extends Fragment { @Nullable private FragmentDataBinding mBinding; …
Mikhail
  • 3,666
  • 4
  • 30
  • 43
173
votes
13 answers

Use of Boolean? in if expression

If I have a nullable Boolean b, I can do the following comparison in Java: Boolean b = ...; if (b != null && b) { /* Do something */ } else { /* Do something else */ } In Kotlin, I can achieve the same by using the !! operator: val b:…
nhaarman
  • 98,571
  • 55
  • 246
  • 278
171
votes
15 answers

Outdated Kotlin Runtime warning in Android Studio

After downloaded and installed latest Kotlin plugin I have Outdated Kotlin Runtime warning from Android Studio that telling me: Your version of Kotlin runtime in 'kotlin-stdlib-1.1.2' library is 1.1.2, while plugin version is…
Arsenius
  • 4,972
  • 4
  • 26
  • 39
171
votes
4 answers

What is the equivalent of Java static final fields in Kotlin?

In Java, to declare a constant, you do something like: class Hello { public static final int MAX_LEN = 20; } What is the equivalent in Kotlin?
pdeva
  • 43,605
  • 46
  • 133
  • 171
171
votes
16 answers

Is there a convenient way to create Parcelable data classes in Android with Kotlin?

I'm currently using the excellent AutoParcel in my Java project, which facilitates the creation of Parcelable classes. Now, Kotlin, which I consider for my next project, has this concept of data classes, that automatically generate the equals,…
thalesmello
  • 3,301
  • 3
  • 20
  • 20
167
votes
13 answers

Kotlin addTextChangeListener lambda?

How do you build a lambda expression for the EditText addTextChangeListener in Kotlin? Below gives an error: passwordEditText.addTextChangedListener { charSequence -> try { password = charSequence.toString() } catch (error:…
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72