我正在躲起来学习 Dagger 2,理论上这似乎并不难,但实际上我已经花了 2 天时间让项目启动并运行,但没有任何结果。
@Module
class ActivityModule {
@Provides
@NonNull
@Singleton
fun getActivitys() : TestClass = TestClass()
}
@Module
class AppModule(val context: Context) {
@Provides
@Singleton
fun getContexts() : Context = context
}
@Component(modules = [(ActivityModule::class), (AppModule::class)])
@Singleton
interface AppComponent {
fun inject(mainActivity: MainActivity)
}
应用程序
class App : Application() {
lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = buildComponent()
}
fun getComponent() : AppComponent = appComponent
private fun buildComponent(): AppComponent {
return DaggerAppComponent.builder()
.appModule(AppModule(this))
.activityBindgsModule(ActivityModule())
.build()
}
}
主要活动
class MainActivity : AppCompatActivity() {
@Inject lateinit var testClass: TestClass
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
App().getComponent().inject(this)
Log.d("MAIN", testClass.getString())
}
}
我在清单上写了
<application
android:name=".App"
...
我在 Build 中注册了这样的依赖项,尝试更新,它没有帮助。
compile "com.google.dagger:dagger:2.11"
compile "com.google.dagger:dagger-android:2.11"
compile "com.google.dagger:dagger-android-support:2.11"
kapt "com.google.dagger:dagger-compiler:2.11"
kapt "com.google.dagger:dagger-android-processor:2.11"
当我在模拟器上运行项目时,在构建期间我在 Messenger 部分收到错误
Error:(25, 17) Unresolved reference: DaggerAppComponent
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
但是如果我从 GitHub 链接下载一个现成的示例到这个项目,一切都会编译,但是一旦我开始一个新项目,我就会出错。我写的这个例子将再写一篇文章。
更新
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "info.android_developer_community.dagerss"
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'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
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:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation "com.google.dagger:dagger:2.14"
implementation "com.google.dagger:dagger-android:2.14"
implementation "com.google.dagger:dagger-android-support:2.14"
kapt "com.google.dagger:dagger-android-processor:2.14"
kapt "com.google.dagger:dagger-compiler:2.14"
kapt "com.google.dagger:dagger-android-support:2.14"
}
首先:
将创建该类的一个新实例
App,您需要获取一个现有实例。它是这样完成的:在您的情况下,最后一行将如下所示:
嗯,其次:
如您所见,该错误表明它
DaggerAppComponent不被称为类,因此未导入。可以尝试自己注册导入,需要指定你AppComponent拥有的包,只需在类名后面加上“Dagger”二字即可(这个类会在编译过程中生成)升级版:
必须添加
apply plugin: 'kotlin-kapt'到文件的开头。您还需要添加为了
android让 Kotlin 生成“存根”