【Android,Jetpack】快速接入Jetpack Hilt备忘录

Android,Jetpack,快速接入Jetpack Hilt备忘录

1.Gradle配置

  • 根目录build.gradle加入classpath
buildscript {
    repositories {
        gradlePluginPortal()
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url 'https://maven.aliyun.com/repository/grails-core' }
        maven { url 'https://gitee.com/liuchaoya/libcommon/raw/master/repository/' }
        maven { url 'https://www.jitpack.io' }
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.46.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
  • 子项目级build.gradle
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'com.google.dagger.hilt.android'
}

dependencies {
    //hilt
    implementation 'com.google.dagger:hilt-android:2.46.1'
    kapt 'com.google.dagger:hilt-compiler:2.46.1'
}

2.Manifest配置

  • 配置AndroidManifest.xml
    <application
        android:name=".MainApplication"
        ...>
        <activity
            ...
        </activity>
    </application>
  • 配置MainApplication
@HiltAndroidApp
class MainApplication : BaseApplication() {
}

3.一些使用示例

  • Activity
@AndroidEntryPoint
class MainActivity : BaseActivityVBVM<ActivityMainBinding, MainViewModel>() {
    @Inject
    lateinit var activityBean: ActivityBean

    override fun initView(savedInstanceState: Bundle?) {
        vb.mainTxt.text = activityBean.name
        vb.mainTxt1.text = vm.getCache()
        vb.mainTxt2.text = vm.getRemote()
    }

    override fun bindViewVM(vb: ActivityMainBinding) {
        vb.vm = vm
    }
}
  • ViewModel
@HiltViewModel
class MainViewModel @Inject constructor(private val _cacheRepository: CacheRepository, private val _remoteRepository: RemoteRepository, private val _getValueListener: IGetValueListener) :
    BaseViewModel() {
    fun getCache(): String = _cacheRepository.getCache()
    fun getRemote(): String = _remoteRepository.getRemote()

    private val _msfValue = MutableStateFlow(_getValueListener.onGetValue())
    val sfValue: StateFlow<String> = _msfValue
}
  • bean
@ActivityScoped
class ActivityBean constructor(val name: String) {
    @Inject
    constructor() : this("123")
}
  • 配置自己的di
    下面模拟本地仓库和远程仓库
class RemoteRepository {
    fun getRemote(): String = "789"
}

class CacheRepository {
    fun getCache(): String = "456"
}
  • @Binds方式注入
@Module
@InstallIn(SingletonComponent::class)
abstract class AbsSingletonModule {

    @Binds
    @Singleton
    abstract fun bindIGetValueListener(getValueCallback: GetValueCallback): IGetValueListener
}

class GetValueCallback @Inject constructor() : IGetValueListener {
    override fun onGetValue(): String = "这是@Bind的实践"
}

interface IGetValueListener {
    fun onGetValue(): String
}
  • @Provides方式注入
@Module
@InstallIn(SingletonComponent::class)
class SingletonModule {

    @Singleton
    @Provides
    fun provideCacheRepository(): CacheRepository = CacheRepository()
}
  • 注入到ViewModel作用域内
@Module
@InstallIn(ViewModelComponent::class)
class ViewModelModule {

    @ViewModelScoped
    @Provides
    fun providerRemoteRepository(): RemoteRepository = RemoteRepository()
}

猜你喜欢

转载自blog.csdn.net/weixin_42473228/article/details/133339793