با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
تولید کد Hilt نیاز به دسترسی به تمام ماژول های Gradle دارد که از Hilt استفاده می کنند. ماژول Gradle که کلاس Application شما را کامپایل میکند، باید همه ماژولهای Hilt و کلاسهای تزریقشده توسط سازنده را در وابستگیهای متعدی خود داشته باشد.
اگر پروژه چند ماژول شما از ماژول های Gradle معمولی تشکیل شده است، می توانید از Hilt همانطور که در Dependency injection با Hilt توضیح داده شده است استفاده کنید. با این حال، این مورد در مورد برنامه هایی که شامل ماژول های ویژگی هستند صدق نمی کند.
انتخاب در ماژول های ویژگی
در ماژول های ویژگی، نحوه وابستگی ماژول ها به یکدیگر معکوس است. بنابراین، Hilt نمی تواند حاشیه نویسی را در ماژول های ویژگی پردازش کند. برای انجام تزریق وابستگی در ماژول های ویژگی خود باید از Dagger استفاده کنید.
برای حل این مشکل با ماژول های ویژگی باید از وابستگی های کامپوننت استفاده کنید. این مراحل را دنبال کنید:
یک رابط کاربری @EntryPoint را در ماژول app (یا در هر ماژول دیگری که می تواند توسط Hilt پردازش شود) با وابستگی هایی که ماژول ویژگی به آن نیاز دارد، اعلام کنید.
یک مؤلفه Dagger ایجاد کنید که به رابط @EntryPoint بستگی دارد.
از Dagger به طور معمول در ماژول ویژگی استفاده کنید.
مثالی از صفحه Dependency injection with Hilt را در نظر بگیرید. فرض کنید یک ماژول ویژگی login به پروژه خود را اضافه کرده اید. شما ویژگی ورود به سیستم را با فعالیتی به نام LoginActivity پیاده سازی می کنید. این بدان معنی است که شما می توانید اتصالات را فقط از مؤلفه برنامه دریافت کنید.
برای این ویژگی، به یک OkHttpClient با اتصال authInterceptor نیاز دارید.
ابتدا یک رابط @EntryPoint نصب شده در SingletonComponent با اتصالاتی که ماژول login نیاز دارد ایجاد کنید:
کاتلین
// 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 ، یک مؤلفه Dagger ایجاد کنید که به رابط @EntryPoint بستگی دارد:
کاتلین
// 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 به عنوان وابستگی یک کلاس استفاده کنید:
کاتلین
// 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;}...}
برای انجام تزریق فیلد، یک نمونه از مؤلفه Dagger با استفاده از applicationContext ایجاد کنید تا وابستگی های SingletonComponent را دریافت کنید:
کاتلین
// 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 و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)."]]