请帮助我将Dagger 2连接到Kotlin。 我的应用程序构建gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "mobile.socialboards.com"
minSdkVersion 21
targetSdkVersion 28
versionCode 19
versionName '1.0'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
configurations {
cleanedAnnotations
compile.exclude group: 'org.jetbrains' , module:'annotations'
}
productFlavors {}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
freeCompilerArgs = ['-Xjvm-default=enable']
}
lintOptions {
abortOnError false
}
}
kapt {
generateStubs = true
}
dependencies {
...
implementation 'com.google.dagger:dagger-android:2.14.1'
implementation 'com.google.dagger:dagger-android-support:2.14.1' // if you use the support libraries
implementation "com.google.dagger:dagger:2.14.1"
kapt 'com.google.dagger:dagger-android-processor:2.12'
kapt "com.google.dagger:dagger-compiler:2.14.1"
compileOnly "org.glassfish:javax.annotation:3.1.1"
}
apply plugin: 'com.google.gms.google-services'
我先做sуnc,然后重建,但是
Android Studio在我的Application kotlin类中找不到DaggerAppComponent
。我研究了很多文章-仍然不明白有什么问题。请帮忙!
答案 0 :(得分:0)
我已经实现了完整的MVVM(Rx,Dagger,Kotlin),而匕首只需要这两个:
implementation 'com.google.dagger:dagger:2.12'
kapt 'com.google.dagger:dagger-compiler:2.12'
有什么让我知道的!
答案 1 :(得分:0)
非常简单,您需要创建ApplicationComponent
,如下所示:
@Singleton
@Component(modules = [
ApplicationModule::class,
])
interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): ApplicationComponent
}
}