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

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors

I have enabled databinding, but while I execute the code I get this error. error e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. I created a fragment class and XML for that class. Im…
wasim
  • 577
  • 6
  • 12
14
votes
3 answers

Firebase ML kit give FirebaseMLException: Waiting for the text recognition model to be downloaded. Please wait

I'm use firebase ml kit for text recognition but give this exception on emulator and real device. W/System.err: com.google.firebase.ml.common.FirebaseMLException: Waiting for the text recognition model to be downloaded. Please wait. at…
Radesh
  • 10,690
  • 3
  • 42
  • 60
14
votes
3 answers

How do I sum all the items of a list of integers in Kotlin?

I have a list of integers, like: val myList = listOf(3,4,2) Is there any quick way in Kotlin to sum all the values of the list? or do I have to use a loop? Thanks.
14
votes
3 answers

java.lang.IllegalStateException: Cannot invoke observeForever on a background thread

Can someone help me find where I am going wrong here. I need to continously observer network data and update the UI whenever there is a data change from the Worker. Please note that this was working before upgrading to androidx. Here is a Worker…
14
votes
2 answers

How does Kotlin coroutines know when to yield when making network calls?

I`m new to Kotlin coroutines and one thing I failed to figure out is, how does the coroutines know when to yield to others when making network calls. If I understand it right, a coroutine works preemptively, which means it knows when to yield to…
Rafoul
  • 143
  • 1
  • 8
14
votes
2 answers

In Android Navigation Architecture, how can I check if current Fragment is the last one?

I need to display custom AlertDialog, but only when there are no more fragments after calling NavController.navigateUp(). My current code does something similar, but there is a bug to it: override fun onBackPressed() { if…
14
votes
3 answers

Testing private methods in Kotlin

How to test private methods in Kotlin? I tried to add @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) from androidx.annotation.VisibleForTesting but it doesn’t make my function private This is how I’m using it @VisibleForTesting(otherwise…
qbait
  • 2,934
  • 2
  • 22
  • 39
14
votes
2 answers

No type arguments expected for class Call

I'm having an issue.I have project and then i copy paste into an other new project and i'm facing this error No type arguments expected for class Call In the first project i didn't have any problem... Here is my interface class which i have the…
Alex Kolydas
  • 1,290
  • 2
  • 14
  • 33
14
votes
4 answers

Removing accents and diacritics in kotlin

Is there any way to convert string like 'Dziękuję' to 'Dziekuje' or 'šećer' to 'secer' in kotlin. I have tried using java.text.Normalizer but it doesn't seem to work the desired way.
Rebronja
  • 145
  • 1
  • 6
14
votes
4 answers

What is the point of tailrec in Kotlin?

tailrec optimizes functions where there is tail recursion. Why doesn't the compiler just optimize it anyway? C compilers optimize for tail recursion. You don't have to mark the method as having tail recursion. The compiler just notices that the last…
HelloWorld
  • 2,271
  • 21
  • 34
14
votes
2 answers

How to parse Unix timestamp to date string in Kotlin

How can I parse a Unix timestamp to a date string in Kotlin? For example 1532358895 to 2018-07-23T15:14:55Z
Chris Edgington
  • 2,384
  • 4
  • 20
  • 34
14
votes
7 answers

Kotlin - Overwrite Obj Props With Modified Obj Props if Not Null

TL;DR: How do I make this less redundant (any approach that works helps)? if (personModification.firstName != null) {person.firstName = personModification.firstName} if (personModification.lastName != null) {person.lastName =…
VSO
  • 8,950
  • 19
  • 69
  • 148
14
votes
3 answers

Kotlin sort hashmap in descending order

I have val myHashMap = HashMap>(), hashmap key value is formatted date as a string for example 20-06-2018 how can I sort this hashMap in descending order? expected result: 22-06-2018 : [] 21-06-2018 : [] 20-06-2018 :…
LeTadas
  • 2,936
  • 4
  • 24
  • 45
14
votes
3 answers

How to provide database to MyFirebaseMessagingService using Dagger 2 so that I can store fcm message locally in android

How can I make it possible to pass database Instance to the MyFirebaseMessagingService class which extends FirebaseMessagingService so that I can save data payload locally? Note: I already setup dagger 2 in my app, it's working perfectly. Below is…
14
votes
3 answers

IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources

I'm using Antlr in a simple Kotlin/Gradle project, and while my Gradle build is generating Antlr sources, they are not available for importing into the project. As you can see (on the left), the classes (Lexer/Parser, etc.) are being generated. I…
Craig Otis
  • 27,979
  • 23
  • 117
  • 216