gpt4 book ai didi

android - Kotlin 中的 Dagger 2 Unresolved reference DaggerXXXSub

转载 作者:太空狗 更新时间:2023-10-29 13:06:55 25 4
gpt4 key购买 nike

我正在尝试在我使用 Kotlin 编写代码的 Android 项目中使用 Dagger 2。我在我的项目中使用了 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>()
}

主 Activity .kt

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

我的应用程序.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 后,项目无法运行。

我的应用程序.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/

关于android - Kotlin 中的 Dagger 2 Unresolved reference DaggerXXXSub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com