Kotlin 踩坑日记(一)Kotlin Dagger2 配置使用

配置

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了。

  1. 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值