Hilt 코드를 생성하려면 Hilt를 사용하는 모든 Gradle 모듈에 액세스해야 합니다. Application 클래스를 컴파일하는 Gradle 모듈에는 전이 종속 항목에 모든 Hilt 모듈과 생성자 삽입 클래스가 있어야 합니다.
다중 모듈 프로젝트가 일반 Gradle 모듈로 구성되었다면 Hilt를 사용한 종속 항목 삽입에 설명된 대로 Hilt를 사용할 수 있습니다. 그러나 기능 모듈이 포함된 앱에는 적용되지 않습니다.
기능 모듈의 Hilt
기능 모듈에서는 일반적으로 모듈이 서로 종속되는 방식이 반전됩니다.
따라서 Hilt는 기능 모듈에서 주석을 처리할 수 없습니다. 기능 모듈에서 종속 항목 삽입을 실행하려면 Dagger를 사용해야 합니다.
기능 모듈과 관련된 이 문제를 해결하려면 구성요소 종속 항목을 사용해야 합니다. 다음 단계를 따르세요.
기능 모듈에 필요한 종속 항목이 있는 app 모듈(또는 Hilt가 처리할 수 있는 다른 모듈)에서 @EntryPoint 인터페이스를 선언합니다.
@EntryPoint 인터페이스에 종속된 Dagger 구성요소를 만듭니다.
기능 모듈에서 평소와 같이 Dagger를 사용합니다.
Hilt를 사용한 종속 항목 삽입 페이지의 예를 생각해 보세요. login 기능 모듈을 프로젝트에 추가한다고 가정해 보겠습니다. LoginActivity라는 활동으로 로그인 기능을 구현합니다. 즉, 애플리케이션 구성요소에서만 결합을 가져올 수 있습니다.
이 기능을 사용하려면 authInterceptor 결합이 있는 OkHttpClient가 필요합니다.
먼저 다음과 같이 login 모듈에 필요한 결합이 있는 SingletonComponent에 설치된 @EntryPoint 인터페이스를 만듭니다.
Kotlin
// LoginModuleDependencies.kt - File in the app module.@EntryPoint@InstallIn(SingletonComponent::class)interfaceLoginModuleDependencies{@AuthInterceptorOkHttpClientfunokHttpClient():OkHttpClient}
자바
// LoginModuleDependencies.java - File in the app module.@EntryPoint@InstallIn(SingletonComponent.class)publicinterfaceLoginModuleDependencies{@AuthInterceptorOkHttpClientOkHttpClientokHttpClient();}
LoginActivity에서 필드 삽입을 실행하려면 다음과 같이 @EntryPoint 인터페이스에 종속된 Dagger 구성요소를 만듭니다.
Kotlin
// LoginComponent.kt - File in the login module.@Component(dependencies=[LoginModuleDependencies::class])interfaceLoginComponent{funinject(activity:LoginActivity)@Component.BuilderinterfaceBuilder{funcontext(@BindsInstancecontext:Context):BuilderfunappDependencies(loginModuleDependencies:LoginModuleDependencies):Builderfunbuild():LoginComponent}}
자바
// LoginComponent.java - File in the login module.@Component(dependencies=LoginModuleDependencies.class)publicinterfaceLoginComponent{voidinject(LoginActivityloginActivity);@Component.BuilderinterfaceBuilder{Buildercontext(@BindsInstanceContextcontext);BuilderappDependencies(LoginModuleDependenciesloginModuleDependencies);LoginComponentbuild();}}
이러한 단계가 완료되면 기능 모듈에서 평소와 같이 Dagger를 사용하세요. 예를 들어 다음과 같이 SingletonComponent의 결합을 클래스의 종속 항목으로 사용할 수 있습니다.
Kotlin
// LoginAnalyticsAdapter.kt - File in the login module.classLoginAnalyticsAdapter@Injectconstructor(@AuthInterceptorOkHttpClientokHttpClient:OkHttpClient){...}
자바
// LoginAnalyticsAdapter.java - File in the login module.publicclassLoginAnalyticsAdapter{privatefinalOkHttpClientokHttpClient;@InjectLoginAnalyticsAdapter(@AuthInterceptorOkHttpClientOkHttpClientokHttpClient){this.okHttpClient=okHttpClient;}...}
필드 삽입을 실행하려면 다음과 같이 applicationContext를 사용하여 SingletonComponent 종속 항목을 가져오는 Dagger 구성요소의 인스턴스를 만듭니다.
Kotlin
// LoginActivity.kt - File in the login module.classLoginActivity:AppCompatActivity(){@InjectlateinitvarloginAnalyticsAdapter:LoginAnalyticsAdapteroverridefunonCreate(savedInstanceState:Bundle?){DaggerLoginComponent.builder().context(this).appDependencies(EntryPointAccessors.fromApplication(applicationContext,LoginModuleDependencies::class.java)).build().inject(this)super.onCreate(savedInstanceState)...}}
자바
// LoginActivity.java - File in the login module.publicclassLoginActivityextendsAppCompatActivity{@InjectLoginAnalyticsAdapterloginAnalyticsAdapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){DaggerLoginComponent.builder().context(this).appDependencies(EntryPointAccessors.fromApplication(getApplicationContext(),LoginModuleDependencies.class)).build().inject(this);super.onCreate(savedInstanceState);...}}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Hilt in multi-module apps\n\nHilt code generation needs access to all the Gradle modules that use Hilt. The\nGradle module that compiles your\n[`Application`](/reference/android/app/Application) class needs to have all Hilt\nmodules and constructor-injected classes in its transitive dependencies.\n\nIf your multi-module project is composed of regular Gradle modules, then you can\nuse Hilt as described in [Dependency injection with\nHilt](/training/dependency-injection/hilt-android). However, this is not the\ncase with apps that include [feature\nmodules](/guide/app-bundle/dynamic-delivery#customize_delivery).\n| **Note:** For deep multi-module projects, consider enabling the `enableExperimentalClasspathAggregation` flag in your `build.gradle` file. Read more about it in the [Hilt documentation](https://dagger.dev/hilt/gradle-setup#classpath-aggregation).\n\nHilt in feature modules\n-----------------------\n\nIn feature modules, the way that modules usually depend on each other is inverted.\nTherefore, Hilt cannot process annotations in feature modules. You must\nuse [Dagger](/training/dependency-injection/dagger-basics) to perform\ndependency injection in your feature modules.\n\nYou must use component dependencies to solve this problem with feature modules. Follow\nthese steps:\n\n1. Declare an [`@EntryPoint` interface](/training/dependency-injection/hilt-android#not-supported) in the `app` module (or in any other module that can be processed by Hilt) with the dependencies that the feature module needs.\n2. Create a Dagger component that depends on the `@EntryPoint` interface.\n3. Use Dagger as usual in the feature module.\n\nConsider the example from the [Dependency injection with\nHilt](/training/dependency-injection/hilt-android) page. Suppose you add a\n`login` feature module to your project. You implement the login feature\nwith an activity called `LoginActivity`. This means that you can get bindings\nonly from the application component.\n\nFor this feature, you need an `OkHttpClient` with the `authInterceptor` binding.\n\nFirst, create an `@EntryPoint` interface installed in the `SingletonComponent`\nwith the bindings that the `login` module needs: \n\n### Kotlin\n\n```kotlin\n// LoginModuleDependencies.kt - File in the app module.\n\n@EntryPoint\n@InstallIn(SingletonComponent::class)\ninterface LoginModuleDependencies {\n\n @AuthInterceptorOkHttpClient\n fun okHttpClient(): OkHttpClient\n}\n```\n\n### Java\n\n```java\n// LoginModuleDependencies.java - File in the app module.\n\n@EntryPoint\n@InstallIn(SingletonComponent.class)\npublic interface LoginModuleDependencies {\n\n @AuthInterceptorOkHttpClient\n OkHttpClient okHttpClient();\n}\n```\n\nTo perform field injection in the `LoginActivity`, create a Dagger\ncomponent that depends on the `@EntryPoint` interface: \n\n### Kotlin\n\n```kotlin\n// LoginComponent.kt - File in the login module.\n\n@Component(dependencies = [LoginModuleDependencies::class])\ninterface LoginComponent {\n\n fun inject(activity: LoginActivity)\n\n @Component.Builder\n interface Builder {\n fun context(@BindsInstance context: Context): Builder\n fun appDependencies(loginModuleDependencies: LoginModuleDependencies): Builder\n fun build(): LoginComponent\n }\n}\n```\n\n### Java\n\n```java\n// LoginComponent.java - File in the login module.\n\n@Component(dependencies = LoginModuleDependencies.class)\npublic interface LoginComponent {\n\n void inject(LoginActivity loginActivity);\n\n @Component.Builder\n interface Builder {\n Builder context(@BindsInstance Context context);\n Builder appDependencies(LoginModuleDependencies loginModuleDependencies);\n LoginComponent build();\n }\n}\n```\n\nOnce those steps are complete, use Dagger as usual in your feature module. For\nexample, you can use the bindings from the `SingletonComponent` as a\ndependency of a class: \n\n### Kotlin\n\n```kotlin\n// LoginAnalyticsAdapter.kt - File in the login module.\n\nclass LoginAnalyticsAdapter @Inject constructor(\n @AuthInterceptorOkHttpClient okHttpClient: OkHttpClient\n) { ... }\n```\n\n### Java\n\n```java\n// LoginAnalyticsAdapter.java - File in the login module.\n\npublic class LoginAnalyticsAdapter {\n\n private final OkHttpClient okHttpClient;\n\n @Inject\n LoginAnalyticsAdapter(\n @AuthInterceptorOkHttpClient OkHttpClient okHttpClient\n ) {\n this.okHttpClient = okHttpClient;\n }\n ...\n}\n```\n\nTo perform field injection, create an instance of the Dagger component\nusing the `applicationContext` to get the `SingletonComponent` dependencies: \n\n### Kotlin\n\n```kotlin\n// LoginActivity.kt - File in the login module.\n\nclass LoginActivity : AppCompatActivity() {\n\n @Inject\n lateinit var loginAnalyticsAdapter: LoginAnalyticsAdapter\n\n override fun onCreate(savedInstanceState: Bundle?) {\n DaggerLoginComponent.builder()\n .context(this)\n .appDependencies(\n EntryPointAccessors.fromApplication(\n applicationContext,\n LoginModuleDependencies::class.java\n )\n )\n .build()\n .inject(this)\n\n super.onCreate(savedInstanceState)\n ...\n }\n}\n```\n\n### Java\n\n```java\n// LoginActivity.java - File in the login module.\n\npublic class LoginActivity extends AppCompatActivity {\n\n @Inject\n LoginAnalyticsAdapter loginAnalyticsAdapter;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n DaggerLoginComponent.builder()\n .context(this)\n .appDependencies(\n EntryPointAccessors.fromApplication(\n getApplicationContext(),\n LoginModuleDependencies.class\n )\n )\n .build()\n .inject(this);\n\n super.onCreate(savedInstanceState);\n ...\n }\n}\n```\n\nFor more context on module dependencies in feature modules, see\n[Component dependencies with feature\nmodules](/training/dependency-injection/dagger-multi-module#dagger-dfm).\n\nFor more information about Dagger on Android, see [Using Dagger in Android\napps](/training/dependency-injection/dagger-android)."]]