首页 > 解决方案 > 如何在 android studio 3.1.2 中启用注释处理器

问题描述

我正在为我的 android 应用程序使用 dagger 2 库,但它不会生成 Daggercomponent 类,所以我读到我应该启用注释处理器,但我在 android studio 3.1.2 的任何地方都找不到它,它不在设置中我也添加了这个gradle的代码并没有帮助

javaCompileOptions{
        annotationProcessorOptions {
            includeCompileClasspath true
        }
    }

请注意我说的是 3.1.2 版本。在以前的版本中,注释处理器在菜单中,但我在这个版本中找不到它

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.example.moein.volley_download_kotlin"
    minSdkVersion 21
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
    javaCompileOptions{
        annotationProcessorOptions {
            includeCompileClasspath true
        }
    }
 }
 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
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.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-             core:3.0.2'

implementation 'com.tonyodev.fetch2downloaders:fetch2downloaders:2.0.0-RC21'
implementation 'com.android.volley:volley:1.1.0'

implementation 'com.google.dagger:dagger:2.13'
annotationProcessor 'com.google.dagger:dagger-compiler:2.13'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.13'
}

(我在 github 上的 dagger 实现代码不想在这里重写:https ://github.com/codepath/android_guides/issues/331 )

标签: androiddependency-injectionannotationsdagger-2

解决方案


如果您在项目中使用 kotlin,则需要使用 kotlin 注释处理器。将其添加到 gradle 文件的开头:

apply plugin: 'kotlin-kapt'

而不是使用annotationProcessoruse kapt

kapt 'com.google.dagger:dagger-compiler:2.13'
kapt 'com.google.dagger:dagger-android-processor:2.13'

我正在检查你的匕首设置,这个模块似乎有点不对劲:

@Module
  class globals {

  lateinit var volleyinstance:VolleyController
  fun golobals(volley:VolleyController){
    volleyinstance=volley

 }

@Provides
@Singleton
fun provideVolley():VolleyController{
    return volleyinstance
 }

}

我没有看到您的 volleyController 在任何其他模块中创建。也许你想做类似的事情:

@Module
  class globals {

@Provides
@Singleton
fun provideVolley():VolleyController{
    return VolleyController()
 }

 }

推荐阅读