• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    kotlin 에서 data binding 및 dagger2 사용하기
    Last Modify : 20 June, 2018(05:41)
    현재 상태 android studio 3.1.3 ext.kotlin_version = '1.2.30' classpath 'com.android.tools.build:gradle:3.1.3' app level build.gradle 에는 apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' android { // .... dataBinding { enabled = true } } dependencies { // ... kapt "android.arch.lifecycle:compiler:1.1.1" kapt 'com.android.databinding:compiler:3.1.3' implementation "com.google.dagger:dagger:$dagger2" kapt "com.google.dagger:dagger-compiler:$dagger2" implementation "com.google.dagger:dagger-android:$dagger2" implementation "com.google.dagger:dagger-android-support:$dagger2" kapt "com.google.dagger:dagger-android-processor:$dagger2" } 상황에서 AppComponent 의 경우 @Singleton @Component( modules = arrayOf( AndroidSupportInjectionModule::class, AppModule::class, ActivityModule::class )) interface AppComponent { @Component.Builder interface Builder { @BindsInstance fun application(app: Application): AppComponent.Builder fun build(): AppComponent } fun inject(app: MainApp) } 식으로 추가 했을때 모듈은 아래와 같이 구현할 수 있다. @Module object AppModule { } @Module object MainActivityModule { @JvmStatic @Provides fun provideWeakActivity(activity: MainActivity): WeakReference<FragmentActivity> = WeakReference(activity) @JvmStatic @Provides fun provideWindowService(): WindowService = HoneMobile.get().window() } @Module abstract class ActivityModule { @ContributesAndroidInjector(modules = arrayOf(MainActivityModule::class)) abstract fun mainActivityInjector(): MainActivity } 주의할 점은 static 형태가 필요할 경우 @JvmStatic 을 붙여주는걸 잊지 말자 .

    Comment


    입력하3 1532187301



    Locations of visitors to this page