الوحدات و@Provides و@Binds
أخبر Hilt بكيفية إنشاء أنواعك
الوحدات و@Provides و@Binds درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
When Constructor Injection Is Not Enough
You cannot add @Inject to a constructor you do not own, like a Retrofit or OkHttp instance. Hilt needs another way to learn how to build those.
Meet the Module
A module is a class that teaches Hilt how to create types it cannot construct on its own. Mark it with @Module to register it.
@Module
object NetworkModule { }Install Into a Component
Every module declares where it lives with @InstallIn. SingletonComponent makes its bindings available app-wide for the whole process.
@Module
@InstallIn(SingletonComponent::class)
object NetworkModuleProvide a Type With @Provides
@Provides marks a function whose return value Hilt will use as a dependency. The function body shows exactly how to build it.
@Provides
fun provideApi(): ApiService = Retrofit.Builder().build().create(ApiService::class.java)Provides Can Take Parameters
A @Provides function can ask for other dependencies as parameters. Hilt supplies them first, then runs your function to assemble the result.
@Provides
fun provideRepo(api: ApiService): Repo = Repo(api)Binding Interfaces With @Binds
When you just need to map an interface to an implementation, @Binds is leaner than @Provides. It tells Hilt which class satisfies the interface.
@Binds
abstract fun bindRepo(impl: RepoImpl): Repo@Binds Needs an Abstract Module
Because @Binds methods are abstract, their module must be an abstract class, not an object. The implementation must already be injectable.
@Module
@InstallIn(SingletonComponent::class)
abstract class RepoModuleProvides vs Binds
Use @Binds for a simple interface-to-class mapping, and @Provides when you must run code to build the object. Binds generates less code.
Mark Singletons
Add @Singleton to a provider so Hilt creates the object once and reuses it. Perfect for heavy clients like a shared OkHttp instance.
@Provides
@Singleton
fun provideClient(): OkHttpClient = OkHttpClient()Disambiguate With Qualifiers
If two providers return the same type, a @Qualifier annotation tells them apart. Hilt then knows which binding each injection point wants.
@Qualifier
annotation class AuthClientModules Build the Graph
Together your modules form the dependency graph. Hilt reads them at compile time and wires every type into a connected, verified tree.
Quick Check
You need to bind a Repo interface to its single RepoImpl class. Which is the leanest choice?
Recap: Teaching Hilt to Build
You saw that modules teach Hilt how to create types it cannot construct, using @Provides for code and @Binds for interface mappings. 🧩
الأسئلة الشائعة
هل درس «الوحدات و@Provides و@Binds» مجاني؟
نعم — نص درس «الوحدات و@Provides و@Binds» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «الوحدات و@Provides و@Binds»؟
أخبر Hilt بكيفية إنشاء أنواعك تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الوحدات و@Provides و@Binds»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة
- الوحدات و@Provides و@Binds
- hiltViewModel و@HiltViewModel
- النطاقات وأعمار المكونات