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
14
votes
3 answers

Live Data and 2-Way Data Binding: Custom setter not being called

I am using 2-way data binding to update a LiveData String object from my ViewModel with a string set in the EditText:
14
votes
1 answer

Kotlin 1.2.50 asks for baseFeatureInfoDir

When I switch in an Android project with classpath 'com.android.tools.build:gradle:3.2.0-alpha18' to classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.50" I run into What went wrong: Some problems were found with the configuration of…
hannes ach
  • 8,318
  • 3
  • 39
  • 50
14
votes
1 answer

How to convert concatenation to template in Kotlin

I am new to programming. Recently, I started learning Kotlin. I got a suggestion with this code: var cont = "N" var result:Int? result = 45 println (cont + " + " + result) It suggests converting this {" + "} to a template, but I do not know how?
RS.Chamroeun
  • 143
  • 1
  • 4
14
votes
2 answers

Best way to pass multiple lambda functions in Kotlin to handle success/failure

New at Kotlin here and trying to learn the best way to use the higher order functions and passing lambdas. I've created this method to call an API and return an object created from a string OR return a failure if something went wrong. fun…
7200rpm
  • 440
  • 3
  • 14
14
votes
1 answer

Kotlin Coroutine Unit Testing Error: Exception in thread "main @coroutine#1 @coroutine#2" java.lang.NullPointerException

I'm trying to unit test a presenter that I made with Kotlin coroutines, and this is also my first time using Mockito Whenever I try to run the unit test I'm getting the following error the first time it tries to do anything with my view while in the…
Ben987654
  • 2,534
  • 2
  • 21
  • 41
14
votes
4 answers

Kotlin type-safe typealiases

I use typealiases in my Kotlin code a lot, but I wonder if I can enforce type-safety on them. typealias Latitude = Double typealias Longitude = Double fun someFun(lat: Latitude, lon: Longitude) {...} val lat: Latitude = 12.34 val lon: Longitude =…
ekaerovets
  • 1,038
  • 10
  • 17
14
votes
4 answers

try-with-resources / use / multiple resources

I'm using a Java-API which heavily uses the Autoclosable-Interface and thus in Java try-with-resources. However in Java you can specify try (res1, res2, res3...) { ... } Do we have a way to use more than one resource? It looks like the well known…
Johannes
  • 1,881
  • 1
  • 21
  • 38
14
votes
6 answers

Kotlin: count occurrences of `charArray` in `String`

I have two strings val string1 = "Hello" val string2 = "Hello world" I have to count existence of each letter from string1 in string2 in Kotlin So far, I have written this much code and stuck with regex val string1_array = string1.toCharArray() val…
Anuj TBE
  • 6,408
  • 15
  • 85
  • 199
14
votes
1 answer

Union types in Java

I've been working with C# for a while and trying to get more familiar with Java. So I'm trying to migrate some of the basic patterns I use on daily basis in C# even only to understand the gap between JVM and dotnet and figure out how to deal with…
Michal Pawluk
  • 401
  • 1
  • 4
  • 10
14
votes
5 answers

Collapsing CardView Animation not working correctly

What I'm trying to do I have a RecyclerView with many items that are basically some CardView. Those cards have a supporting text in the middle of their bodies, which has the visibility set to GONE by default, and it's made VISIBLE when I click the…
14
votes
4 answers

Throwing Exceptions with Mockito in Kotlin

I am using Kotlin and am trying to throw an Exception on a specific method invocation but always get the following error Checked exception is invalid for this method! Invalid: exceptions.ServiceException This is the test val client : IClient =…
Jakob Abfalter
  • 4,270
  • 14
  • 48
  • 81
14
votes
1 answer

Is it possible to add an interface to an existing class in Kotlin?

Sometimes it is useful to create an interface that is already implemented by existing third-party code (ie. from a library). For example, we could imagine selecting a subset of the methods of the String class and declaring a GenericString. We might…
Casebash
  • 100,511
  • 79
  • 236
  • 337
14
votes
1 answer

destructor in Kotlin programming language

I am new to Kotlin, have written a class in kotlin to perform database operation I have defined database connection in constructor using init but I want to close database connection using destructor. Any Idea of how to achieve this using kotlin…
RKP
  • 520
  • 1
  • 5
  • 20
14
votes
4 answers

RxJava Return single, execute completable after

I'm trying to accomplish the following: Return some data as single, execute a completable after. The following code does not compile due to single.andThen(). The actions need to be executed in this order. val single = Single.create {…
Jeremy
  • 485
  • 2
  • 4
  • 10
14
votes
2 answers

Type of the parameter must be a class annotated with @Entity or a collection/array of it

Okey, so I tried to follow this guide: https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1 which led me to this code: https://gist.github.com/florina-muntenescu/1c78858f286d196d545c038a71a3e864 I tried to make my own example when I…
LindaK
  • 141
  • 1
  • 4