facebook twitter hatena line email

「Android/kotlin/DaggerHilt」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(MainViewModelを追加)
(ページの白紙化)
行1: 行1:
==サンプル==
 
プロジェクト直のbuild.gradleに以下追加
 
<pre>
 
buildscript {
 
    dependencies {
 
        classpath "com.android.tools.build:gradle:4.1.1"
 
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 
        def dagger_hilt_version = "2.31-alpha"
 
        classpath "com.google.dagger:hilt-android-gradle-plugin:${dagger_hilt_version}"
 
    }
 
}
 
</pre>
 
  
app/build.gradleに以下追加
 
<pre>
 
plugins {
 
    id 'kotlin-kapt'
 
    id 'dagger.hilt.android.plugin'
 
}
 
dependencies {
 
    def dagger_hilt_version = "2.31.2-alpha"
 
    implementation "com.google.dagger:hilt-android:${dagger_hilt_version}"
 
    kapt "com.google.dagger:hilt-android-compiler:${dagger_hilt_version}"
 
    def dagger_hilt_view_model_version = "1.0.0-alpha03"
 
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:${dagger_hilt_view_model_version}"
 
    kapt "androidx.hilt:hilt-compiler:${dagger_hilt_view_model_version}"
 
}
 
</pre>
 
 
applicationを追加
 
 
MainApplication.kt
 
<pre>
 
import android.app.Application
 
import dagger.hilt.android.HiltAndroidApp
 
 
@HiltAndroidApp
 
class MainApplication : Application()
 
</pre>
 
 
AndoridManifest.xml のandroid:nameにpathを追加
 
<pre>
 
<?xml version="1.0" encoding="utf-8"?>
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 
    package="com.example.daggerhiltapplication">
 
    <application
 
        android:name="com.example.daggerhiltapplication.MainApplication"
 
</pre>
 
 
MainActivity.kt
 
<pre>
 
import androidx.appcompat.app.AppCompatActivity
 
import android.os.Bundle
 
import dagger.hilt.android.AndroidEntryPoint
 
import javax.inject.Inject
 
 
@AndroidEntryPoint
 
class MainActivity : AppCompatActivity() {
 
    @Inject
 
    lateinit var usecase: HogeUsecase
 
    override fun onCreate(savedInstanceState: Bundle?) {
 
        super.onCreate(savedInstanceState)
 
        setContentView(R.layout.activity_main)
 
 
        Log.v("MainActivity", "onCreate!!")
 
        usecase.print("MainActivity", "usecase.print!!")
 
    }
 
}
 
</pre>
 
 
サービスとユースケースの読み込みモジュール
 
 
ApplicationBindsModule.kt
 
<pre>
 
import dagger.Binds
 
import dagger.Module
 
import dagger.hilt.InstallIn
 
import dagger.hilt.components.SingletonComponent
 
 
@Module
 
@InstallIn(SingletonComponent::class)
 
abstract class ApplicationBindsModule {
 
    @Binds
 
    abstract fun bindHogeService(hogeServiceImpl: HogeServiceImpl) : HogeService
 
}
 
</pre>
 
 
ApplicationProvidesModule.kt
 
<pre>
 
import dagger.Module
 
import dagger.Provides
 
import dagger.hilt.InstallIn
 
import dagger.hilt.components.SingletonComponent
 
 
@Module
 
@InstallIn(SingletonComponent::class)
 
object ApplicationProvidesModule {
 
    @Provides
 
    fun provideHogeUseCase(hogeService: HogeService) : HogeUsecase {
 
        Log.v("ApplicationProvides", "provideHogeUseCase!!")
 
        return HogeUsecase(hogeService)
 
    }
 
}
 
</pre>
 
 
HogeService.kt
 
<pre>
 
import android.util.Log
 
import javax.inject.Inject
 
 
interface HogeService {
 
    fun print(tag: String, message: String)
 
}
 
class HogeServiceImpl @Inject constructor(): HogeService {
 
    override fun print(tag: String, message: String) {
 
        Log.v("HogeServiceImpl", "print!! tag=$tag message=$message")
 
    }
 
}
 
</pre>
 
 
HogeUsecase.kt
 
<pre>
 
import javax.inject.Inject
 
 
class HogeUsecase @Inject constructor(private val service: HogeService) {
 
    fun print(tag: String, message: String) {
 
        Log.v("HogeUsecase", "print!! tag=$tag message=$message")
 
        service.print(tag, message)
 
    }
 
}
 
</pre>
 
 
出力
 
<pre>
 
V/ApplicationProvides: provideHogeUseCase!!
 
V/MainActivity: onCreate!!
 
V/HogeUsecase: print!! tag=MainActivity message=usecase.print!!
 
V/HogeServiceImpl: print!! tag=MainActivity message=usecase.print!!
 
</pre>
 
 
==MainViewModelを追加==
 
app/build.gradle
 
<pre>
 
dependencies {
 
    implementation "androidx.activity:activity-ktx:1.1.0"
 
}
 
</pre>
 
MainViewModel.kt
 
<pre>
 
import androidx.lifecycle.SavedStateHandle
 
import androidx.lifecycle.ViewModel
 
import dagger.hilt.android.lifecycle.HiltViewModel
 
import javax.inject.Inject
 
 
@HiltViewModel
 
class MainViewModel @Inject constructor(
 
        private val savedState: SavedStateHandle,
 
        private val usecase: HogeUsecase
 
): ViewModel() {
 
    fun print() {
 
        usecase.print("MainViewModel", "print!!")
 
    }
 
}
 
</pre>
 
 
MainActivity.ktを改修
 
<pre>
 
import androidx.appcompat.app.AppCompatActivity
 
import android.os.Bundle
 
import android.util.Log
 
import androidx.activity.viewModels
 
import dagger.hilt.android.AndroidEntryPoint
 
import javax.inject.Inject
 
 
@AndroidEntryPoint
 
class MainActivity : AppCompatActivity() {
 
 
    @Inject
 
    lateinit var usecase: HogeUsecase
 
 
    private val mainViewModel: MainViewModel by viewModels()
 
 
    override fun onCreate(savedInstanceState: Bundle?) {
 
        super.onCreate(savedInstanceState)
 
        setContentView(R.layout.activity_main)
 
 
        Log.v("MainActivity", "onCreate!!")
 
        usecase.print("MainActivity", "usecase.print!!")
 
 
        mainViewModel.print()
 
 
    }
 
}
 
</pre>
 
 
出力
 
<pre>
 
V/ApplicationProvides: provideHogeUseCase!!
 
V/MainActivity: onCreate!!
 
V/HogeUsecase: print!! tag=MainActivity message=usecase.print!!
 
V/HogeServiceImpl: print!! tag=MainActivity message=usecase.print!!
 
V/ApplicationProvides: provideHogeUseCase!!
 
V/HogeUsecase: print!! tag=MainViewModel message=print!!
 
V/HogeServiceImpl: print!! tag=MainViewModel message=print!!
 
</pre>
 
 
==参考==
 
https://medium.com/kaleidot725/%E8%A9%B1%E9%A1%8C%E3%81%AE-dagger-hilt-%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%BF%E3%81%9F-13acab8163e0
 

2021年3月31日 (水) 15:06時点における版