我正在尝试在我的Android项目中使用Dagger 2,我在Kotlin中进行编码。我在我的项目中使用了Dagger 2,就像Dagger 2 Android中的示例一样。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.wj.kotlintest"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.google.dagger:dagger-android-support:2.11'
kapt 'com.google.dagger:dagger-compiler:2.11'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
@Singleton
@Component(modules = arrayOf(ActivityModule::class))
interface ApplicationSub {
fun inject(app: MyApplication)
}
@Module(subcomponents = arrayOf(MainActivitySub::class))
abstract class ActivityModule {
@Binds
@IntoMap
@ActivityKey(MainActivity::class)
abstract fun bindMainActivity(builder: MainActivitySub.Builder): AndroidInjector.Factory<out Activity>
}
@Subcomponent
interface MainActivitySub : AndroidInjector<MainActivity> {
@Subcomponent.Builder
abstract class Builder : AndroidInjector.Builder<MainActivity>()
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
public class MyApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> activityInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerApplicationSub
.create()
.inject(this);
}
@Override
public AndroidInjector<Activity> activityInjector() {
return activityInjector;
}
}
我的代码是这样的,它可以运行,但在我尝试将MyApplication.java转换为MyApplication.kt后,项目无法运行。
class MyApplication: Application(), HasActivityInjector{
@Inject
lateinit var activityInjector: DispatchingAndroidInjector<Activity>
override fun onCreate() {
super.onCreate()
DaggerApplicationSub
.builder()
.build()
.inject(this)
}
override fun activityInjector(): AndroidInjector<Activity> {
return activityInjector
}
}
我可以构建成功项目,文件DaggerApplicationSub存在,但是当我尝试运行这个项目时,我得到了编译错误:
Error:(5, 49) Unresolved reference: DaggerApplicationSub
Error:(24, 9) Unresolved reference: DaggerApplicationSub
我不知道如何解决这个问题,我的所有代码都在KotlinTest,谢谢你的帮助!
答案 0 :(得分:2)
在您的gradle中添加以下内容
kapt {
generateStubs = true
}
dependencies {
kapt 'com.google.dagger:dagger-compiler:2.8'
compile 'com.google.dagger:dagger:2.8'
}
有关generateStubs的更多信息,请查看此链接https://blog.jetbrains.com/kotlin/2015/06/better-annotation-processing-supporting-stubs-in-kapt/