0% found this document useful (0 votes)
4 views6 pages

Hilt Custom Component in Android App

The document describes an Android application using Hilt for dependency injection, featuring a MainActivity that injects a MyRepository instance to retrieve data. It includes definitions for MyRepository, MyRepositoryImpl, MyService, and a custom Dagger component with a module for providing dependencies. The application structure demonstrates how to set up Hilt and manage dependencies effectively within an Android app.

Uploaded by

Prem Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Hilt Custom Component in Android App

The document describes an Android application using Hilt for dependency injection, featuring a MainActivity that injects a MyRepository instance to retrieve data. It includes definitions for MyRepository, MyRepositoryImpl, MyService, and a custom Dagger component with a module for providing dependencies. The application structure demonstrates how to set up Hilt and manage dependencies effectively within an Android app.

Uploaded by

Prem Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// MainActivity.

kt
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import
[Link]
import [Link]
import [Link]

// Annotate MainActivity with @AndroidEntryPoint to enable Hilt


injection
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

// Inject MyRepository using the custom component


@Inject
lateinit var myRepository: MyRepository

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)

// Example usage: Call a method from the injected repository


val data = [Link]()
println("Data from MyRepository: $data")

setContent {
HiltCustomComponentTheme {
Surface(
modifier = [Link](),
color = [Link]
) {
Greeting(data)
}
}
}
}
}

@Composable
fun Greeting(data: String, modifier: Modifier = Modifier) {
Column(
modifier = [Link](),
verticalArrangement = [Link],
horizontalAlignment = [Link]
) {
Text(
text = "Hello from Hilt Custom Component!",
style = [Link]
)
Text(
text = "Data from Repository: $data",
style = [Link]
)
}
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
HiltCustomComponentTheme {
Greeting("Sample Data")
}
}

// [Link]
package [Link]

import [Link]
import [Link]

// Define a simple repository interface


interface MyRepository {
fun getData(): String
}

// Implement the repository


@Singleton // This annotation means there will be only one instance of
MyRepositoryImpl
class MyRepositoryImpl @Inject constructor(
private val myService: MyService // MyService will be provided by
our custom component
) : MyRepository {
override fun getData(): String {
return "Repository data: ${[Link]()}"
}
}

// [Link]
package [Link]

// Define a simple service


class MyService @Inject constructor() {
fun doSomething(): String {
return "Service did something!"
}
}

// [Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

// 1. Define the Custom Component Interface


// @Singleton ensures that the component lives as long as the
application
// @Component specifies that this is a Dagger component
// modules specifies which modules provide dependencies to this
component
@Singleton
@Component(modules = [CustomModule::class])
interface CustomComponent {
// This method is used for field injection into classes not
directly supported by Hilt
// (e.g., if you needed to inject into a plain Kotlin class that
isn't an Android component).
// For Android components like Activity, Hilt handles this
automatically via @AndroidEntryPoint.
// However, it's good practice to define it if you plan to manually
inject.
fun inject(mainActivity:
[Link])

// Expose dependencies that other components or classes might need


fun myRepository(): MyRepository
}

// 2. Define the Module for the Custom Component


// @Module indicates this is a Dagger module
// @InstallIn(SingletonComponent::class) tells Hilt to install this
module into the SingletonComponent
// This allows dependencies provided by this module to be available in
the application's singleton scope.
@Module
@InstallIn(SingletonComponent::class) // This module will be part of
Hilt's SingletonComponent
object CustomModule {

// Provide MyService as a singleton


@Provides
@Singleton
fun provideMyService(): MyService {
return MyService()
}

// Provide MyRepositoryImpl as a singleton and bind it to


MyRepository interface
@Provides
@Singleton
fun provideMyRepository(myService: MyService): MyRepository {
return MyRepositoryImpl(myService)
}
}

// [Link]
package [Link]

import [Link]
import [Link]

// 3. Application Class to Initialize Hilt and Custom Component


(Optional, Hilt does most of it)
// @HiltAndroidApp triggers Hilt's code generation for the application
@HiltAndroidApp
class CustomApplication : Application() {
// You typically don't need to manually create the CustomComponent
here
// as Hilt integrates it. However, if you had a truly standalone
Dagger component
// not managed by Hilt's default setup, you might do something like
this:
// lateinit var customComponent: CustomComponent
// override fun onCreate() {
// [Link]()
// customComponent = [Link]()
// }
}

// This class is not strictly necessary for the basic Hilt setup,
// but demonstrates how you *would* manually manage a component
// if you weren't relying entirely on Hilt's automatic injection.
// For the provided example, Hilt handles the injection of MyRepository
// into MainActivity because MainActivity is annotated with
@AndroidEntryPoint
// and MyRepository is provided via a module installed in
SingletonComponent.
object CustomComponentManager {
// This would typically be initialized once in your Application
class
// if you were managing the component manually without Hilt's full
integration.
// For this Hilt example, Hilt takes care of component creation.
// private lateinit var customComponent: CustomComponent
// fun initialize(application: Application) {
// customComponent = [Link]()
// .build()
// }

// fun getCustomComponent(): CustomComponent {


// if (!::[Link]) {
// throw IllegalStateException("CustomComponent not
initialized. Call initialize() first.")
// }
// return customComponent
// }
}

You might also like