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

IntArray vs Array in Kotlin

I'm not sure what the difference between an IntArray and an Array is in Kotlin and why I can't used them interchangeably: I know that IntArray translates to int[] when targeting the JVM, but what does Array translate to? Also, you can…
frankelot
  • 13,666
  • 16
  • 54
  • 89
121
votes
5 answers

How to use spring annotations like @Autowired in kotlin?

Is it possible to do something like following in Kotlin? @Autowired internal var mongoTemplate: MongoTemplate @Autowired internal var solrClient: SolrClient
eendroroy
  • 1,471
  • 2
  • 12
  • 16
121
votes
8 answers

How to create an empty array in kotlin?

I'm using Array(0, {i -> ""}) currently, and I would like to know if there's a better implementation such as Array() plus, if I'm using arrayOfNulls(0) as Array, the compiler will alert me that this cast can never succeed. But it's…
huangcd
  • 2,369
  • 3
  • 18
  • 26
119
votes
6 answers

Kotlin: Difference between object and companion object in a class

What is the difference between an object and a companion object in a class in kotlin? Example: class MyClass { object Holder { //something } companion object { //something } } I already read that companion object…
Poweranimal
  • 1,622
  • 3
  • 12
  • 16
119
votes
2 answers

Create an instance of an abstract class in Kotlin

I'm new to Kotlin and I'm trying to use it in my Android project. I have this code: public var oneTouchTimer: CountDownTimer = CountDownTimer(500, 100) { override fun onTick(l: Long) { } override fun onFinish() { } } And it's…
Sloganho
  • 2,041
  • 3
  • 16
  • 20
117
votes
17 answers

Hilt Unsupported metadata version in Kotlin

I was tried to run my code in Kotlin 1.5.10 With plugin as plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id 'dagger.hilt.android.plugin' and dependencies as below dependencies { ... //Dagger - Hilt …
116
votes
12 answers

Why kotlin gradle plugin cannot build with 1.8 target?

I have the simplest gradle project configured using intellij for kotlin 1.2.10. Here is my build.gradle file: buildscript { ext.kotlin_version = '1.2.10' repositories { mavenCentral() } dependencies { classpath…
alisabzevari
  • 8,008
  • 6
  • 43
  • 67
116
votes
8 answers

What is a "receiver" in Kotlin?

How is it related to extension functions? Why is with a function, not a keyword? There appears to be no explicit documentation for this topic, only the assumption of knowledge in reference to extensions.
F. George
  • 4,970
  • 4
  • 20
  • 38
116
votes
20 answers

Cannot find setter for field - using Kotlin with Room database

I'm integrating with the Room persistence library. I have a data class in Kotlin like: @Entity(tableName = "story") data class Story ( @PrimaryKey val id: Long, val by: String, val descendants: Int, val score: Int, …
gsb
  • 5,520
  • 8
  • 49
  • 76
116
votes
5 answers

What's Kotlin Backing Field For?

As a Java developer, the concept of a backing field is a bit foreign to me. Given: class Sample { var counter = 0 // the initializer value is written directly to the backing field set(value) { if (value >= 0) field = value …
Yudhistira Arya
  • 3,491
  • 6
  • 25
  • 42
116
votes
6 answers

How to get generic parameter class in Kotlin

Firebase's snapshot.getValue() expects to be called as follows: snapshot?.getValue(Person::class.java) However I would like to substitute Person with a generic parameter that is passed into the class via the class declaration i.e. class…
Jaja Harris
  • 4,266
  • 4
  • 23
  • 22
115
votes
6 answers

How to use @Parcelize now that kotlin-android-extensions is being deprecated?

How do I replace annotation class Parcelize from package kotlinx.android.parcel with @Parcelize which is not coming from the kotlin-android-extensions plugin?
vepzfe
  • 4,217
  • 5
  • 26
  • 46
115
votes
6 answers

How Kotlin coroutines are better than RxKotlin?

Why would I want to use Kotlin's coroutines? It seems that the RxKotlin library is much more versatile. Kotlin's coroutines look significantly less powerful and more cumbersome to use in comparison. I base my opinion on coroutines on this design…
charlie_pl
  • 2,898
  • 4
  • 25
  • 39
113
votes
5 answers

RequiresApi vs TargetApi android annotations

Whats the difference between RequiresApi and TargetApi? Sample in kotlin: @RequiresApi(api = Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M) class FingerprintHandlerM() : FingerprintManager.AuthenticationCallback() NOTE:…
113
votes
2 answers

How do I manage unit test resources in Kotlin, such as starting/stopping a database connection or an embedded elasticsearch server?

In my Kotlin JUnit tests, I want to start/stop embedded servers and use them within my tests. I tried using the JUnit @Before annotation on a method in my test class and it works fine, but it isn't the right behaviour since it runs every test case…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227