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
2 answers

Unable to run parameterized tests with Android Test Orchestrator

I'm trying to run parameterized tests using Android Test Orchestrator. But for some reason parameterized tests won't start. I can run all tests properly without Orchestrator but I need it to clear some data between tests. This is output from Gradle.…
Aleksander Mielczarek
  • 2,787
  • 1
  • 24
  • 43
22
votes
3 answers

How can I combine two arrays into one array in Kotlin?

I want to make one array from two arrays. I tried to do use +: var array1 = intArrayOf(1,2,3) var array2 = intArrayOf(4,5,6) var array3 = array1 + array2 But it is not working unfortunately... How can I combine them?
RobotVice2001
  • 237
  • 1
  • 2
  • 4
22
votes
3 answers

How to get date 7 days ago to today in Kotlin?

I want to get date from 7 days ago to today in Kotlin. Any suggestions? This is what I have so far val date = Calendar.getInstance() val yesterday = Calendar.getInstance() yesterday.add(Calendar.DATE,-1) var todayOrYesterday:String? var…
user8896788
22
votes
5 answers

Building a Kotlin + Java 9 project with Gradle

I'm fairly new to Gradle (and Java 9, to be honest), and I'm trying to use Gradle to build a simple library project that is a mix of Java 9 and Kotlin. More in detail, there is an interface in Java and an implementation in Kotlin; I'd do everything…
Germano Rizzo
  • 481
  • 1
  • 3
  • 8
22
votes
3 answers

The @Rule > must be public ValidationError in Kotlin Junit test

I tried to use a unit test rule annotation and Android Studio didn't highlight any error here: @Rule val htmlManager = HtmlManager() However after executing the test following error happens: org.junit.internal.runners.rules.ValidationError:…
donfuxx
  • 11,277
  • 6
  • 44
  • 76
22
votes
4 answers

Kotlin negative modulo returns negative value

In Kotlin I have seen that for function a.mod(n), if a is negative the result is negative. Which is not what modulo is supposed to do. What can I do to have always positive modulo? For example: (-2).mod(9) returns -2 and should be 7.
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
22
votes
2 answers

Type mismatch: inferred type is String but Charset was expected in kotlin

I have the following code in my main activity: var qNa_list = parseQuestions(loadJSONFromAsset("qna_list.json")) fun loadJSONFromAsset(file_name:String): String? { var json: String? = null try { val isis = assets.open(file_name) …
Kotlinboy
  • 3,725
  • 4
  • 16
  • 27
22
votes
5 answers

How to make a Kotlin Comparable Type?

Just learning to define a DateRange type val wholeYear2017 = Date(2017,1,1)..Date(2017,12,31) So I created the type as below class DateRange>(override val start: Date, override val endInclusive: Date) :…
Elye
  • 53,639
  • 54
  • 212
  • 474
22
votes
3 answers

How to get ext.* variables into plugins block in build.gradle.kts

My build file looks like this: val nexusBaseUri: String by extra val gradle_version: String by extra val kotlin_version: String by extra buildscript { val nexusBaseUri by extra { "https://mynexusserver/nexus" } val gradle_version by extra {…
Andy
  • 8,749
  • 5
  • 34
  • 59
22
votes
3 answers

Kotlin Coroutines in Android Service

I have an Android service which starts and syncs different types of data with the server when it's online. I'm new to Kotlin coroutines and I'm trying to accomplish the following: fun syncData{ //Job1 make retrofit call to server //Job2 make…
julioribeiro
  • 1,565
  • 2
  • 14
  • 22
22
votes
4 answers

NoSuchMethodError: java.lang.Long.hashCode

I have the following override of method on hashCode in AbstractORM class: var _id = Random().nextLong() override fun getId() = _id // AbstractORM class implements an interface that defines this method getId() override fun hashCode() =…
m0skit0
  • 25,268
  • 11
  • 79
  • 127
22
votes
2 answers

Clean Architecture: Use different model classes for different data sources?

I am currently developing a news feed android app. I try to design my app according to the principles of clean architecture. In the data layer I am using the repository pattern as a facade for the diferent data sources: remote data from an API…
Elias
  • 563
  • 4
  • 18
22
votes
2 answers

synchronize property getters/setters

So I'm trying to get a handle on how Kotlin handles synchronization of properties. If I have this class: class Foo { var a = 0 var b = 0 } and I want to make sure that a & b have all access synchronized. How would I do it? I've tried using…
craigmiller160
  • 5,751
  • 9
  • 41
  • 75
22
votes
2 answers

How to inject primitive variables in Kotlin?

I am using Dagger2 for DI in my Android app, and using this code for injecting classes into my Activity is fine: @field:[Inject ApplicationContext] lateinit var context: Context but, lateinit modifier is not allowed on primitive type properties in…
22
votes
2 answers

How to detect values changed in Data class Kotlin?

I want to detect any values changed of a property of my class so then I could do another operation after that. In other word, if one of a specific data of a property is changed, then a specific event will fire by then. Actually, if it is a normal…
jujuzi
  • 389
  • 1
  • 4
  • 16