kc_dev
kc_dev

Reputation: 789

Can you use Hilt in a Java-only Android project?

According to the official integration guide, you need to add

plugins {
  id 'kotlin-kapt'
  ...
}

and

dependencies {
    implementation "com.google.dagger:hilt-android:{hilt_version}"
    kapt "com.google.dagger:hilt-compiler:{hilt_version}"
}

to your build.grade file. However, when I do a gralde sync, I get the following error:

Plugin [id: 'kotlin-kapt'] was not found in any of the following sources:

is it possible to use Dagger-Hilt in a pure java project? Or do you have to either use plain Dagger or use Kotlin?

Upvotes: 7

Views: 4462

Answers (2)

Zeeshan Ali
Zeeshan Ali

Reputation: 777

Yes, you can use Hilt in a Java-only Android project.

Step 1

Add hilt plugin in module level.

plugins {
    id 'com.google.dagger.hilt.android' version '2.44' apply false
}

Step 2

Add hilt plugin in app level.

plugins {
   id 'com.android.application'
   id 'com.google.dagger.hilt.android'
}

Step 3

Add the following dependencies and make sure to use annotationProcessor instead of kapt. As kapt is for kotline based projects.

dependencies {
    implementation "com.google.dagger:hilt-android:2.44"
    annotationProcessor "com.google.dagger:hilt-compiler:2.44"
}

And now finally you are able to use hilt in you java based android application.

enter image description here

Upvotes: 6

Ocasin
Ocasin

Reputation: 231

Yes, you can use the following instead of kotlin-kapt:

dependencies {
    implementation "com.google.dagger:hilt-android:{hilt_version}"
    annotationProcessor 'com.google.dagger:hilt-compiler:{hilt_version}'
}


plugins {
    id 'dagger.hilt.android.plugin'
}

Upvotes: 11

Related Questions