文筆不好,有不懂的屬性可以去百度橡淑,這里不作過多解釋
導(dǎo)入
沒啥好說的,需要注意的就是別忘了加上
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
在dependencies里 (我這里用的2.13 有最新就用最新的吧)
//dagger2
implementation 'com.google.dagger:dagger:2.13'
kapt 'com.google.dagger:dagger-compiler:2.13'
implementation 'com.google.dagger:dagger-android:2.13'
implementation 'com.google.dagger:dagger-android-support:2.13'
kapt 'com.google.dagger:dagger-android-processor:2.13'
進(jìn)入主題
Module
這里寫了個ActivityBuilderModule
@Module
abstract class ActivityBuilderModule {
@Singleton
@Binds
abstract fun application(app: MyApplication): Context
@ContributesAndroidInjector(modules = arrayOf(MainModule::class))
abstract fun bindMainActivity(): MainActivity
}
/**
* Activity里的每一個Fragment,要在module里面如下聲明
* 并且Fragment還有用@Inject標(biāo)注其無參構(gòu)造方法
*/
@Module
abstract class MainModule {
@Binds
abstract fun bindMainFragment(mainFragment: MainFragment): Fragment
@ContributesAndroidInjector
abstract fun bindMainFragment(): MainFragment
}
Component
只需創(chuàng)建一個實(shí)例岭埠,有子組件可以寫在modules里
AndroidSupportInjectionModule 這個module是來自Dagger.Android 用來幫助生成和定位SupComponents(子組件)
@Singleton
@Component(modules = arrayOf(AndroidSupportInjectionModule::class, ActivityBuilderModule::class))
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): AppComponent.Builder
fun build(): AppComponent
}
fun inject(instance: MyApplication)
}
別忘了Make Project
Component會在自己生成一個 (Dagger+你的Component),本文的是DaggerAppComponent蔚鸥,然后在Application創(chuàng)建單例的Component
Application
1.你可以直接繼承 DaggerApplication
2.如本文惜论,繼承HasActivityInjector實(shí)現(xiàn),否則編譯不過
@Inject
lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
override fun activityInjector(): AndroidInjector<Activity> {
return dispatchingActivityInjector
}
繼承HasActivityInjector
class MyApplication : Application() , HasActivityInjector {
@Inject
lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
override fun activityInjector(): AndroidInjector<Activity> {
return dispatchingActivityInjector
}
override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder().application(this)
.build().inject(this)
}
}
注入Activity和Fragment
1.你可以直接繼承DaggerAppCompatActivity止喷、DaggerFragment
2.創(chuàng)建BaseActivity馆类、BaseFragment 然后繼承...如下文
//BaseActivity
abstract class BaseActivity : AppCompatActivity(), HasFragmentInjector, HasSupportFragmentInjector {
@Inject
lateinit var supportFragmentInjector: DispatchingAndroidInjector<Fragment>
@Inject
lateinit var frameworkFragmentInjector: DispatchingAndroidInjector<android.app.Fragment>
override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return supportFragmentInjector
}
override fun fragmentInjector(): AndroidInjector<android.app.Fragment> {
return frameworkFragmentInjector
}
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}
}
//BaseFragment
abstract class BaseFragment : Fragment(), HasSupportFragmentInjector {
@Inject
lateinit var childFragmentInjector: DispatchingAndroidInjector<Fragment>
override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return childFragmentInjector
}
@SuppressWarnings("deprecation")
override fun onAttach(activity: Activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Perform injection here before M, L (API 22) and below because onAttach(Context)
// is not yet available at L.
AndroidSupportInjection.inject(this)
}
super.onAttach(activity)
}
override fun onAttach(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Perform injection here for M (API 23) due to deprecation of onAttach(Activity).
AndroidSupportInjection.inject(this)
}
super.onAttach(context)
}
}
Activity
ps:lateinit前不要用private 否則編譯報(bào)錯
class MainActivity : BaseActivity() {
@Inject
lateinit var mMainFragment: MainFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager
.beginTransaction()
.add(R.id.fragment_content, mMainFragment)
.commit()
}
}
Fragment
@Inject標(biāo)注其無參構(gòu)造方法
class MainFragment @Inject constructor(): BaseFragment() {
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_main, container, false)
}
}
總結(jié)坑點(diǎn)
- DaggerAppComponent爆紅不要急,Make Project就出來了(如果能順利編譯)
- lateinit 前不要用 private 否則編譯報(bào)錯
- @Inject標(biāo)注其無參構(gòu)造方法 (@Inject constructor()),然后在Module @Binds
- 實(shí)在編譯不過弹谁,再看一下本文 或 看下GitHub上的項(xiàng)目