بعد ذلك، يمكن لأي نشاط أو جزء تمت إضافة تعليقات توضيحية إليه باستخدام @AndroidEntryPoint.
الحصول على المثيل ViewModel كالمعتاد باستخدام ViewModelProvider أو
by viewModels()إضافات KTX:
توفّر "ViewModelComponent" جميع نماذج Hilt ViewModels التي تلي
دورة الحياة نفسها مثل ViewModel، وبالتالي يمكنها البقاء على قيد الحياة عند إجراء تغييرات في الإعدادات.
لتحديد نطاق التبعية إلى ViewModel، استخدِم التعليق التوضيحي @ViewModelScoped.
سيُجري نوع @ViewModelScoped ذلك بحيث يكون مثيل واحد لنطاق النطاق
نوع البيانات على مستوى جميع الاعتماديات التي تم إدخالها في ViewModel.
ستتلقَّى الحالات الأخرى من ViewModel التي تطلب المثيل ذي النطاق
مثيل مختلف.
وإذا كانت هناك حاجة لمشاركة مثيل واحد عبر نماذج ViewModels المختلفة،
يجب تحديد نطاقه باستخدام @ActivityRetainedScoped أو @Singleton.
إذا كان ViewModelعلى مستوى التنقل
الرسم البياني،
تستخدم الدالة hiltNavGraphViewModels التي تعمل مع الأجزاء
تمت إضافة تعليقات توضيحية إليه باستخدام @AndroidEntryPoint.
لمعرفة كيفية تكامل Hilt مع Jetpack Compose، يمكنك الاطّلاع على القسم Hilt
الإنشاء والمكتبات الأخرى:
إدخال WorkManager باستخدام Hilt
أضف التبعيات الإضافية التالية إلى ملف Gradle الخاص بك. لاحظ أنه في
بالإضافة إلى المكتبة، يجب تضمين معالج إضافي للتعليقات التوضيحية
التي تعمل في أعلى معالِج التعليقات التوضيحية في Hilt:
app/build.gradle
Groovy
dependencies{...implementation'androidx.hilt:hilt-work:1.0.0'// When using Kotlin.kapt'androidx.hilt:hilt-compiler:1.0.0'// When using Java.annotationProcessor'androidx.hilt:hilt-compiler:1.0.0'}
Kotlin
dependencies{implementation("androidx.hilt:hilt-work:1.0.0")// When using Kotlin.kapt("androidx.hilt:hilt-compiler:1.0.0")// When using Java.annotationProcessor("androidx.hilt:hilt-compiler:1.0.0")}
يمكنك إدخال Worker باستخدام
التعليق التوضيحي @HiltWorker في الصف و@AssistedInject في Worker
الدالة الإنشائية للكائن. يمكنك استخدام @Singleton فقط أو عمليات الربط غير المحدَّدة النطاق في
Worker عناصر يجب أيضًا إضافة تعليقات توضيحية على Context وWorkerParameters.
التبعيات ذات القيمة @Assisted:
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Use Hilt with other Jetpack libraries\n\nHilt includes extensions for providing classes from other Jetpack libraries.\nHilt currently supports the following Jetpack components:\n\n- `ViewModel`\n- Navigation\n- Compose\n- WorkManager\n\nYou must add the Hilt dependencies to take advantage of these integrations. For\nmore information about adding dependencies, see [Dependency injection with\nHilt](/training/dependency-injection/hilt-android#setup).\n\nInject ViewModel objects with Hilt\n----------------------------------\n\nProvide a [`ViewModel`](/topic/libraries/architecture/viewmodel) by annotating\nit with `@HiltViewModel` and using the `@Inject` annotation in the `ViewModel`\nobject's constructor. \n\n### Kotlin\n\n```kotlin\n@HiltViewModel\nclass ExampleViewModel @Inject constructor(\n private val savedStateHandle: SavedStateHandle,\n private val repository: ExampleRepository\n) : ViewModel() {\n ...\n}\n```\n\n### Java\n\n```java\n@HiltViewModel\npublic class ExampleViewModel extends ViewModel {\n\n private final ExampleRepository repository;\n private final SavedStateHandle savedStateHandle;\n\n @Inject\n ExampleViewModel(\n SavedStateHandle savedStateHandle,\n ExampleRepository repository)\n {\n this.savedStateHandle = savedStateHandle;\n this.repository = repository;\n }\n ...\n}\n```\n\nThen, an activity or a fragment that is annotated with `@AndroidEntryPoint` can\nget the `ViewModel` instance as normal using `ViewModelProvider` or the\n`by viewModels()` [KTX extensions](/kotlin/ktx): \n\n### Kotlin\n\n```kotlin\n@AndroidEntryPoint\nclass ExampleActivity : AppCompatActivity() {\n private val exampleViewModel: ExampleViewModel by viewModels()\n ...\n}\n```\n\n### Java\n\n```java\n@AndroidEntryPoint\npublic class ExampleActivity extends AppCompatActivity {\n\n private ExampleViewModel exampleViewModel;\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n exampleViewModel = new ViewModelProvider(this).get(ExampleViewModel.class);\n }\n ...\n}\n```\n| **Note:** To use Dagger's assisted injection with ViewModels, see the following [Github issue](https://github.com/google/dagger/issues/2287).\n\n### @ViewModelScoped\n\nAll Hilt ViewModels are provided by the `ViewModelComponent` which follows the\nsame lifecycle as a `ViewModel`, and as such, can survive configuration changes.\nTo scope a dependency to a `ViewModel` use the `@ViewModelScoped` annotation.\n\nA `@ViewModelScoped` type will make it so that a single instance of the scoped\ntype is provided across all dependencies injected into the `ViewModel`.\nOther instances of a ViewModel that request the scoped instance will receive\na different instance.\n\nIf a single instance needs to be shared across various ViewModels, then it\nshould be scoped using either `@ActivityRetainedScoped` or `@Singleton`.\n\nIntegration with the Jetpack navigation library\n-----------------------------------------------\n\nAdd the following additional dependencies to your Gradle file:\n\napp/build.gradle \n\n### Groovy\n\n```groovy\ndependencies {\n ...\n implementation 'androidx.hilt:hilt-navigation-fragment:1.0.0'\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n ...\n implementation(\"androidx.hilt:hilt-navigation-fragment:1.0.0\")\n}\n```\n\nIf your `ViewModel` is [scoped to the navigation\ngraph](/guide/navigation/navigation-programmatic#share_ui-related_data_between_destinations_with_viewmodel),\nuse the `hiltNavGraphViewModels` function that works with fragments that are\nannotated with `@AndroidEntryPoint`. \n\n### Kotlin\n\n```kotlin\nval viewModel: ExampleViewModel by hiltNavGraphViewModels(R.id.my_graph)\n```\n\n### Java\n\n```java\nNavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph);\n\nExampleViewModel exampleViewModel = new ViewModelProvider(\n backStackEntry,\n HiltViewModelFactory.create(context, backStackEntry)\n).get(ExampleViewModel.class)\n```\n\nIntegration with Jetpack Compose\n--------------------------------\n\nTo see how Hilt integrates with Jetpack Compose, see the Hilt section of\n[Compose and other libraries](/jetpack/compose/libraries#hilt).\n\nInject WorkManager with Hilt\n----------------------------\n\nAdd the following additional dependencies to your Gradle file. Note that in\naddition to the library, you need to include an additional annotation processor\nthat works on top of the Hilt annotation processor:\n\napp/build.gradle \n\n### Groovy\n\n```groovy\ndependencies {\n ...\n implementation 'androidx.hilt:hilt-work:1.0.0'\n // When using Kotlin.\n kapt 'androidx.hilt:hilt-compiler:1.0.0'\n // When using Java.\n annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0'\n}\n```\n\n### Kotlin\n\n```kotlin\ndependencies {\n implementation(\"androidx.hilt:hilt-work:1.0.0\")\n // When using Kotlin.\n kapt(\"androidx.hilt:hilt-compiler:1.0.0\")\n // When using Java.\n annotationProcessor(\"androidx.hilt:hilt-compiler:1.0.0\")\n}\n```\n\nInject a [`Worker`](/reference/kotlin/androidx/work/Worker) using the\n`@HiltWorker` annotation in the class and `@AssistedInject` in the `Worker`\nobject's constructor. You can use only `@Singleton` or unscoped bindings in\n`Worker` objects. You must also annotate the `Context` and `WorkerParameters`\ndependencies with `@Assisted`: \n\n### Kotlin\n\n```kotlin\n@HiltWorker\nclass ExampleWorker @AssistedInject constructor(\n @Assisted appContext: Context,\n @Assisted workerParams: WorkerParameters,\n workerDependency: WorkerDependency\n) : Worker(appContext, workerParams) { ... }\n```\n\n### Java\n\n```java\n@HiltWorker\npublic class ExampleWorker extends Worker {\n\n private final WorkerDependency workerDependency;\n\n @AssistedInject\n ExampleWorker(\n @Assisted @NonNull Context context,\n @Assisted @NonNull WorkerParameters params,\n WorkerDependency workerDependency\n ) {\n super(context, params);\n this.workerDependency = workerDependency;\n }\n ...\n}\n```\n\nThen, have your [`Application`](/reference/android/app/Application) class\nimplement the `Configuration.Provider` interface, inject an instance of\n`HiltWorkFactory`, and pass it into the `WorkManager` configuration as follows: \n\n### Kotlin\n\n```kotlin\n@HiltAndroidApp\nclass ExampleApplication : Application(), Configuration.Provider {\n\n @Inject lateinit var workerFactory: HiltWorkerFactory\n\n override fun getWorkManagerConfiguration() =\n Configuration.Builder()\n .setWorkerFactory(workerFactory)\n .build()\n}\n```\n\n### Java\n\n```java\n@HiltAndroidApp\npublic class ExampleApplication extends Application implements Configuration.Provider {\n\n @Inject HiltWorkerFactory workerFactory;\n\n @Override\n public Configuration getWorkManagerConfiguration() {\n return new Configuration.Builder()\n .setWorkerFactory(workerFactory)\n .build();\n }\n}\n```\n| **Note:** Because this customizes the `WorkManager` configuration, you also must remove the default initializer from the `AndroidManifest.xml` file as specified in the [WorkManager docs](https://developer.android.com/topic/libraries/architecture/workmanager/advanced/custom-configuration).\n| **Warning:** `WorkManager` version `2.6.0-alpha01` or higher uses the `androidx.startup` initializer. To properly configure `WorkManager` in this version with Hilt, check out the [`WorkManager` release notes](https://developer.android.com/jetpack/androidx/releases/work#2.6.0-alpha01)."]]