I often adopt new technologies in my hobby or pet projects to keep up with the latest practices, as experimenting with newer technologies in professional settings can be challenging. Recently, I transitioned to the K2 compiler and moved from a full XML-based UI to Jetpack Compose. Additionally, I migrated from KAPT to KSP, making my project fully Kotlin-based. Here are some insights from these migrations:
- UI Migration: Transitioning from XML to Jetpack Compose was enjoyable and less challenging.
- Navigation Challenges: Jetpack Compose's navigation is still maturing. It currently lacks some features, especially for complex data passing. However, a new Kotlin serialization-based navigation library in beta shows promise.
- Development Speed: Building UIs with Compose generally takes less time.
- Code Reusability: Writing decoupled and reusable code is easier with Jetpack Compose. However, as the UI complexity grows, maintaining IDE previewability becomes challenging.
- Build Time Improvement: Fully migrating to Jetpack Compose allowed me to remove data binding and view binding, reducing build times from an average of 2 minutes to 1 minute 30 seconds.
- Lifecycle Library Update: The latest Android lifecycle library (2.8.0) breaks the current flow conversion to Compose's collectAsStateWithLifecycle(). This is fixed in Compose 1.7 beta. If you're below version 1.7 and using lifecycle library 2.8.0, be prepared for some challenges.
- Compose Compiler Compatibility: Specific Compose compilers are compatible with specific Kotlin versions, which can be a headache. Fortunately, the K2 compiler addresses this issue, simplifying the process.
- Library Support: Major Android libraries now support KSP, though updating these may introduce new behavioral changes.
- Data Binding: Data binding still doesn't support KSP. However, with my full migration to Compose, this wasn’t an issue.
- Build Time Reduction: Switching to KSP reduced my build time from 1 minute 30 seconds to 1 minute.
- Setup: With Gradle version 8.2.2, setting up the K2 compiler was straightforward by adding two flags to my gradle.properties.
- Compatibility: Jetpack Compose compiler 1.5.0, compatible with Kotlin 1.9, also works seamlessly with the K2 compiler.
- Codebase Stability: No additional changes were needed in my codebase to run and test with the K2 compiler.
- Build Time: For my pure Kotlin project, build times with the K2 compiler were reduced significantly, from above 1 minute to below 30 seconds.