/r/Kotlin
Discussion about Kotlin, a statically typed programming language for the JVM, Android, JavaScript, and native.
Kotlin is a statically typed programming language for the JVM, Android, JavaScript, and native.
Related subreddits:
Resources:
/r/Kotlin
Are you a Kotlin Multiplatform developer? We want to hear from you! The Kotlin Multiplatform survey is now live, and this is your chance to share your feedback on the tools, ecosystem, and your overall experience. Your input will help us identify areas for improvement and prioritize future updates to better meet your needs.
🕑 The survey should take approximately 10 minutes to complete.
🎁 Fill out the survey with meaningful answers to get the chance to win a prize of your choice:
Thank you for helping us make Kotlin Multiplatform even better!
I have a question regarding a scenario where I have a list of data that I'm processing using an async block. Inside the async block, I'm performing some operations. Should the function being called within the async block be a suspend function to ensure that if the parent scope/job is canceled, the parallel tasks running within the async block are also canceled? Or is it sufficient for the function inside the async block to be a regular function?
suspend fun processRecords() {
withContext(appDispatchers.io) {
(1..1000000)
.map {
async {
processHeavyComputation()
}
}.awaitAll()
}
}
suspend fun processHeavyComputation() {
delay(100)
println("some work")
}
fun processHeavyComputation() {
println("some work")
}
and then calling this processRecords from viewModel using viewModelScope like below?
private fun processRecords(currentTimeMillis: Long) {
currentJob =
viewModelScope
.
launch
(dispatcher.io) {
someObj.processRecords()
}
}
fun stopProcessingRecords() {
currentJob.cancel()
}
We’re excited to announce ksoup v0.2.1, the latest update to our Kotlin Multiplatform library for seamless HTML parsing.
What’s New:
• Updated dependencies: Kotlin 2.1.0, jsoup 1.18.2, Ktor2 2.3.13, and more.
• watchOS Support: Expanding cross-platform compatibility.
• New Features: Charset support and ControllableInputStream.
• Improved Performance: Faster I/O with fleeksoft-io.
• Modular updates for better clarity and efficiency.
Check it out:
• GitHub: fleeksoft/ksoup
We’d love to hear your feedback! 🚀
Hi everyone,
I'm working on a BLE (Bluetooth Low Energy) remote app using Kotlin and need some advice from the community. The goal of this app is to connect with a hardware device via BLE, access its storage, and fetch the data stored on the device to display it in the app.
Here’s what I’ve done so far:
I would appreciate any advice, tutorials, or code snippets that could help me tackle this challenge. Thanks in advance for your support!
P.S.: If you’ve worked on a similar project, I’d love to hear about your experience and any tips you can share.
Looking forward to your suggestions!
Best regards,
Sartaj Qamar
However, I’m stuck on how to access and fetch data from the hardware's storage once the BLE connection is established.
I have integrated a python script within the android app using chaquopy. Now I want create a data flow between the two. I figured out how to send data from kotlin to python, but now I want to expose a kotlin function that python can call to send data back to kotlin. But I am not able to find the right implementation. Would appreciate some help on what’s the best way to do this.
I'm l.ooking for a developer that can build a app that can remotely execute USSD codes and remotely send commands during the USSD session.
EDIT: it basically involves using the telephonyManager to dial USSD codes (eg. *123") This starts a USSD session with the mobile network where you can navigate through a menu using numbered responses.
Hey Kotlin folks!
I’m exploring the client-side tech stack for a streaming app like Netflix.
The app will target Android and iOS, so I’m considering Flutter, React Native, fully native with Kotlin & Swift, or Kotlin Multiplatform!
*Features include DRM-protected video playback
What do you think is the most recommended option?
Of course, fully native is ideal in theory, but I’d love to hear your thoughts on balancing the effort of separate platform development with other options.
Thanks in advance! 🙏
Hi,
In AndroidStudio I can't use \@Serializable
without opting-in for \@kotlinx.serialization.InternalSerializationApi
. Why is that? None of the tuto I found online mentionthis and I can't find help on this problem online either.
In project pluggins I have id("org.jetbrains.kotlin.plugin.serialization") version "2.0.21" apply false
.
In module pluggins id("org.jetbrains.kotlin.plugin.serialization")
, and in dependencies implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
.
Thanks
Edit:
module
plugins {
application
id("com.android.application") version "8.7.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.24" apply false
id("org.jetbrains.kotlin.jvm") version "2.0.21" apply false
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.21" apply false
}
project
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.serialization")
}
android {
namespace = "com.example.queezer"
compileSdk = 34
defaultConfig {
applicationId = "com.example.queezer"
minSdk = 16
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
implementation("androidx.activity:activity-ktx:1.8.2")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
}
With the preview of guards in 2.1.0, the decision between using when
vs if
/else if
/else
has moved towards when
. Considering readability, "declarativeness", safety, conciseness, when
in my opinion seems to generally win over if/else
when there are 3+ branches. The main scenarios I can think of where if/else
is still preferable are:
when
adds the braces at the top and bottom, this would add a lot of vertical space usage that might not be justified for 2 branchesExamples:
if (a) x
else if (b) y
if (a) {
x
}
else {
x
}
if/else
blocks. Similar to the above, the extra vertical space from a when
here might not be worth it.Examples:
if (a) x
else if (b) y
else if (c) z
else y + z
if/else if/else
expression can be in a single line. Here we might not want to expand it to 5 lines with a when statement.Example:
if (a) x else if (b) y else z
The above cases, however, are fairly specific. Would you agree that in all other cases, and more generally when
is preferable? Or do you have a different way of thinking about this decision?
"I'm embarking on my Kotlin journey!" As a beginner, I'm keen on grasping the core concepts quickly. What are the most essential building blocks I should focus on? What are the best ways to learn Kotlin effectively and efficiently? What are some common pitfalls to avoid as a beginner Kotlin developer? Also, what are some practical projects that could help solidify my understanding and showcase my skills? Any tips, tricks, or resources from experienced Kotlin developers would be greatly appreciated!
Hey i have a graduation project this year I will build an app connected to obd 2 elm 327 bluetooth The app will send to the user notifications for preventive maintenance changing oil,filters,tires ... When he cross a number of kilometeres like oil when u cross 10000 km u must change the oil of the car I have only six months to learn and build the app do u advice me kotlin or flutter I don't care about multi cross platforms of the beauty of the app All what i care is the succes of the idea Please need help asap
During my last vacation, shortly after the release of Ktor 3 with WASM support, I felt inspired to start a new side project leveraging this technology. I created a game map browser for the game Oxygen Not Included using Compose Multiplatform and Ktor, paired with a Ktor server backend.
You can explore it at stefan-oltmann.de/oni-seed-browser/
The code is open-source, licensed under AGPL, and hosted on GitHub.
I believe it won't be long before Compose for Web is ready for production use. This side project also served as an opportunity to evaluate the current state of the technology, and I must say, I'm thoroughly impressed.
Hey everyone!
We’re thrilled to announce the release of fleeksoft-io 0.0.2, our Kotlin Multiplatform I/O library, now packed with exciting updates to make your development experience even better.
🚀 What’s New in 0.0.2?
1️⃣ okio Extension
2️⃣ New URI Module
3️⃣ Standard and Extended Charset Modules
4️⃣ Dependency Updates
• kotlinx-io bumped to version 0.6.0.
• Kotlin bumped to version 2.1.0
🌐 Get Started
• GitHub: https://github.com/fleeksoft/fleeksoft-io
🤝 Feedback & Contributions
We’re building this library for the community, and your feedback is invaluable! If you have feature requests, ideas, or bugs to report, feel free to open an issue or contribute directly.
Let us know what you think and how you’re using fleeksoft-io in your projects!
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView6"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/textView3"
app:navGraph="@navigation/navigation" />
Want to compile my Kotlin command-line utilities to native macos for faster startup. When I tried with Kotlin-Native 2.0.21, got error saying full Xcode not found. I have Xcode Command-line Tools installed. Don't have enough disk space for full Xcode.
Any way to compile Kotlin to macos executable without full Xcode? Willing to use an older Kotlin version, if it previously worked with only Xcode Command-line Tools.
Hello, I'm trying to build a native android app that heavily depends on SMS retrieval and processing. I have defined the permissions, granted them at run time and defined my broadcast receiever in the manifest.
Everything works. But the app is being flagged as malware by all devices.
I don't want to use foreground services as it will use an intrusive persistent notification. I just want the app to work in a way that it generates a Json from some SMS messages.
I know this is achievable, since Truecaller does it. I don't want persistent notifications and the app should work even when it's not in use.
Is there something I'm doing wrong? Truecaller does this perfectly even without being set as the default SMS app and it will not show any notifications, except for when you get the SMS.
How can I achieve this?
Disclaimer! I am the library author.
Hey friends! I've been tidying up my library for advent of code and I'd love to share with you the 0.2.1 update and get your feedback. This is something that I used myself but released as a library. It provides a DSL for generating test cases for your Advent of Code puzzles.
I also wanted to try out Writerside, so I revamped the documentation with a new website. There is also a project template, if you'd like to try AoC with Kotin this year.
Good luck to everyone!
Hey,
I've recently learned about concurrency and parallelism with both threads and coroutines.
I wanted to actually understand how they work and are built on a deeper level. I also asked about them here, and your answers were really helpful. Now that I think I had learned and tried both for quite a bit, I'd like to start using them in actual side projects.
But here's my new question: When should I start using coroutines (and threads) ?
I mean, for Threads it's even harder to decide.
I know that both Threads and Coroutines have some kind of overhead, Coroutines less since they are implemented in the application-layer and have less overhead in general.
But since both have an overhead, it must mean that they only become viable to use at one point and can be bad if used too early.
Can you give practical advice on when using Threads and when using Coroutines becomes viable?
Threads for reading 2 files for some operation? Coroutines already when I have a db-call and a network-call in one function?
If you check our Kotlin Gilded Rose codebase out of GitHub (https://github.com/dmcg/gilded-rose-tdd) and try to build it, you will find that you need to be running Postgres just in order to compile things, and then again in order to run the unit tests.
That’s not too much of an issue on my Mac, but with the expansion of the team we would like to set up a cloud continuous integration server, and there we are going to want to manage the Postgres lifecycle inside our build.
It would be a start if only there was a way to manage Docker containers in our tests. Containers in our tests ... Testcontainers!
In this episode
There is a playlist of TDD Gilded Rose episodes - https://www.youtube.com/playlist?list=PL1ssMPpyqocg2D_8mgIbcnQGxCPI2_fpA
The codebase is available on GitHub https://github.com/dmcg/gilded-rose-tdd
If you are going to be at KotlinConf 2025, or even just in Copenhagen in May, then you should totally be signing up for the workshop that Nat Pryce and I are running. It’s called Refactoring to Functional Kotlin, and will give you hands-on experience of taking legacy code and safely migrating it to a functional style. Places are limited, so buy now at https://kotlinconf.com/workhops
If you like this video, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.
I've been wondering if it's possible to run specific modules with node.js while the rest of the backend runs on ktor.
For example, Bluesky has an SDK for Typescript but not for Kotlin, so the way to use it would be to create a microservice just for the Bluesky api.
Has anyone faced this kind of situation?
Our Android project follows a multi-module architecture with:
- 20 features
- Each feature is split into 4 modules:
- Only the app module can access private modules (for DI purposes) to bind the implementation with the interfaces
- Using Koin for dependency injection
- Most core feature data layers have been converted to KMP modules
our target is to keep the native ui and share the business logic only
## Current Migration Progress
We've started migrating to KMP with the following approach:
## Challenge
We're facing a architectural challenge with our library approach:
- Moving business logic and data modules into a single shared library compromises modularity
- Features that previously only depended on their own public data will now have access to both private and public data of all features
- This breaks our careful module isolation and risks creating unwanted dependencies
## Question
How can we migrate to KMP while maintaining our current modular architecture? Specifically:
## Technical Details
- DI Framework: Koin
- Target Platforms: Android, iOS, data only with native ui
- Current Approach: Library-based sharing