Tüm Yazılar
KategoriAndroid
Okuma Süresi
16 dk okuma
Yayın Tarihi
...
Kelime Sayısı
988kelime

Kahveni hazırla - bu içerikli bir makale!

Kotlin 2.1 K2 compiler default, context parameters (experimental), guard conditions, multi-dollar string interpolation ve KMP iyileştirmeleri.

Kotlin 2.1: K2 Compiler + Context Parameters Yenilikler

# Kotlin 2.1: K2 Compiler + Context Parameters Yenilikler

Kotlin 2.1 (2024 Q4 release) K2 compiler'ı default yaptı — %2 daha hızlı compilation, daha iyi IDE experience. Context parameters experimental olarak geldi — dependency injection'ı simplify ediyor. Guard conditions, non-local break/continue, multi-dollar string interpolation gibi syntax iyileştirmeleri. Bu yazı Kotlin 2.1'in önemli yeniliklerini ve migration stratejisini anlatır.

💡 Pro Tip: Kotlin 2.0 → 2.1 major change K2 compiler default. Mevcut projelerde %95 sorunsuz geçer ama annotation processor'lar (KAPT → KSP2 migration) edge case'leri var.

İçindekiler


K2 Compiler: Default Değişiklik

K2 Kotlin compiler rewrite'ı 4 yıl sürdü, 2.0'da stable, 2.1'de default.

