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

How to use TypeToken + generics with Gson in Kotlin

I'm unable to get a List of generic type from a custom class (Turns): val turnsType = TypeToken>() {}.type val turns = Gson().fromJson(pref.turns, turnsType) it said: cannot access '' it is 'public /*package*/' in 'TypeToken'
Juan Saravia
  • 7,661
  • 6
  • 29
  • 41
152
votes
8 answers

Android P visibilityawareimagebutton.setVisibility can only be called from the same library group

I'm trying to use the new Android P FloatingActionButton that's part of the com.google.android.material.floatingactionbutton.FloatingActionButton and I'm getting this warning: VisibilityAwareImageButton.setVisibility can only be called from the…
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
152
votes
4 answers

In Kotlin, how do I read the entire contents of an InputStream into a String?

I recently saw code for reading entire contents of an InputStream into a String in Kotlin, such as: // input is of type InputStream val baos = ByteArrayOutputStream() input.use { it.copyTo(baos) } val inputAsString = baos.toString() And also: val…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
152
votes
21 answers

Unresolved reference: kotlinx

I am trying to try out Kotlin and the Kotlin Android extensions in Android Studio. I have tried this both in Android Studio v 1.5.1 on Ubuntu 14.04, and in Android Studio v 1.5.1 on OS X El Capitan with the same result. Here is what I am doing: I…
nPn
  • 16,254
  • 9
  • 35
  • 58
151
votes
14 answers

How to get current local date and time in Kotlin

How to get current Date (day month and year) and time (hour, minutes and seconds) all in local time in Kotlin? I tried through LocalDateTime.now() but it is giving me an error saying Call requires API Level 26 (curr min is 21). How could I get time…
rgoncalv
  • 5,825
  • 6
  • 34
  • 61
151
votes
15 answers

Effective Enums in Kotlin with reverse lookup?

I'm trying to find the best way to do a 'reverse lookup' on an enum in Kotlin. One of my takeaways from Effective Java was that you introduce a static map inside the enum to handle the reverse lookup. Porting this over to Kotlin with a simple enum…
Baron
  • 1,535
  • 2
  • 10
  • 6
148
votes
8 answers

Could not find method kapt() for arguments

I'm facing a problem for over 3 days now and I can't solve. since I started to use Kotlin for Android,I stopped using "annotationProcessor" and started using "kapt", all things were working great with kapt until I started to build an Android Instant…
148
votes
28 answers

Safeargs library doesnt generate direction class

I use navigation library and safeargs for passing data. I define argument to fragment like that.
6155031
  • 4,171
  • 6
  • 27
  • 56
148
votes
6 answers

Kotlin equivalent of Java's equalsIgnoreCase

What is the equivalent of Java equalsIgnoreCase in Kotlin to compare String values? I have used equals but it's not case insensitive.
Farwa
  • 6,156
  • 8
  • 31
  • 46
148
votes
7 answers

"Not enough information to infer parameter T" with Kotlin and Android

I'm trying to replicate the following ListView in my Android app using Kotlin: https://github.com/bidrohi/KotlinListView. Unfortunately I'm getting an error I'm unable to resolve myself. Here's my code: MainActivity.kt: override fun…
Timo Güntner
  • 2,863
  • 4
  • 17
  • 24
148
votes
6 answers

Difference between a class and object in Kotlin

I'm new to Kotlin and have recently converted a simple file from java to Kotlin. I am wondering why the Android converter changed my java class to a Kotlin object. Java: public class MyClass { static public int GenerateChecksumCrc16(byte…
Crunchy234
  • 1,887
  • 2
  • 16
  • 21
148
votes
3 answers

Kotlin Data Class from Json using GSON

I have Java POJO class like this: class Topic { @SerializedName("id") long id; @SerializedName("name") String name; } and I have a Kotlin data class Like this data class Topic(val id: Long, val name: String) How to provide the…
erluxman
  • 18,155
  • 20
  • 92
  • 126
147
votes
31 answers

Kotlin unresolved reference in IntelliJ

I started off with this tutorial for learning Kotlin in IntelliJ IDEA. When I tried running the following example, fun main(args: Array) { println("lol") } Build fails with the following error: Error:(5, 5) Kotlin: Unresolved reference:…
Anony-mouse
  • 2,041
  • 2
  • 11
  • 23
147
votes
12 answers

Test expected exceptions in Kotlin

In Java, the programmer can specify expected exceptions for JUnit test cases like this: @Test(expected = ArithmeticException.class) public void omg() { int blackHole = 1 / 0; } How would I do this in Kotlin? I have tried two syntax variations,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
146
votes
7 answers

Kotlin: Equivalent of getClass() for KClass

In Java we can resolve a variable's class through getClass() like something.getClass(). In Kotlin I am aware of something.javaClass which is nice but I want to be able to get the KClass in a similar way. I've seen the Something::class syntax but…
Jire
  • 9,680
  • 14
  • 52
  • 87