引包
implementation "org.koin:koin-android:2.2.0-rc-3"
implementation "org.koin:koin-androidx-viewmodel:2.2.0-rc-3"
Koin 怎么使用?
对象
data class Gril (val age:Int, val name:String)
Application
import android.app.Application
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidFileProperties
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.logger.Level
class App : Application(){
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger(Level.DEBUG)
androidContext(this@App)
androidFileProperties()
modules(listOf(girlModule))
}
}
}
MainActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import org.koin.android.ext.android.inject
class MainActivity : AppCompatActivity() {
val TAG:String = "MainActivityTAG"
private val girl by inject<Gril>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.i(TAG, "girl memory address:"+ girl.toString() +",girl age:"+girl.age+",girl name:"+girl.name)
}
}
网友评论