「Android/kotlin/DaggerHilt」の版間の差分
提供: 初心者エンジニアの簡易メモ
行47: | 行47: | ||
android:name="com.example.daggerhiltapplication.MainApplication" | android:name="com.example.daggerhiltapplication.MainApplication" | ||
</pre> | </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) | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </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 | 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年2月9日 (火) 04:12時点における版
サンプル
プロジェクト直のbuild.gradleに以下追加
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}" } }
app/build.gradleに以下追加
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}" }
applicationを追加
MainApplication.kt
import android.app.Application import dagger.hilt.android.HiltAndroidApp @HiltAndroidApp class MainApplication : Application()
AndoridManifest.xml のandroid:nameにpathを追加
<?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"
MainActivity.kt
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) } }