https://kotlinlang.org logo
Join SlackCommunities
Powered by
# forum
  • r

    rss

    03/31/2025, 2:59 PM
    Kotlin with gradle not working I am trying to initialise a basic Kotlin project using Gradle. I followed this official doc step by step but I am getting this error Error: Main method not found in class org.example.MainKt, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is my Main.kt file package...
  • r

    rss

    03/31/2025, 9:39 PM
    Error passing generic varargs array to other class Hey folks. I’m currently struggling with this problem and hope to find some assistance from the Kotline experts here Kotlin Playground Kotlin Playground: Edit, Run, Share Kotlin Code Online Explore Kotlin and practice your coding skills on the Kotlin...
  • r

    rss

    04/01/2025, 8:59 AM
    Comparison operators in when expressions I’m aware that I’m not the first person to bring this up, but all discussions on the matter I’ve found are several years old. When writing a when statement, it’s often the case that we’re dealing with numeric values (or generally: Comparables). However, a when case in Kotlin currently cannot use any comparison operators. For example, the following sadly doesn’t work: val myInteger: Int = randomInt() val description = when(myInteger){ < 0 -> "negative" 0 -> "zero" > 0 -> "positive" }...
  • r

    rss

    04/01/2025, 10:59 AM
    Accidental override when using custom exception and extending RuntimeException and implementing GraphQLError in kotlin I have a custom exception class which extends RuntimeException and implements GraphQLError. There is a conflict between the message attribute in RuntimeException and getMessage() in GraphQLError. I see a post where this was mentioned in the past. Issue - 7658. Is there a resolution for this issue? The getter setter generated for message property in RuntimeException is in conflict with the method getMessage() in GraphQLError. How do we work around this. Any suggestions please. Thanks....
  • r

    rss

    04/03/2025, 7:59 PM
    Kotlin Multiplatform publishToMavenLocal does not include iOS targets on CI (but works locally) I’m working on a Kotlin Multiplatform Mobile (KMM) project where I publish shared modules to a local or remote Maven repository. My module targets: android jvm iosX64 iosArm64 iosSimulatorArm64 Locally, running the following command on my MacBook: ./gradlew publishToMavenLocal generates the correct output structure in .m2/repository/, including the iOS-specific folders: network-iosarm64/ network-iosx64/ network-iossimulatorarm64/ However, when I run this on Bamboo CI, using the same...
  • r

    rss

    04/05/2025, 6:19 AM
    How to maintain the subtype passed as an argument to a function? I defined this extension function which runs only when a collection is not empty. fun Collection.runIfNotEmpty(block: (Collection) -> Unit) = takeIf { isNotEmpty() }?.let { block(this) } Now I can use it with List and Set so I am happy. The problem is that the argument to the lambda let is always Collection and not the concrete List or Set I call this on. For example. // it has type Collection and not LIST emptyList().runIfNotEmpty { it -> println(it) }; // it has type...
  • r

    rss

    04/11/2025, 6:49 AM
    Saving Notifications from gmail and google applications does not work in android/kotlin (topic deleted by author) 1 post - 1 participant Read full topic
  • r

    rss

    04/11/2025, 6:49 AM
    Unable to read Gmail and system app notifications with NotificationListenerServic I’m building an Android app that includes a feature for logging all incoming notifications on the device. To capture these notifications, I’m using NotificationListenerService, and for persistent storage, I’m utilizing Room Database. For the most part, this implementation works well — the app is able to read and store notifications from a variety of apps successfully. However, I’m encountering issues specifically with some apps like Gmail, Pinterest, and a few other Google-related apps....
  • r

    rss

    04/13/2025, 12:09 PM
    Create new file using compiler plugin Not having much luck trying to figure out how to add a new file via a compiler plugin (the reason is I need to add a top level function with a specific package name, so I can’t just add a function to an existing file). I’ve tried creating a IrFileImpl and adding it to the module fragment, but then the compiler complains that source files != ir files. I’ve tried using buildFile {} inside IrGenerationExtension but that returns an FirFile whereas the module framegment is existing an IrFile. Just...
  • r

    rss

    04/14/2025, 7:49 PM
    Overload functions in Kotlin vs Java I wonder why Kotlin does not follow Java way in resolution of overloaded functions ambiguity. Java choses the most specific method at compile time based on types, whereas Kotlin gives compilation error. Please consider examples. Java: public interface Logger { void debug(Object entry, Throwable error); void debug(Function entryProducer, Throwable error); void debug(String template, Object arg1, Object ... args); } public void test(Logger log) {...
  • r

    rss

    04/15/2025, 3:59 PM
    Compose for native linux/windows app Hello everyone! I found out about Kotlin Multiplatform and Compose Multiplatform few days ago and as I understood I can develop native apps for Linux and Windows with it. It sounds cool so I started to try. But not succeed. If I create project using KMP Wizard for Desktop it uses JVM as a target. If I add linuxX64 or mingwX64 target, Gradle throws errors because Compose libraries don’t work with them. So I don’t understand how to use Compose for native build. Please help me. If you have...
  • r

    rss

    04/18/2025, 11:19 AM
    Sync folder/file detection in Android In my application, I want to allow the user to select a file, which comeback as a URI. This URI can natively resolve to a file path. I want to determine whether the file is stored locally or if it’s from a synced cloud storage provider such as Google Drive, OneDrive, Dropbox, etc. Is there a reliable way to detect whether a given file (from a URI or resolved path) originates from a synced cloud provider? If so, how can I identify which service it belongs to (e.g., Google Drive vs. OneDrive)?...
  • r

    rss

    04/23/2025, 4:19 AM
    Value class constructors Can you transform arguments to value classes in the way you can in Scala? class NormalizedString(private val s: String) extends AnyVal { def value: String = s.trim.toLowerCase } object Main extends App { val myString = new NormalizedString(" Hello World ") println(myString.value) // Output: "hello world" } 1 post - 1 participant Read full topic
  • r

    rss

    04/24/2025, 6:39 AM
    Updated Kotlin Courses for 2025 Good day here , I am learning Kotlin for Android development but I am facing a challenge as all the course I have come across are a bit outdate. Any recommendations for updated courses?? 1 post - 1 participant Read full topic
  • r

    rss

    04/24/2025, 8:59 PM
    How to get bytes from JS Blob I cannot figure out how to get to raw bytes from Blob (org.w3c.files.Blob). Kotlin JS representation of Blob does have any suitable method 1 post - 1 participant Read full topic
  • r

    rss

    04/25/2025, 11:09 AM
    Nullability check optimization I wonder what is the reason for this kotlin compiler behavior? It would be more reasonable to make mandatory only the first nullability check in this example, as the rest of the chain is kind of guaranteed: interface X { val link: X } fun test(x: X?) { val linkedX: X? = x?.link?.link?.link val linkedX2: X? = x?.link.link.link } But no, it is not so: <a class="lightbox"...
  • r

    rss

    04/26/2025, 1:49 AM
    Compile Time Union Types: A Small Step forward towards Denotable Union Types The proposal about union types was not put forward by me first, see KT-13108. Here we only disscuss about Compile Time Union Types. Currently, the type of when/if expression is the common super type(s) of each case. For example, the local varibale x in the following code was infered as Comparable & java.io.Serializable. var b = false fun main() { val x = if (b)...
  • r

    rss

    04/29/2025, 1:09 PM
    Opening Compose Multiplatform screen based on notification payload on iOS I am developing application that uses Jetbrains Compose both for Android and iOS. Thing is I want to implement feature: when I click push-notification I want it to open application and navigate to specific route (I am using Compose Multiplatform Navigation). I already solved this issue for Android pretty trivially but I cannot figure out how can I implement that thing in my iOS app. I will be very greateful for any help 1 post - 1 participant <a...
  • r

    rss

    04/30/2025, 6:09 PM
    Not enough information to infer type variable T I’m using a library function call for which the return type is Any. So, in this case the actual return type is an array of doubles. Actually, what’s returned is {double[1]@ …} so it is (I think) a two dimensional array of doubles. So, I want to extract a single array of doubles or Doubles that is, I just want to get at the numbers. This is proving to be a huge hassle. So much of Kotlin ultimately ends up being a hassle. Is there a quick and dirty way to convert/cast this Any type to an...
  • r

    rss

    05/02/2025, 11:49 PM
    There are no ecs library for android app development There is no ECS library for developing Android apps, so I tried using the ECS way to create a simple todo application. Anyone interested, please check. If you have any ideas, please share them! github

    quick view▾

    1 post - 1 participant <a...
  • r

    rss

    05/03/2025, 11:09 AM
    Improved Any?.toString I love the fact that we have Any?.toString but it returns “null” when the receiver is (duh) null. Instead of this, we could pass an optional String parameter which could be the return value. My implementation: fun Any?.toString(nullReplacement: String = "null"): String { if(this == null) return nullReplacement return this.toString() } How can this be useful? Writing less code for providing the replacement. Example usage: Before return if(foo.bar?.baz == null) "baz is null" else...
  • r

    rss

    05/04/2025, 3:49 PM
    Context Parameters support for PSI Does ContextParameter will be added to psi like “KtContextParameter” or not? 1 post - 1 participant Read full topic
  • r

    rss

    05/05/2025, 1:09 PM
    How to Compile Kotlin Code at Runtime from Java I have a task where I need to compile Kotlin source code at runtime from within a Java application. Is kotlin-compiler-embeddable the recommended way to achieve this? For now, I am using K2JVMCompiler, but I’m not sure if this is the best and most stable way. Is this the right tool for compiling Kotlin code from Java at runtime, or are there better alternatives or best practices I should consider? 1 post - 1 participant <a...
  • r

    rss

    05/07/2025, 1:39 PM
    Bug of type checking for numeric types on Js platform It looks like Kotlin 1.9.22 common code does not properly do type checking of primitive types on Js platform. Is this a bug or a feature? Is it fixed in later versions? val x: Double = 180.0 val y: Any = x if (y is Byte) { println("Y is Byte") } 1 post - 1 participant Read full topic
  • r

    rss

    05/08/2025, 11:39 AM
    Is Maven a Viable Replacement for Gradle in Kotlin Development? I have a low-spec potato laptop (i3 gen 3 2ghz with 8gb ram), and I’ve tried using Gradle with IntelliJ—it felt like my laptop was about to explode. I looked for alternatives and tried Gradle with VSCode. This time it didn’t feel like it was going to explode, but it was still very heavy. Finally, I tried Maven with VSCode, and with this setup, my laptop ran normally like usual. My question is: is it okay to develop Kotlin using the Maven build system? From what I’ve read, Kotlin is very...
  • r

    rss

    05/09/2025, 2:59 AM
    suspendCoroutine COMPLETELY BROKEN? I have tried the following code: Kotlin Playground Kotlin Playground: Edit, Run, Share Kotlin Code Online Explore Kotlin and practice your coding skills on the Kotlin Playground! Simply type a snippet of code and click Run to try it on the fly....
  • r

    rss

    05/09/2025, 10:39 AM
    Parameter and method precedence I had this (very simplified) code: // In a generic lib interface ISerializer { //... } // In a project using this lib class MySerializer : ISerializer { fun writeMyBlock(name: String, exp: ISerializer.() -> Unit) { //... exp() //... } } Months later, in the generic lib, I simply add a method in my interface (unfortunately, I choose exp as name): // In a generic lib interface ISerializer { fun exp(vararg s: String) { //... } } Some of my projects are then silently buggy: The...
  • r

    rss

    05/10/2025, 9:39 AM
    Access value by type with any kind of data structure Suppose I need to keep three numbers of Int, Long and Float in a bunch, and access these numbers by their type. First idea is to use HashMap, where key is type of number, and value is number itself. val bunch = mapOf( Int::class to 0, Long::class to 0L, Float::class to 0f, ) But in this case Kotlin does not inference needed value type, and I need to use explicit cast. val i: Int = bunch[Int::class]!! // Error: Initializer type mismatch: expected 'Int', actual 'Number'. val f:...
  • r

    rss

    05/10/2025, 4:19 PM
    Infer type from filterIsInstance Having interface sealed interface Param { val value: T @JvmInline value class Power(override val value: Float) : Param @JvmInline value class Income(override val value: Float) : Param } I put some instances of it to a list val params = listOf( Param.Power(1f), Param.Income(2f) ) Then I want a function, which should return value of a parameter by it’s type, like inline fun get() = params.filterIsInstance().first().value But this one does return Any? instead...
  • r

    rss

    05/11/2025, 5:59 AM
    Ghee in the morning is one more than one of the day of lord Krishna is one more than one of my friends and I Dud e Number of lord Krishna is the world of lord Shiva is the world of the world of the world of the world of the world of the world of lord 1 post - 1 participant Read full topic