What is Koin?
Koin is simple dependency injection support using DSL by Kotlin. (A programatic lightweight dependency injection framework)
These days, DI is popular in Android, (Dagger2 etc..) Koin is simple and easy to use
What to cover in this sample
- General Inject
- View Model Inject
Preparation
To use Koin, we need to add gradle description (Use version 2.1.3)
implementation 'org.koin:koin-android:2.1.3' // Koin Android Scope feature implementation "org.koin:koin-android-scope:2.1.3" // Koin Android ViewModel feature implementation "org.koin:koin-android-viewmodel:2.1.3"
Scope is not required in this sample. ViewModel is for ViewModel injection
Let’s start sample
General Inject
The first is simple example.
Prepare following codes
- HelloRepository
- HelloRepositoryImpl
- HelloSimplePresenter
- modules.kt
- MyApplication
- MainActivity
- AndroidManifest.xml
HelloRepository.kt
interface HelloContextRepository { fun getName() : String }
HelloRepositoryImpl.kt
This is implementation of above interface
class HelloRepositoryImpl() : HelloRepository { override fun giveHello() = "Hello Koin" }
HelloSimplePresenter
This is factory class
class HelloSimplePresenter(val repo: HelloRepository) { fun sayHello() = "${repo.giveHello()} from $this" }
modules.kt
Prepare module import to use
import org.koin.android.ext.koin.androidContext import org.koin.android.viewmodel.dsl.viewModel import org.koin.core.qualifier.named import org.koin.dsl.module val appModule = module { single<HelloRepository> { HelloRepositoryImpl() } factory { HelloSimplePresenter(get()) } // This is for next ViewModel inject single<HelloViewModelRepository> { HelloViewModelRepositoryImpl()} viewModel { HelloViewModel(get()) } }
MyApplication
Use startKoin to import module list and some setup (context, logger etc…, Only modules are required, others are not mandatory)
class MyApplication : Application() { override fun onCreate() { super.onCreate() // Start Koin startKoin { // use AndroidLogger as Koin Logger - default Level.INFO // The androidLogger() function makes it possible to receive logs from Koin androidLogger() // use the Android context given there androidContext(this@MyApplication) // load properties from assets/koin.properties file androidFileProperties() modules(appModule) } } }
MainActivity
Test to use (both general and view model)
class MainActivity : AppCompatActivity() { val firstPresenter: HelloSimplePresenter by inject() val myViewModel: HelloViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val greeting = firstPresenter.sayHello() print(greeting) // Hello Koin from val presenter : HelloSimplePresenter = get() val greetintg2 = presenter.sayHello() print(greetintg2) // Hello Koin from val greeting3 = myViewModel.sayHello() // Hello ViewModel Koin from print(greeting3) } }
View Model Inject
- HelloViewModel
- HelloViewModelRepository
- HelloViewModelRepositoryImpl
HelloViewModel
class HelloViewModel(val repo: HelloViewModelRepository) : ViewModel() { fun sayHello() = "${repo.giveHello()} from $this" }
HelloViewModelRepository
interface HelloViewModelRepository { fun giveHello(): String }
HelloViewModelRepositoryImpl
class HelloViewModelRepositoryImpl() : HelloViewModelRepository { override fun giveHello()= "Hello ViewModel Koin" }
ViewModel and general inject is almost all same
コメント