Kotlin 踩坑日记(一)Kotlin Dagger2 配置使用
2017-05-24 17:24
393 查看
配置
build.gradle
apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' ... dependencies { ... //dagger2 compile rootProject.ext.dependencies["dagger"] kapt rootProject.ext.apt["dagger-compiler"] //dagger2 android 一个dagger2 关于Android的增强库 可选项 compile rootProject.ext.dependencies["dagger-android"] //可选项 compile rootProject.ext.dependencies["dagger-android-support"] //可选项 kapt rootProject.ext.apt["dagger-android-processor"] }
AppComponent
@Singleton @Component(modules = arrayOf(AppModule::class)) interface AppComponent { fun inject(app: BaseApplication) }
AppModule
@Module class AppModule(val app: Application) { @Provides @Singleton fun provideApplication() = app }
Application
class BaseApplication : Application() { override fun onCreate() { super.onCreate() initApplication() DaggerCoreComponent .builder() .coreModule(CoreModule(this)) .build(); } }
以上配置完成,就可以愉快的在Kotlin中使用Dagger2了。
坑
Circular dependency between the following tasks在使用Kotlin 1.1.2-4 plugin 时,使用kapt,会出现循环依赖的错误,这个错误仅需要将 Kotlin 1.1.2-4降级到Kotlin 1.1.2-2版本即可。
根目录下的build.gradle
buildscript { //此处降级 ext.kotlin_version = '1.1.2-2' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } apply plugin: 'kotlin' allprojects { repositories { jcenter() } } repositories { mavenCentral() }
感谢StackOverFlow
http://stackoverflow.com/questions/44035504/how-to-use-data-binding-and-kotlin-in-android-studio-3-0-0
相关文章推荐
- 详解Kotlin 中使用和配置 Dagger2
- MOSS 2007应用日记(17)――如何使用和配置回收站
- 君正4750开发板使用日记5-WINCE的开发配置
- Kotlin 踩坑日记(四) Maven Google 仓库 gradle 使用
- android studio 使用kotlin编写architecture componets的配置问题
- 在kotlin-MVP使用dagger2(v2.11+)
- 企业库(配置管理应用程序块)使用日记
- kotlin+butterknife 使用配置
- Kotlin配置与使用
- Kotlin 踩坑日记(二) dagger2 Java8 的支持错误
- Kotlin中使用Dagger2 可能导致错误"Dagger does not support injection into private fields"
- 实操日记:Linux系统rsync配置和使用
- Android studio中Kotlin配置和使用的图文详解
- Dagger2在Android studio中的配置与简单使用
- kotlin和DataBinding一起使用踩坑日记
- 君正4750开发板使用日记4-QT Embedded、Tslib、QT for VS2008的配置与编译
- Dagger2在Android studio中的配置与简单使用
- 使用Kotlin开发Android 创建工程与配置
- AndroidStudio配置使用Kotlin
- 配置kotlin自带的编译器,并使用kotlinc、kotlin命令