Performance

  • %2x daha hızlı compilation: (büyük projeler)
  • %40 daha az memory: (IDE'de)
  • %60 daha az recompilation: (incremental)
  • Better type inference: (generics, inline class)

Yeni Özellikler

  • Smart cast improvements (nullable types)
  • Sealed class exhaustiveness (better)
  • Data class @JvmRecord support
  • Inline class compile time %40 hızlı

Migration Issues

kotlin
1// K1'de sessizce çalışan ama K2'de hata
2class Foo {
3 val bar: String
4 get() = bar // K1: runtime StackOverflow, K2: compile error
5}

K2 daha strict — bu gibi edge case'leri compile time'da yakalıyor.


Context Parameters (Experimental)

Dependency injection'ı simplify eden yeni syntax:

kotlin
1// Old way — her fonksiyonda parametre passing
2fun Logger.processOrder(order: Order) {
3 this.log("Processing ${order.id}")
4 // ...
5}
6 
7// New context parameters (2.1 experimental)
8context(logger: Logger)
9fun processOrder(order: Order) {
10 logger.log("Processing ${order.id}")
11 // ...
12}
13 
14// Call side
15with(logger) {
16 processOrder(myOrder)
17}

Multiple Context

kotlin
1context(logger: Logger, transaction: Transaction)
2suspend fun chargeCustomer(amount: Money) {
3 logger.log("Charging ${amount.display}")
4 transaction.withCustomer { customer ->
5 customer.charge(amount)
6 }
7}

DI framework'lere (Koin, Hilt) alternative.


Guard Conditions in when

Pattern matching improvement:

kotlin
1when (x) {
2 is Int -> println("int")
3 is String if x.length > 0 -> println("non-empty string")
4 is String -> println("empty string")
5 is List<*> if x.all { it is Number } -> println("number list")
6 else -> println("other")
7}

Swift'in guard clauses'ına benzer — sealed class handling'de çok güçlü.


Non-local break/continue

Inline function içinde outer loop kontrolü:

kotlin
1inline fun processItems(items: List, action: (Item) -> Unit) {
2 items.forEach { action(it) }
3}
4 
5fun main() {
6 processItems(items) { item ->
7 if (item.invalid) break // 2.1: outer loop'u break eder
8 if (item.skip) continue // 2.1: supported
9 println(item)
10 }
11}

Önceden çalışmıyordu, şimdi idiomatic.


Multi-dollar String Interpolation

kotlin
1val dollar = "$100"
2val message = $$"Price: $dollar, tax: $tax"$$
3// String içinde $ escape gereksiz

Regex ve JSON template'leri için çok daha temiz.


KMP İyileştirmeleri

Kotlin Multiplatform 2.1'de major improvements:

Swift Export (Preview)

Kotlin kod'u Swift için native binding üretiyor — iOS team için daha iyi DX:

kotlin
1// Kotlin
2class UserRepository {
3 suspend fun fetchUser(id: String): User { ... }
4}
swift
1// Otomatik üretilen Swift wrapper
2class UserRepository {
3 func fetchUser(id: String) async throws -> User { ... }
4 // async/await natively, not callback-based
5}

Compose Multiplatform 1.7

iOS support stable. Tek codebase → Android + iOS + Desktop + Web.

Native Shared State

kotlinx.atomicfu ile thread-safe shared state KMP'de.


Gradle + Maven Compatibility

  • Gradle 8.10+: Kotlin 2.1 uyumlu
  • JVM target: 8, 11, 17, 21 (21 default recommendation)
  • Android AGP: 8.6+
  • KAPT: Deprecated (KSP2 migrate)

build.gradle.kts

kotlin
1plugins {
2 kotlin("jvm") version "2.1.0"
3 kotlin("plugin.serialization") version "2.1.0"
4 id("com.google.devtools.ksp") version "2.1.0-1.0.28"
5}
6 
7kotlin {
8 jvmToolchain(21)
9 compilerOptions {
10 freeCompilerArgs.add("-Xcontext-parameters") // Experimental
11 }
12}

Deprecations

  • KAPT: Deprecated, KSP2 migrate et
  • Old K1 compiler: Deprecated, removed in 2.2
  • Native memory manager legacy: Yeni memory manager zorunlu

Migration Guide

  1. Kotlin version: 2.0.x → 2.1.0
  2. Gradle: 8.8+ gerekir
  3. AGP: 8.6+
  4. KAPT → KSP2: Room, Hilt gibi annotation processor'lar
  5. Compose Compiler: 1.5.15+ (Compose 1.7 ile uyumlu)
  6. KSP2 migrate: KSP1 removed 2026 Q2
  7. Dependencies check: kotlinx.serialization, coroutines, etc.

ALTIN İPUCU

Bu yazının en değerli bilgisi

Bu ipucu, yazının en önemli çıkarımını içeriyor.

Easter Egg

Gizli bir bilgi buldun!

Bu bölümde gizli bir bilgi var. Keşfetmek ister misin?

kotlin
1// Guard conditions + context parameters
2context(logger: Logger)
3fun processValue(value: Any?) = when (value) {
4 null -> logger.warn("Null value")
5 is Int if value > 0 -> logger.info("Positive: $value")
6 is Int if value < 0 -> logger.warn("Negative: $value")
7 is String if value.isBlank() -> logger.warn("Blank string")
8 is String -> logger.info("String: $value")
9 is List<*> if value.isEmpty() -> logger.warn("Empty list")
10 is List<*> -> logger.info("List of ${value.size}")
11 else -> logger.error("Unknown type: ${value::class}")
12}

Okuyucu Ödülü

**External Resources:** - [Kotlin 2.1 release notes](https://kotlinlang.org/docs/whatsnew21.html) - [K2 compiler migration guide](https://kotlinlang.org/docs/k2-compiler-migration-guide.html) - [Context parameters proposal](https://github.com/Kotlin/KEEP/blob/master/proposals/context-parameters.md) - [KSP2 migration](https://kotlinlang.org/docs/ksp-migrate-from-kapt.html) - [KMP Swift Export](https://kotlinlang.org/docs/multiplatform-swift-export.html)

Sonuç

Kotlin 2.1 K2 compiler'ı cement etti — %2x hız artışı, daha iyi type inference, cleaner IDE experience. Context parameters DI'ı simplify ediyor. KMP 2.1'de iOS production-ready. Migration çoğunlukla smooth; KAPT → KSP2 ve annotation processor compatibility tek edge case. 2.2'de bekleniyor: stable context parameters, stable Swift export, Kotlin LSP 2.0.

*İlgili yazılar: Kotlin Coroutines Flow, Compose 1.7, Android 15.*

Etiketler

#Android#Kotlin#Kotlin 2.1#K2 Compiler#KMP#Language#2026
Muhittin Çamdalı

Muhittin Çamdalı

Senior iOS Developer

12+ yıllık deneyime sahip iOS Developer. Swift, SwiftUI ve modern iOS mimarileri konusunda uzman. Apple platformlarında performanslı ve kullanıcı dostu uygulamalar geliştiriyorum.

iOS Geliştirme Haberleri

Haftalık Swift tips, SwiftUI tricks ve iOS best practices. Spam yok, sadece değerli içerik.

Gizliliğinize saygı duyuyoruz. İstediğiniz zaman abonelikten çıkabilirsiniz.

Paylaş

Bunu da begenebilirsiniz