匕首2在Kotlin未解决的参考DaggerXXXSub
我试图在我的Android项目中使用Dagger 2,我在Kotlin编码。 我在我的项目中使用了Dagger 2,就像Dagger 2 Android中的例子。
应用程序/的build.gradle
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' }
ApplicationSub.kt
@Singleton @Component(modules = arrayOf(ActivityModule::class)) interface ApplicationSub { fun inject(app: MyApplication) }
ActivityModule.kt
@Module(subcomponents = arrayOf(MainActivitySub::class)) abstract class ActivityModule { @Binds @IntoMap @ActivityKey(MainActivity::class) abstract fun bindMainActivity(builder: MainActivitySub.Builder): AndroidInjector.Factory<out Activity> }
MainActivitySub.kt
@Subcomponent interface MainActivitySub : AndroidInjector<MainActivity> { @Subcomponent.Builder abstract class Builder : AndroidInjector.Builder<MainActivity>() }
MainActivity.kt
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
MyApplication.java
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后,项目无法运行。
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中 ,感谢您的帮助!
在你的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/