在搜索互联网之后,似乎不是一个类似的问题而且正在吃我。在使用Dagger 2学习依赖注入的过程中,我试图将一个例子从Java转换为Kotlin。该项目在Java中编译得很好,但是使用Kotlin,不喜欢javax.inject.Provider类并且无法构建。
缺少什么? Kotlin对Provider类的使用是否不正确?
以下是Gradle事件日志中的错误:
repositorytrends\custom_implementations\RepoTrendsAppComponent.java:8: error: java.util.Map<java.lang.Class<? extends android.app.Activity>,? extends javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.app.Activity>>> cannot be provided without an @Provides-annotated method.
这是违规文件。内部构造函数的参数(Map)是成功构建的决定因素:
class ActivityInjector
@Inject internal constructor(private val activityInjectors: Map<Class<out Activity>, Provider<AndroidInjector.Factory<out Activity>>>){
private val cache = HashMap<String, AndroidInjector<out Activity>>()
internal fun inject(activity: Activity) {
if (activity !is RepoTrendActivity) {
throw IllegalArgumentException("Activity must extend BaseActivity")
}
val instanceId = activity.getInstanceID
if (cache.containsKey(instanceId)) {
(cache[instanceId] as AndroidInjector<Activity>).inject(activity)
return
}
val injectorFactory = activityInjectors[activity.javaClass]?.get() as AndroidInjector.Factory<Activity>
val injector = injectorFactory.create(activity)
cache.put(instanceId, injector)
injector.inject(activity)
}
internal fun clear(activity: Activity) {
if (activity !is RepoTrendActivity) {
throw IllegalArgumentException("Activity must extend BaseActivity")
}
cache.remove(activity.getInstanceID)
}
companion object {
internal operator fun get(context: Context): ActivityInjector{
return (context.applicationContext as RepoTrendsApp).activityInjector
}
}
}
以下是与Gradle构建错误日志相关的其他类:
@Singleton
@Component(modules = arrayOf(
RepoTrendsAppModule::class
))
interface RepoTrendsAppComponent {
fun inject(repoTrendsApp: RepoTrendsApp)
}
自定义应用程序文件:
class RepoTrendsApp: Application(){
@Inject lateinit var activityInjector: ActivityInjector
}
Build.gradle for good measure:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
defaultConfig {
applicationId 'com.inviscidlabs.repositorytrends'
minSdkVersion 21
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'
}
}
productFlavors {
}
kapt {
generateStubs = true
}
}
dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
implementation "com.android.support:design:$supportLibraryVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-moshi:$retrofitVersion"
implementation "com.squareup.moshi:moshi:$moshiVersion"
kapt "com.ryanharter.auto.value:auto-value-moshi:$autoValueMoshiVersion"
compileOnly "com.ryanharter.auto.value:auto-value-moshi-annotations:$autoValueMoshiVersion"
compileOnly "com.google.auto.value:auto-value:$autoValueVersion"
annotationProcessor "com.google.auto.value:auto-value:$autoValueVersion"
implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"
implementation "com.jakewharton.rxrelay2:rxrelay:$rxRelayVersion"
//Drop in replacement for Fragments
implementation "com.bluelinelabs:conductor:$conductorVersion"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}
根据要求, RepoTrendsAppModule :
import android.app.Application
import android.content.Context
import dagger.Module
import dagger.Provides
@Module
class RepoTrendsAppModule(val application: Application){
@Provides
fun provideApplicationContext(): Context = application
}
答案 0 :(得分:1)
您不需要ActivityInjector
课程。修改您的应用程序类,如下所示:
class RepoTrendsApp: Application(), HasActivityInjector {
@Inject
internal lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
override fun onCreate() {
super.onCreate()
DaggerRepoTrendsAppComponent.builder()
.repoTrendsAppModule(RepoTrendsAppModule(this))
.build()
.inject(this)
}
override fun activityInjector(): AndroidInjector<Activity>? {
return dispatchingActivityInjector
}
}
以及您的组件:
@Singleton
@Component(modules = arrayOf(
AndroidSupportInjectionModule::class,
RepoTrendsAppModule::class
))
interface RepoTrendsAppComponent : AndroidInjector<RepoTrendsApp>
答案 1 :(得分:0)
听起来有些模块上缺少@Provides
注释。以下是Kotlin / Android / Dagger 2模块定义的示例:
@Module
class MyAndroidModule(private val application: Application) {
@Provides
@Singleton
@CustomInjectionAnnotation
fun provideApplicationContext(): Context = application
@Provides
@Singleton
fun provideLocationManager(): LocationManager =
application.getSystemService(Context.LOCATION_SERVICE) as LocationManager
@Provides
@Singleton
@Named("version")
fun provideVersionString(): String = "beta"
}