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
318
votes
5 answers

Kotlin and new ActivityTestRule : The @Rule must be public

I'm trying to make UI test for my android app in Kotlin. Since the new system using ActivityTestRule, I can't make it work: it compiles correctly, and at runtime, I get: java.lang.Exception: The @Rule 'mActivityRule' must be public. at…
Geob-o-matic
  • 5,940
  • 4
  • 35
  • 41
312
votes
10 answers

Kotlin – String formatting

Kotlin has an excellent feature called string templates. val i = 10 val s = "i = $i" // evaluates to "i = 10" But is it possible to have any formatting in the templates? For example, I would like to format Double in string templates in kotlin, at…
MajesticRa
  • 13,770
  • 12
  • 63
  • 77
303
votes
31 answers

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15

In Stripe, my client wants email and cardholder name, but the Stripe payment UI doesn't provide that option in com.stripe.android.view.CardMultilineWidget. I wanted to give it a try with the latest stripe version, I was using Stripe version…
Karthy Sbk
  • 3,153
  • 2
  • 8
  • 7
300
votes
9 answers

Warning "Kotlin plugin version is not the same as library version" (but it is!)

I have an Android studio project in which I have added a Java library module, which I call core. My three Gradle build files look like this. project/build.gradle buildscript { ext.kotlin_version = '1.2.40' repositories { google() …
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
299
votes
32 answers

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0

I've got a gradle FAILURE: ..."Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0." Case description: Attached to the project codebase the next libs: APP/build.gradle //(Required) Writing and executing…
Philipp Buhaievskiy
  • 3,217
  • 2
  • 10
  • 17
298
votes
14 answers

Android Get Current timestamp?

I want to get the current timestamp like that : 1320917972 int time = (int) (System.currentTimeMillis()); Timestamp tsTemp = new Timestamp(time); String ts = tsTemp.toString();
Rjaibi Mejdi
  • 6,820
  • 3
  • 21
  • 26
296
votes
25 answers

How can I get a random number in Kotlin?

A generic method that can return a random integer between 2 parameters like ruby does with rand(0..n). Any suggestion?
Yago Azedias
  • 4,480
  • 3
  • 17
  • 31
288
votes
2 answers

How can I generate random number in specific range in Android?

I want to generate random number in a specific range. (Ex. Range Between 65 to 80) I try as per below code, but it is not very use full. It also returns the value greater then max. value(greater then 80). Random r = new Random(); int i1 =…
Mohit Kanada
  • 15,274
  • 8
  • 31
  • 41
286
votes
12 answers

`break` and `continue` in `forEach` in Kotlin

Kotlin has very nice iterating functions, like forEach or repeat, but I am not able to make the break and continue operators work with them (both local and non-local): repeat(5) { break } (1..5).forEach { continue@forEach } The goal is to…
voddan
  • 31,956
  • 8
  • 77
  • 87
284
votes
8 answers

How to create empty constructor for data class in Kotlin Android

I have 10+ variables declared in Kotlin data class, and I would like to create an empty constructor for it like how we typically do in Java. Data class: data class Activity( var updated_on: String, var tags: List, var…
Sai
  • 15,188
  • 20
  • 81
  • 121
282
votes
10 answers

What does the suspend function mean in a Kotlin Coroutine?

I'm reading Kotlin Coroutine and know that it is based on suspend function. But what does suspend mean? Can Coroutine or function get suspended? From https://kotlinlang.org/docs/reference/coroutines.html Basically, coroutines are computations that…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
278
votes
1 answer

Convert Kotlin Array to Java varargs

How can I convert my Kotlin Array to a varargs Java String[]? val angularRoutings = arrayOf("/language", "/home") // this doesn't work web.ignoring().antMatchers(angularRoutings) How to pass an ArrayList to a varargs method…
robie2011
  • 3,678
  • 4
  • 21
  • 20
272
votes
8 answers

What is the difference between launch/join and async/await in Kotlin coroutines

In the kotlinx.coroutines library you can start new coroutine using either launch (with join) or async (with await). What is the difference between them?
Roman Elizarov
  • 27,053
  • 12
  • 64
  • 60
269
votes
15 answers

How to call a function after delay in Kotlin?

As the title, is there any way to call a function after delay (1 second for example) in Kotlin?
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
268
votes
15 answers

Setting text in EditText Kotlin

I am trying to set text in a EditText but it says: Type mismatch. Required: Editable Found: String My code is as follow: String name = "Paramjeet" val nametxt = findViewById (R.id.nametxt) as EditText nametxt.text = name Don't say to use setText…
Paramjeet Singh
  • 2,930
  • 2
  • 9
  • 17