1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hilt の kapt を ksp にする

Posted at

はじめに

kapt 、ksp って何? みたいな話は他者様の記事に任せるとして、本記事には Hilt + kapt の環境を Hilt + ksp に移行した作業のみを簡潔に記載する。

移行の方針

Hilt + ksp にする方針は kapt から KSP に移行するDagger KSP の Requirements に基づく。

Dagger 2.48 (or above)
Kotlin 1.9.0 (or above)
KSP 1.9.0-1.0.12 (or above)

環境

AGP(Android Gradle plugin) は 8.4.1

gradle/wrapper/gradle-wrapper.properties
plugins {
    id 'com.android.application' version '8.4.1' apply false
    id 'com.android.library' version '8.4.1' apply false
 (省略)    
app/build.gradle
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

build.gradle

 plugins {
     id 'com.android.application' version '8.4.1' apply false
     id 'com.android.library' version '8.4.1' apply false
-    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
-    id 'com.google.dagger.hilt.android' version '2.44' apply false
+    id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
+    id 'com.google.dagger.hilt.android' version '2.48.1' apply false
+    id 'com.google.devtools.ksp' version '1.9.0-1.0.12' apply false
 }
(省略)

app/build.gradle

plugins {
     id 'com.android.application'
     id 'org.jetbrains.kotlin.android'
     id 'androidx.navigation.safeargs'
-    id 'kotlin-kapt'
     id 'com.google.dagger.hilt.android'
+    id 'com.google.devtools.ksp'
 }

 (省略)
      compileOptions {
-        sourceCompatibility JavaVersion.VERSION_1_8
-        targetCompatibility JavaVersion.VERSION_1_8
+        sourceCompatibility = JavaVersion.VERSION_17
+        targetCompatibility = JavaVersion.VERSION_17
     }
     kotlinOptions {
-        jvmTarget = '1.8'
+        jvmTarget = "17"
     }
     viewBinding {
         enabled = true
     }
-    kapt {
-        correctErrorTypes true
-    }
 }
 (省略)
      // Hilt
-    def hilt_version = "2.44"
+    def hilt_version = "2.48.1"
     implementation "com.google.dagger:hilt-android:$hilt_version"
-    kapt "com.google.dagger:hilt-compiler:$hilt_version"
+    ksp "com.google.dagger:hilt-compiler:$hilt_version"
 (省略) 

sourceCompatibilitytargetCompatibilityJavaVersion.VERSION_17 にしない場合は以下のビルドエラーが発生する。

Execution failed for task ':app:kspDebugAndroidTestKotlin'.
> 'compileDebugAndroidTestJavaWithJavac' task (current target is 1.8) and 'kspDebugAndroidTestKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

jvmTarget17 にしない場合も以下のビルドエラーが発生する。

Execution failed for task ':app:kspDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 17) and 'kspDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

以上で移行完了。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?