Skip to content

Replace Context Receivers with Context Parameters - #548

Open
Kimblebee wants to merge 13 commits into
kim/dependency/update-gradle-9.3.1from
kim/dependency/deprecate-context-receivers
Open

Replace Context Receivers with Context Parameters#548
Kimblebee wants to merge 13 commits into
kim/dependency/update-gradle-9.3.1from
kim/dependency/deprecate-context-receivers

Conversation

@Kimblebee

@Kimblebee Kimblebee commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

TLDR: Upgrades kotlin version to 2.4 with the following implications:

  • deprecates Context Receivers in favor of Context Parameters
  • upgrades Hilt version to 2.60.1 to resolve compatibility issues
  • removes all experimental context receiver flags (kotlin 2.2 required experimental flags for context parameters)

ktlint is also upgraded to 1.8.0 😋

The changes to CameraSession / ConcurrentCameraSession

  • During this dependency update, previous experimental Context Receivers used in these files ~ surprise surprise ~ were deprecated in favor of the new ✨ Context Parameters ✨ as of Kotlin 2.2.0! (Read official docs here)!

Alternative Considered:

I considered reverting to the Extension Functions pattern for the following reasons:

  • Current standard
  • For 11 out of the 12 refactored functions, they only required one receiver (CameraSessionContext). For these, standard Kotlin extension functions (CameraSessionContext.foo()) work perfectly fine, and are 100% stable since Kotlin 1.0.
    • caveat to this is that the one (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! 🆗

butterfly hatching from chrysalis

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.
@Kimblebee
Kimblebee changed the base branch from main to kim/dependency/update-gradle-9.3.1 July 31, 2026 15:20

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

high

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)

medium

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)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

core/camera/effects/single-stream/build.gradle.kts (36-42)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/components/capture/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/controller/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/controller/impl/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/uistate/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/uistate/capture/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/uistateadapter/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

ui/uistateadapter/capture/build.gradle.kts (49-55)

medium

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
  1. Check for adherence to official Kotlin style guides and Android best practices. (link)

@Kimblebee Kimblebee mentioned this pull request Jul 31, 2026
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
@Kimblebee
Kimblebee force-pushed the kim/dependency/deprecate-context-receivers branch from cdf30b5 to f74ec6d Compare July 31, 2026 15:42
@Kimblebee
Kimblebee marked this pull request as ready for review July 31, 2026 15:54
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.

context(CameraSessionContext)
@ExperimentalCamera2Interop
context(c: CameraSessionContext)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you find out how the new context parameter should be handled on other platforms we mirror our code on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants