Skip to content

Commit 1adef3d

Browse files
Chang-EricDagger Team
authored andcommitted
Update docs on gradle setup to include KSP.
Fixes #5076. RELNOTES=n/a PiperOrigin-RevId: 856854766
1 parent 88c8766 commit 1adef3d

1 file changed

Lines changed: 63 additions & 6 deletions

File tree

hilt/gradle-setup.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies {
1414
annotationProcessor 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
1515
1616
// For instrumentation tests
17-
androidTestImplementation 'com.google.dagger:hilt-android-testing:{{site.daggerVersion}}'
17+
androidTestImplementation 'com.google.dagger:hilt-android-testing:{{site.daggerVersion}}'
1818
androidTestAnnotationProcessor 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
1919
2020
// For local unit tests
@@ -25,21 +25,77 @@ dependencies {
2525

2626
## Using Hilt with Kotlin
2727

28-
If using Kotlin, then apply the
29-
[kapt plugin](https://kotlinlang.org/docs/reference/kapt.html) and declare the
30-
compiler dependency using `kapt` instead of `annotationProcessor`.
28+
If using Kotlin, you have the choice of using either
29+
[KSP](https://kotlinlang.org/docs/ksp-overview.html) or
30+
[kapt](https://kotlinlang.org/docs/kapt.html).
3131

32-
Additionally configure kapt to correct error types by setting
32+
If using KSP, then apply the
33+
[KSP plugin](https://kotlinlang.org/docs/ksp-quickstart.html) in your root
34+
`build.gradle` file.
35+
36+
Otherwise if using `kapt`, then apply the
37+
[kapt plugin](https://kotlinlang.org/docs/reference/kapt.html) in your root
38+
`build.gradle` file.
39+
40+
<!-- Even though this is switching between ksp and kapt, reuse the code selector
41+
as Java/Kotlin because otherwise nothing will be selected when the code
42+
selection is Java or Kotlin -->
43+
<div class="c-codeselector__button c-codeselector__button_java">KSP</div>
44+
<div class="c-codeselector__button c-codeselector__button_kotlin">kapt</div>
45+
```groovy
46+
plugins {
47+
// Choose an appropriate version to replace the X.X.X
48+
id 'com.google.devtools.ksp' version 'X.X.X' apply false
49+
}
50+
```
51+
{: .c-codeselector__code .c-codeselector__code_java }
52+
```groovy
53+
plugins {
54+
// Choose an appropriate version to replace the X.X.X
55+
id 'org.jetbrains.kotlin.kapt' version 'X.X.X' apply false
56+
}
57+
```
58+
{: .c-codeselector__code .c-codeselector__code_kotlin }
59+
60+
Then declare the Hilt compiler dependency using `ksp` or `kapt` respectively
61+
instead of `annotationProcessor`.
62+
63+
Additionally if using `kapt`, configure it to correct error types by setting
3364
[`correctErrorTypes`](https://kotlinlang.org/docs/reference/kapt.html#non-existent-type-correction)
3465
to true.
3566

67+
<div class="c-codeselector__button c-codeselector__button_java">KSP</div>
68+
<div class="c-codeselector__button c-codeselector__button_kotlin">kapt</div>
69+
```groovy
70+
plugins {
71+
id 'com.google.devtools.ksp'
72+
}
73+
74+
dependencies {
75+
implementation 'com.google.dagger:hilt-android:{{site.daggerVersion}}'
76+
ksp 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
77+
78+
// For instrumentation tests
79+
androidTestImplementation 'com.google.dagger:hilt-android-testing:{{site.daggerVersion}}'
80+
kspAndroidTest 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
81+
82+
// For local unit tests
83+
testImplementation 'com.google.dagger:hilt-android-testing:{{site.daggerVersion}}'
84+
kspTest 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
85+
}
86+
```
87+
{: .c-codeselector__code .c-codeselector__code_java }
3688
```groovy
89+
plugins {
90+
id 'org.jetbrains.kotlin.kapt'
91+
}
92+
3793
dependencies {
3894
implementation 'com.google.dagger:hilt-android:{{site.daggerVersion}}'
3995
kapt 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
4096
4197
// For instrumentation tests
42-
androidTestImplementation 'com.google.dagger:hilt-android-testing:{{site.daggerVersion}}'
98+
androidTestImplementation 'com.google.dagger:hilt-android-testing:{{site.daggerVersion}}'
4399
kaptAndroidTest 'com.google.dagger:hilt-compiler:{{site.daggerVersion}}'
44100
45101
// For local unit tests
@@ -51,6 +107,7 @@ kapt {
51107
correctErrorTypes true
52108
}
53109
```
110+
{: .c-codeselector__code .c-codeselector__code_kotlin }
54111

55112
## Hilt Gradle plugin {#hilt-gradle-plugin}
56113

0 commit comments

Comments
 (0)