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
145
votes
9 answers

Override getter for Kotlin data class

Given the following Kotlin class: data class Test(val value: Int) How would I override the Int getter so that it returns 0 if the value negative? If this isn't possible, what are some techniques to achieve a suitable result?
spierce7
  • 14,797
  • 13
  • 65
  • 106
145
votes
1 answer

Are there constructor references in Kotlin?

In Java we have the Class::new syntax for constructor references. I know, there are callable references for methods, but how about constructors? A typical use case for me would be factories.
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
142
votes
16 answers

Your project requires a newer version of the Kotlin Gradle plugin. (Android Studio)

I've just updated my flutter project packages to be null-safety compliant and now Android Studio wants me to update my project to use the latest version of Kotling Gradle Plugin. Can't see where to change this though. I have tried to change…
jeffmayn
  • 1,785
  • 2
  • 9
  • 22
142
votes
18 answers

Jetpack Compose - Column - Gravity center

I'm creating a layout with Jetpack Compose and there is a column. I would like center items inside this column: Column(modifier = ExpandedWidth) { Text(text = item.title) Text(text = item.description) }
142
votes
5 answers

How to create an instance of anonymous interface in Kotlin?

I have a third party Java library which an object with interface like this: public interface Handler { void call(C context) throws Exception; } How can I concisely implement it in Kotlin similar to Java anonymous class like…
Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
140
votes
19 answers

Kotlin Android start new Activity

I want to start another activity on Android but I get this error: Please specify constructor invocation; classifier 'Page2' does not have a companion object after instantiating the Intent class. What should I do to correct the error? My…
J Adonai Dagdag
  • 1,835
  • 2
  • 10
  • 22
140
votes
2 answers

Property must be initialized or be abstract

How to declare class field? Like we can have it in java: protected SharedPreferences mSharedPreferences; And later in onCreate(): mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) Now I can use it anywhere I want (in…
Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64
137
votes
8 answers

Getters and Setters in Kotlin

In Java, for example, I can write getters on my own (generated by IDE) or use Annotations like @Getter in lombok - which was pretty simple. Kotlin however has getters and setters by default. But I can't understand how to use them. I want to make it,…
nutella_eater
  • 3,393
  • 3
  • 27
  • 46
132
votes
22 answers

Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

I am getting the following error while building the project. haven't used CoordinatorLayout in this project. just added as a dependency in build.gradle : I am using Android Studio 3.2 Canary 4. LogCat AGPBI: {"kind":"error","text":"Program type…
Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27
132
votes
4 answers

static methods and variables in Kotlin?

I want to be able to save a class instance to a private/public static variable, but I can't figure out how to do this in Kotlin. public class Foo { private static Foo instance; public Foo() { if (instance == null) { …
Caleb Bassham
  • 1,874
  • 2
  • 16
  • 33
130
votes
19 answers

Execution failed for task ':app:mapDebugSourceSetPaths'. > Error while evaluating property 'extraGeneratedResDir' of task

Edit, TLDR: This is most likely a version incompatibility issue. Try to find the compatible google-services Gradle plugin version for your AGP (Android Gradle Plugin) or downgrade your AGP. The selected answer is updated (almost regularly) with…
130
votes
4 answers

What is the difference between init block and constructor in kotlin?

I have started learning Kotlin. I would like to know the difference between init block and constructor. What is the difference between this and how we can use this to improve? class Person constructor(var name: String, var age: Int) { var…
Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
130
votes
3 answers

Kotlin: withContext() vs Async-await

I have been reading kotlin docs, and if I understood correctly the two Kotlin functions work as follows : withContext(context): switches the context of the current coroutine, when the given block executes, the coroutine switches back to previous…
Mangat Rai Modi
  • 5,397
  • 8
  • 45
  • 75
130
votes
15 answers

Kotlin - How to correctly concatenate a String

A very basic question, what is the right way to concatenate a String in Kotlin? In Java you would use the concat() method, e.g. String a = "Hello "; String b = a.concat("World"); // b = Hello World The concat() function isn't available for Kotlin…
Daniele
  • 4,163
  • 7
  • 44
  • 95
130
votes
6 answers

How can I create an array in Kotlin like in Java by just providing a size?

How can I create a Array like we do in java? int A[] = new int[N]; How can I do this in Kotlin?
Kevin Mathew
  • 1,427
  • 2
  • 9
  • 9