android hilt 坑位

开发者 2024-9-23 01:43:10 68 0 来自 中国
之前和如今利用

之前
buildscript {    ...    dependencies {        ...        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'    }}...apply plugin: 'kotlin-kapt'apply plugin: 'dagger.hilt.android.plugin'android {    ...}dependencies {    implementation "com.google.dagger:hilt-android:2.28-alpha"    kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"}如今 用到了
plugins {    id 'com.android.application' version '7.1.3' apply false    id 'com.android.library' version '7.1.3' apply false    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false    // 新的    id 'com.google.dagger.hilt.android' version '2.41' apply false}plugins {    id 'com.android.application'    id 'org.jetbrains.kotlin.android'    id 'kotlin-android-extensions'    id 'dagger.hilt.android.plugin'    id 'kotlin-kapt'}如许直接写 就会找不到 dagger.hilt.android.plugin 我看了半天就不知道为啥,就表现解释掉。
···
The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found.
···
写上
/*hide*/    implementation "com.google.dagger:hilt-android:2.38.1"    kapt "com.google.dagger:hilt-android-compiler:2.38.1"    implementation "androidx.activity:activity-ktx:1.4.0"    implementation 'androidx.fragment:fragment-ktx:1.4.0'之后在Sync Now,就安然无恙了。
demo 部门。正常的viewmodel 部门利用

一个多余的代码都没有了
@HiltAndroidAppclass App : Application() {}首页中
@AndroidEntryPointclass MainActivity : AppCompatActivity() {    private val viewmodel: UserViewmodel by viewModels()    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        hello.text = viewmodel.text.value    }}@HiltViewModelclass UserViewmodel @Inject constructor(    private val userRepository: UserRepository) : ViewModel() {    private val _text = MutableLiveData<String>()    val text: LiveData<String> = _text    init {        loadCrypto()    }    private fun loadCrypto() {        _text.value = userRepository.getHelloText()    }}interface UserRepository {    fun getHelloText(): String?}详细实现class UserImpl : UserRepository {    override fun getHelloText(): String? {        return "hello hilt";    }}绑定命据
@Module@InstallIn(SingletonComponent::class)class AppModule {    @Provides    @Singleton    fun provideuserRepository(): UserRepository = UserImpl()}焦点就是

3.png 和官网的想法一样。
但为啥代码不一样
如许去掉 viewmodel 利用
@Module@InstallIn(ActivityComponent::class)public interface UserEngine {    @Binds    fun bindUserService(userImpl: UserImpl): UserRepository}import javax.inject.Injectclass UserImpl @Inject constructor() : UserRepository {    override fun getHelloText(): String {        return "hello hilt"    }}修改了一下 @Inject constructor()  添加了这个。
同样的mainactivity 中利用
@AndroidEntryPointclass MainActivity : AppCompatActivity() {    //    private val viewmodel: UserViewmodel by viewModels()    @Inject    @JvmField    var userRepository: UserRepository? = null    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)//        hello.text = viewmodel.text.value        hello.text = userRepository?.getHelloText()    }}就简朴多了。
官网上有一个可以多个 绑定的

也是同样的原理可以做的

慢慢的官方的文档看到另有点味道了。反而以为

6.png 末了感谢
https://developer.android.google.cn/training/dependency-injection/hilt-android#inject-provides
https://blog.csdn.net/Android_data/article/details/120638365
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 20:30, Processed in 0.161778 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表