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
267
votes
13 answers

startForeground fail after upgrade to Android 8.1

After upgrading my phone to 8.1 Developer Preview my background service no longer starts up properly. In my long-running service I've implemented a startForeground method to start the ongoing notification which is called in on…
Rawa
  • 13,357
  • 6
  • 39
  • 55
267
votes
9 answers

NullPointerException when trying to access views in a Kotlin fragment

How to use Kotlin Android Extensions with Fragments? If I use them inside onCreateView(), I get this NullPointerException exception: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View…
solidak
  • 5,033
  • 3
  • 31
  • 33
261
votes
11 answers

How to make "inappropriate blocking method call" appropriate?

I am currently trying to leverage kotlin coroutines more. But I face a problem: when using moshi or okhttp inside these coroutines I get a warning: "inappropriate blocking method call" What is the best way to fix these? I really do not want to be…
ligi
  • 39,001
  • 44
  • 144
  • 244
253
votes
39 answers

Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details

Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details build:gradle(Module:app) buildscript { repositories { maven { url 'https://jitpack.io' url…
Ankur_009
  • 3,823
  • 4
  • 31
  • 47
251
votes
16 answers

Swift 'if let' statement equivalent in Kotlin

In Kotlin is there an equivalent to the Swift code below? if let a = b.val { } else { }
iadcialim24
  • 3,817
  • 5
  • 21
  • 32
250
votes
8 answers

How can I resolve the error "The minCompileSdk (31) specified in a dependency's AAR metadata" in native Java or Kotlin?

The error message: The minCompileSdk (31) specified in a dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties) is greater than this module's compileSdkVersion (android-30).…
Mohammad Zeeshan
  • 4,525
  • 4
  • 12
  • 15
250
votes
4 answers

What does 'by' keyword do in Kotlin?

While developing for android I sometimes come across something that looks like this: var someModel: someViewModel by notNullAndObservable { vm -> ... } I don't understand what the significance of the by keyword is.
Mark
  • 9,718
  • 6
  • 29
  • 47
248
votes
2 answers

Kotlin : Public get private set var

What is the correct way to define a var in kotlin that has a public getter and private (only internally modifiable) setter?
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
248
votes
6 answers

How to convert List to Map in Kotlin?

For example I have a list of strings like: val list = listOf("a", "b", "c", "d") and I want to convert it to a map, where the strings are the keys. I know I should use the .toMap() function, but I don't know how, and I haven't seen any examples of…
LordScone
  • 3,207
  • 2
  • 18
  • 24
247
votes
10 answers

Kotlin's List missing "add", "remove", Map missing "put", etc?

In Java we could do the following public class TempClass { List myList = null; void doSomething() { myList = new ArrayList<>(); myList.add(10); myList.remove(10); } } But if we rewrite it to Kotlin…
Elye
  • 53,639
  • 54
  • 212
  • 474
243
votes
8 answers

What's the Kotlin equivalent of Java's String[]?

I see that Kotlin has ByteArray, ShortArray, IntArray, CharArray, DoubleArray, FloatArray, which are equivalent to byte[], short[], int[],char[], double[], float[] in Java. Now I'm wondering, is there any StringArray equivalent to Java's String[]?
chancyWu
  • 14,073
  • 11
  • 62
  • 81
242
votes
8 answers

How to check "instanceof " class in kotlin?

In kotlin class, I have method parameter as object (See kotlin doc here ) for class type T. As object I am passing different classes when I am calling method. In Java we can able to compare class using instanceof of object which class it is. So I…
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
241
votes
9 answers

Accessing Kotlin extension functions from Java

Is it possible to access extension functions from Java code? I defined the extension function in a Kotlin file. package com.test.extensions import com.test.model.MyModel /** * */ public fun MyModel.bar(): Int { return…
Lovis
  • 9,513
  • 5
  • 31
  • 47
233
votes
38 answers

android:exported needs to be explicitly specified for . Apps targeting Android 12 and higher are required to specify

After upgrading to android 12, the application is not compiling. It shows "Manifest merger failed with multiple errors, see logs" Error showing in Merged manifest: Merging Errors: Error: android:exported needs to be explicitly specified for .…
Ashique Bava
  • 2,486
  • 2
  • 9
  • 21
228
votes
17 answers

unrecognized Attribute name MODULE (class com.sun.tools.javac.util.SharedNameTable$NameImpl)

I received this build error having updated my native android project to Android S (Pre release of android 12). Googling for the error threw up no valuable hits. One hit from Netbeans a few years back and some Xamarin problems. The stack trace is…
Chris
  • 4,662
  • 2
  • 19
  • 27