使用 Hilt 实现依赖项注入

使用 Hilt 实现依赖项注入

附上官网文档
Hilt 实现依赖项注入
参考了大佬的文档
因为AndroidStudio的版本问题,和之前老的版本不太一样。
其中build.gradle分成两部分:

  1. settings.gradle

  2. build.gradle

1、settings.gradle

这里可以添加moven库

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven { url 'https://maven-other.tuya.com/repository/maven-releases/' }
        maven { url 'https://jitpack.io' }

    }
}

2、build.gradle

Android文档描述
Android文档描述
然后我们在项目的根级 build.gradle 文件中这样添加:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
    }
}
**//buildscript要在plugins 前面,否则会出现报错**
plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
}



task clean(type: Delete) {
    delete rootProject.buildDir
}

在app/build.gradle中添加
app/build.gradle

plugins {
    id 'com.android.application' //本身就存在的,我们在下方添加相对应的就行了
    id'dagger.hilt.android.plugin'
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值