🎉 코틀린 2.0

코틀린 2.0 이 나오면서 전반적으로 더 안정화되고
더 똑똑해졌는데요. (Type checks and casts)

많은 업데이트 중에 안드로이드 개발하는 입장에서
Explicit Backing Field를 사용하고 싶어서
마이그레이션을 진행하게 됐습니다.

안드로이드에서는 ViewModel 에서 상태를 두 변수를 생성하는데요.

  • 안에서 상태를 처리하는 Mutable 객체
    private val _city: MutableStateFlow<String> = MutableStateFlow<String>("")
  • 뷰에서 상태를 바라보는 Immutable 객체
    val city: StateFlow<String> get() = _state.asStateFlow()

상태를 어떻게 묶는지에 따라 다르지만
해당 변수가 ViewModel에서 꽤 많이 생길 수 있습니다.

이를 코틀린 2.0의 Explicit Backing Field 을 이용하면
하나의 변수로 관리할 수 있습니다.

val city: StateFlow<String>
        field = MutableStateFlow("")

추가로 아직 gradle.build.kts 를 사용하는 것이 아닌
gradle.build 를 사용할 때 마이그레이션을 어떻게 했는지
공유해보겠습니다.

🔮 Migration

.gitIgnore

.kotlin/

build.gradle(:root)

  • 코틀린 버전 업데이트
  • 코틀린 그래들 플러그인 업데이트
  • 컴포즈 컴파일러 플러그인 (구글에서 > 젯브레인)
  • ksp 버전 업데이트
buildscript {
  ext {
  	 kotlinVersion = '2.0.0'
  }
  
  repositories {
        google()
        mavenCentral()
  }

  dependencies {
        ...
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        ...
  }
  
}

plugins {
    id 'com.google.devtools.ksp' version '2.0.0-1.0.22' apply false
    id 'org.jetbrains.kotlin.plugin.compose' version "$kotlinVersion" apply false
}

...

build.gradle(:module)

  • Compose Compiler Plugin 명시
  • 바뀐 문법 AS-IS, TO-BE
	// AS-IS
	//kotlinOptions {
    //   jvmTarget = "17"
    //}
    
    // 🌟 TO-BE
    kotlin {
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_17)
        }
    }
	// AS-IS
    //composeOptions {
    //    kotlinCompilerExtensionVersion = rootProject.composeCompilerVersion
    //}
    
    // 🌟 TO-BE
    composeCompiler {
        enableStrongSkippingMode = true
        includeSourceInformation = true
    }

적용한 build.gradle 스크립트

plugins {
	...
    id "io.sentry.kotlin.compiler.gradle" version "4.7.0"
    id 'org.jetbrains.kotlin.plugin.compose'
}

android {
    ...

   
    kotlin {
        compilerOptions {
            jvmTarget.set(JvmTarget.JVM_17)
        }
    }
        
    composeCompiler {
        enableStrongSkippingMode = true
        includeSourceInformation = true
    }
    
    ...
}

dependencies {
    ....
}

👋🏻 포스트를 마치며

코틀린 2.0 마이그레이션을 위와같이 마치고
들뜬 마음으로 바로 리팩토링을 진행할려고 하였으나 ...

아직 서포팅이 되지 않는다고 하네요.
위와 관련해서 찾아보니 compiler 옵션을 설정해봤는데
release로 빌드 시 오류가 나면서
production에서 사용하지 말라는 메세지가 나와서
아직 사용하지는 못 하겠네요 🥲

이 때, 아직 저희 회사는 kts를 사용하지 않아서
우리회사와 비슷한 상황의 환경에서 도움이 되지 않을까 싶어서
공유해보았는데요.

다음 블로그 포스팅은 위와 관련이 있는데요
Version Catalog 와 CustomConventionPlugin 을 이용한
MultiModule 의 build.gradle.kts 마이그레이션을 공유해보겠습니다.

profile
공부하는 개발자

0개의 댓글

Powered by GraphCDN, the GraphQL CDN