【问题标题】:how to enable annotation processorin android studio 3.1.2如何在 android studio 3.1.2 中启用注释处理器
【发布时间】:2018-06-04 17:44:32
【问题描述】:

我正在为我的 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上的匕首实现代码不想在这里重写:https://github.com/codepath/android_guides/issues/331

【问题讨论】:

  • 你能发布你的 build.gradle 文件吗?一级应用
  • @LeviAlbuquerque 完成
  • 查看答案@Moeinh77
  • @Moeinh77 运气好吗?
  • @LeviAlbuquerque 嘿 tnx 询问但不幸的是还没有。我的gradle构建时间也突然变得非常非常慢

标签: android dependency-injection annotations dagger-2


【解决方案1】:

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

apply plugin: 'kotlin-kapt'

而不是使用annotationProcessor 使用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()
 }

 }

【讨论】:

  • 嘿,tnx,但我这样做了,但它没有解决任何其他想法?
  • 通常dagger组件是生成的,但不会自动导入。你检查是否可以在干净构建后按 ctrl+enter 导入它?
  • tnx,我试过了,但仍然没有。我的 android studio gradle 变得非常慢你知道为什么吗?我只是想让匕首工作,现在 gradle 变得如此缓慢,我恢复了一切,但仍然很慢
  • 你写的这段代码给了我这个错误:文件夹 D:\StudioProjects\Volley_Download_KOTLIN\app\build\generated\source\kaptKotlin\debug 文件夹 D:\StudioProjects\Volley_Download_KOTLIN\app\build\generated \source\kaptKotlin\release 3rd-party Gradle 插件可能是原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
相关资源
最近更新 更多