HiltAnnotationExtension is a Kotlin library that simplifies the creation of Hilt modules and entry points using annotations and code generation.
- Java 17: This library requires Java 17 or higher.
- Kotlin: Compatible with Kotlin 1.9.0 or higher.
- Gradle: Recommended version 8.7 or higher.
Add the following dependencies to your project’s build.gradle.kts
:
dependencies {
implementation("com.github.PanyaNaknoppakun:HiltAnnotationExtension:1.0.6")
kapt("com.github.PanyaNaknoppakun:HiltAnnotationExtension:1.0.6")
}
This annotation generates a Hilt module that binds the annotated class to its interface.
package com.example
import com.pls.annotation.GenerateBindHiltModule
import javax.inject.Inject
interface MyRepository {
fun doSomething()
}
@GenerateBindHiltModule
class MyRepositoryImpl @Inject constructor() : MyRepository {
override fun doSomething() {
// Implementation here
}
}
This will generate the following module:
@Module
@InstallIn(SingletonComponent::class)
abstract class MyRepositoryHiltModule {
@Binds
abstract fun bindMyRepository(myRepositoryImpl: MyRepositoryImpl): MyRepository
}
This annotation generates a Hilt module that provides an instance of the annotated class.
package com.example
import com.pls.annotation.GenerateProvidesHiltModule
@GenerateProvidesHiltModule(installIn = ActivityRetainedComponent::class)
object MyRepositoryProvider
This will generate the following module:
@Module
@InstallIn(ActivityRetainedComponent::class)
object MyRepositoryProvider_HiltModule {
@Provides
fun provideMyRepository(): MyRepository {
return MyRepository()
}
}
This annotation generates a Hilt entry point for the annotated class.
package com.example
import com.pls.annotation.GenerateEntryPointHiltModule
@GenerateEntryPointHiltModule
class NoAndroid {
// Implementation here
}
This will generate the following entry point:
@EntryPoint
@InstallIn(SingletonComponent::class)
interface NonAndroid_HiltEntryPoint {
fun inject(noAndroid: NoAndroid)
}
- Annotate Your Classes: Use @GenerateBindHiltModule, @GenerateEntryPointHiltModule, and @GenerateProvidesHiltModule on your classes as shown in the examples above.
- Build Your Project: The annotation processor will generate the necessary Hilt modules and entry points.
- Use the Generated Code: You can now use the generated modules and entry points in your application.
This project is licensed under the MIT License. See the LICENSE file for details.