Replace Context Receivers with Context Parameters - #548
Conversation
Upgrades the project's build system: - Upgraded Android Gradle Plugin to 9.3.1 and Gradle Wrapper to 9.6.1. - Migrated all modules from 'kotlin.android' to AGP's built-in Kotlin support. - Replaced 'kotlin.kapt' with 'legacy-kapt' for compatibility with built-in Kotlin. - Upgraded Hilt to 2.59.2 for AGP 9 compatibility. - Updated compiler flags to pass -Xcontext-receivers under the new built-in Kotlin configuration block in 11 modules. - Updated README to reflect Android Studio Quail / AGP 9 requirements. - Enabled parallel Gradle sync in gradle.properties.
Deletes the unused and duplicated test helper classes in :ui:components:capture. Gradle 9 fails the build if a module contains test sources but no actual @test classes to execute, which was the case here since it only contained test utilities.
There was a problem hiding this comment.
Code Review
This pull request upgrades the project's build configuration, including updating the Android Gradle Plugin to 9.3.1, Gradle to 9.6.1, and Hilt to 2.59.2. It also migrates from Kotlin context receivers to context parameters (using -Xcontext-parameters and with(c) blocks) across several modules and updates the JVM target to 17. Feedback on these changes highlights a non-existent ktlintVersion (1.8.0) that will cause build failures, a detached comment in CameraSession.kt, poorly formatted closing braces in feature/postcapture/build.gradle.kts, and inconsistent indentation of the compilerOptions block across multiple build.gradle.kts files.
I am having trouble creating individual review comments. Click here to see my feedback.
gradle/init.gradle.kts (17)
Ktlint version 1.8.0 does not exist. The latest stable versions of Pinterest Ktlint are in the 1.3.x / 1.4.x / 1.5.x range. Specifying a non-existent version will cause the Spotless plugin / build to fail to resolve the dependency. Please verify and use a valid version (e.g., 1.3.1 or keep 1.2.1).
val ktlintVersion = "1.3.1"core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt (880-884)
The comment // 2 has become detached from ImageCapture.FLASH_MODE_OFF and is now floating on its own line, which is confusing. It should be kept on the same line as the constant or removed.
FlashMode.OFF -> ImageCapture.FLASH_MODE_OFF // 2
FlashMode.ON -> if (isScreenFlashRequired) {feature/postcapture/build.gradle.kts (78)
The closing braces }} are poorly formatted on a single line. They should be placed on separate lines to adhere to standard Kotlin DSL formatting guidelines.
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
core/camera/effects/single-stream/build.gradle.kts (36-42)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/components/capture/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/controller/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/controller/impl/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/uistate/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/uistate/capture/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/uistateadapter/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
ui/uistateadapter/capture/build.gradle.kts (49-55)
The indentation of the compilerOptions block is inconsistent, and there are unnecessary blank lines. It should be aligned with jvmToolchain (8 spaces of indentation) to adhere to standard Kotlin DSL style guidelines.
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.add("-Xcontext-parameters")
}
}References
- Check for adherence to official Kotlin style guides and Android best practices. (link)
Migrates deprecated context receivers to the new context parameters syntax: - Updated context receivers to the new Kotlin 2.2.0 context parameters syntax (context(c: CameraSessionContext)) in CameraSession and ConcurrentCameraSession, using the -Xcontext-parameters compiler flag. - Upgraded compiler flags from -Xcontext-receivers to -Xcontext-parameters in 11 modules. - Upgraded ktlint to 1.8.0 to support Kotlin 2.2.0 context parameters. # Conflicts: # core/camera/effects/single-stream/build.gradle.kts # ui/components/capture/build.gradle.kts # ui/controller/build.gradle.kts # ui/controller/impl/build.gradle.kts # ui/uistate/build.gradle.kts # ui/uistate/capture/build.gradle.kts # ui/uistateadapter/build.gradle.kts # ui/uistateadapter/capture/build.gradle.kts
cdf30b5 to
f74ec6d
Compare
…xt parameter flags
Upgrades the project's build system: - Upgraded Android Gradle Plugin to 9.3.1 and Gradle Wrapper to 9.6.1. - Migrated all modules from 'kotlin.android' to AGP's built-in Kotlin support. - Replaced 'kotlin.kapt' with 'legacy-kapt' for compatibility with built-in Kotlin. - Upgraded Hilt to 2.59.2 for AGP 9 compatibility. - Updated compiler flags to pass -Xcontext-receivers under the new built-in Kotlin configuration block in 11 modules. - Updated README to reflect Android Studio Quail / AGP 9 requirements. - Enabled parallel Gradle sync in gradle.properties.
Deletes the unused and duplicated test helper classes in :ui:components:capture. Gradle 9 fails the build if a module contains test sources but no actual @test classes to execute, which was the case here since it only contained test utilities.
….1' into kim/dependency/deprecate-context-receivers
…ate-context-receivers
….1' into kim/dependency/deprecate-context-receivers
|
|
||
| context(CameraSessionContext) | ||
| @ExperimentalCamera2Interop | ||
| context(c: CameraSessionContext) |
There was a problem hiding this comment.
Could you find out how the new context parameter should be handled on other platforms we mirror our code on?
TLDR: Upgrades kotlin version to 2.4 with the following implications:
ktlint is also upgraded to 1.8.0 😋
The changes to
CameraSession/ConcurrentCameraSessionContext Receiversused in these files ~ surprise surprise ~ were deprecated in favor of the new ✨ Context Parameters ✨ as of Kotlin2.2.0! (Read official docs here)!Alternative Considered:
I considered reverting to the Extension Functions pattern for the following reasons:
CameraSessionContext). For these, standard Kotlin extension functions (CameraSessionContext.foo()) work perfectly fine, and are 100% stable since Kotlin 1.0.Preview.Builder.updateCameraStateWithCaptureResults) would need one of the receivers to be explicitly passed as a regular parameter.But TBH since Context Parameters is stable in
2.4.0, i think it is OK to keep this PR as is! 🆗