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

Kotlin: fun vs val

Kotlin supports computed properties but I am not sure when to use them. Let's say I have a class: class Car(val color: String) and have this function that returns true if the car is white: fun isWhite(car: Car): Boolean { return car.color ==…
functionaldude
  • 682
  • 6
  • 14
22
votes
3 answers

Why ".kt" extension is sometimes visible and sometimes not?

In on of my Android Studio Kotlin projects, sometimes .KT is shown and sometimes not, in the Project explorer, in the Activity folder. What is the criteria used by Android Studio to show or hide the .KT extension ? Thanks
KotlinIsland
  • 799
  • 1
  • 6
  • 25
22
votes
9 answers

Kotlin-DataBinding-Error: Check your module classpath for missing or conflicting dependencies

How can I solve this problem. I can't make this data binding work, I have tried everything. Build.gradle(module app) apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin:…
Alen
  • 949
  • 3
  • 17
  • 37
22
votes
3 answers

How to mock Build.VERSION.SDK_INT using mockk

How can I mock Build.VERSION.SDK_INT in mockk? I've done the following: @Test fun testFoo(){ mockkStatic(Build::class) mockkStatic(Build.VERSION::class) every { Build.VERSION.SDK_INT } answers { 22 } } I end up getting…
JHowzer
  • 3,684
  • 4
  • 30
  • 36
22
votes
1 answer

does "object" in kotlin get garbage collected

If we have an Object like this object Repo { var activeMovies: ArrayList? = null } and then we call it like this to assign a value Repo.activeMovies = movieList after the Activity that instantiated it is finish, does it get Garbage…
FantomasMex
  • 219
  • 3
  • 11
22
votes
3 answers

public static void main in Kotlin

In Java, especially in Android studio, every time that I want to run or test some Java source code quickly, I will create public static void main (shortkey: psvm + tab) and the IDE will show "Play" button to run it immediately. Do we have some…
phatnhse
  • 3,870
  • 2
  • 20
  • 29
22
votes
2 answers

Abstract val with annotation in kotlin android

Can I write: @IdRes abstract fun getHeaderId(): Int With a val instead of a fun in kotlin? It complains I need a backing field or delegate when i write: @IdRes <-- errors abstract val headerId: Int Which is the most idiomatic in this case?…
Adam
  • 2,845
  • 2
  • 32
  • 46
22
votes
15 answers

Kotlin: open new Activity inside of a Fragment

How can I open a new Activity inside of a fragment when using a button? I tried this override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) LogOut_btn.setOnClickListener { …
L. Busekrus
  • 412
  • 1
  • 4
  • 11
22
votes
5 answers

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch

I'm using androidx navigation architecture along with Kotlin 1.2.71 in Android studio 3.2.1. My fragment code is: package com.dell.andnav.fragments import android.os.Bundle import android.support.v4.app.Fragment import…
Ajay Kulkarni
  • 2,900
  • 13
  • 48
  • 97
22
votes
5 answers

How to add date separators in recycler view using Paging Library?

After a lot of searching, I know its possible with regular adapter, but I have no idea how to do it using Paging Library. I don`t need code just a clue. Example
someguy234
  • 223
  • 2
  • 6
22
votes
1 answer

What's the purpose of `val` property with `final` modifier?

Recently IntelliJ suggested to add final to one of a val properties. This particular property was initialized in init {} block. I've tried to find out what is the semantics of final val construct and when should I use it, but Kotlin is all about…
wst
  • 4,040
  • 4
  • 41
  • 59
22
votes
4 answers

How to get min/max from ArrayList based on its object attribute values?

What I want to achieve is to get min/max attribute value of object from ArrayList. For example if Object has attribute weight(float), I want heaviest object from the list. I've tried to implement Comparable to get max/min value but this…
martin1337
  • 2,384
  • 6
  • 38
  • 85
22
votes
1 answer

JobScheduler JobService is started without Application

We converted our main Application class to Kotlin recently. Since then we are experiencing crashes, especially during the night (when our application was probably killed by the system), when our JobService is startet. We are accessing the…
marilion91
  • 2,094
  • 1
  • 19
  • 28
22
votes
3 answers

Testing private methods in Kotlin

How to test private methods in Kotlin? I tried to add @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) from androidx.annotation.VisibleForTesting but it doesn’t make my function private This is how I’m using it @VisibleForTesting(otherwise…
jakub
  • 3,576
  • 3
  • 29
  • 55
22
votes
2 answers

How to remove left indent in the PreferenceScreen?

Since PreferenceFragment is deprecated, I use PreferenceFragmentCompat. After replacing the fragment, I get left indentation from the content.: Indentation most likely appears because of the icons, but I do not use them (default is no icon). I've…
Holofox
  • 597
  • 4
  • 11