【问题标题】:Setting Explict Annotation Processor设置显式注释处理器
【发布时间】:2017-08-17 01:01:42
【问题描述】:

我正在尝试将 maven 存储库添加到我的 Android Studio 项目中。 当我进行 Gradle 项目同步时,一切都很好。但是,每当我尝试构建我的 apk 时,都会收到此错误:

Execution failed for task ':app:javaPreCompileDebug'.
> Annotation processors must be explicitly declared now.  The following dependencies on 
the compile classpath are found to contain annotation processor.  Please add them to 
the annotationProcessor configuration.
 - classindex-3.3.jar
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions
.includeCompileClasspath = true to continue with previous behavior.  Note that this 
option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.

错误 404s 中包含 (https://developer.android.com/r/tools/annotation-processor-error-message.html) 的链接,因此它没有帮助。

我已在 android studio 设置中启用注释处理,并将includeCompileClasspath = true 添加到我的注释处理器选项中。我还尝试将 classindexclassindex-3.3classindex-3.3.jar 添加到处理器 FQ 名称,但这也没有帮助。

这些是我在依赖项下添加到默认 build.gradle 的行:

dependencies {
    ...
    compile group: 'com.skadistats', name: 'clarity', version:'2.1.1'
    compile group: 'org.atteo.classindex', name: 'classindex', version:'3.3'
}

最初我只是添加了“清晰”项,因为那是我关心的一项,但我添加了“classindex”条目,希望它能修复它。删除“清晰度”并保留“classindex”给了我完全相同的错误。

我对 gradle/maven 不太熟悉,因此我们将不胜感激。

【问题讨论】:

标签: android maven android-studio gradle annotation-processor


【解决方案1】:

要修复错误,只需将这些依赖项的配置更改为使用 annotationProcessor。如果依赖项包含的组件也需要在编译类路径上,请再次声明该依赖项并使用编译依赖项配置。

例如:

annotationProcessor 'com.jakewharton:butterknife:7.0.1'
compile 'com.jakewharton:butterknife:7.0.1'

此链接详细描述:https://developer.android.com/studio/preview/features/new-android-plugin-migration.html#annotationProcessor_config

为了完整起见,包含相关的 sn-p。

使用注解处理器依赖配置

在 Gradle 的早期版本的 Android 插件中,依赖于 编译类路径被自动添加到处理器 类路径。也就是说,您可以将注释处理器添加到 编译类路径,它会按预期工作。然而,这导致 通过添加大量的对性能产生重大影响 对处理器的不必要依赖。

使用新插件时,注释处理器必须添加到 使用 annotationProcessor 依赖项的处理器类路径 配置,如下图:

依赖{ ... annotationProcessor 'com.google.dagger:dagger-compiler:' }

插件假定一个依赖是一个注解处理器,如果它的 JAR 文件包含以下文件: META- INF/services/javax.annotation.processing.Processor。如果插件 在编译类路径上检测注释处理器,您的构建 失败,您会收到一条错误消息,其中列出了每个注释 编译类路径上的处理器。要修复错误,只需更改 这些依赖项的配置以使用 annotationProcessor。如果 依赖项包括也需要编译的组件 类路径,再次声明该依赖项并使用编译 依赖配置。

【讨论】:

    【解决方案2】:

    我遇到了类似的错误。我按照 Google 链接上的说明进行操作。

    尝试将以下说明添加到您的应用 gradle 文件中:

    defaultConfig {
        applicationId "com.yourdomain.yourapp"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = false
            }
        }
    }
    

    注意->includeCompileClasspath false

    【讨论】:

    • “请注意,此选项已弃用,将来将被删除。”
    • 是的@everyman,但现在这是我们必须处理的选项。
    • 建议不要使用这种方式。请改为参考此答案。 stackoverflow.com/a/44294076/1237141
    • 它适用于我,如果您的annotationProcessor 放入build.gradle lib 模块,则必须将其添加到app 模块的build.gradle 而不是您的lib 模块中。
    【解决方案3】:

    将此代码添加到您的 gradle 应用程序

    defaultConfig{
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath true
            }
        }
    }
    

    我在这里找到了解决方案https://github.com/JakeWharton/butterknife/issues/908

    【讨论】:

      【解决方案4】:

      只需使用注释处理器更新您的黄油刀

      compile 'com.jakewharton:butterknife:8.8.1'
      annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
      

      希望对你有帮助

      【讨论】:

        【解决方案5】:

        将此添加到defaultConfig

        android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true

        【讨论】:

        • 此选项已弃用,将来将被删除。
        【解决方案6】:

        在 build.gradle(module app) 中

        1. 应用插件:

           apply plugin: 'com.jakewharton.butterknife'
          
        2. 在依赖项部分添加以下行:

           annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'
           implementation 'com.jakewharton:butterknife:8.7.0'
          

        在 build.gradle(Project:projectName) 中,在依赖项中添加 classPath,如下所示:

            classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        

        它将解决此问题。 如果没有,则添加 maven:

         maven {
         url 'https://maven.google.com'
         }
        

        【讨论】:

          【解决方案7】:

          如果您对编译类路径有依赖项,其中包含您不需要的注释处理器,您可以通过将以下内容添加到您的 build.gradle 文件来禁用错误检查。请记住,您添加到编译类路径的注释处理器仍未添加到处理器类路径中。

           android {
              ...
              defaultConfig {
                  ...
                  javaCompileOptions {
                      annotationProcessorOptions {
                          includeCompileClasspath false
                      }
                  }
              }
          }
          

          如果您在迁移到新的依赖项解析策略时遇到问题,您可以通过将 includeCompileClasspath 设置为 true 将行为恢复到 Android 插件 2.3.0 的行为。但是,不建议将行为恢复到 2.3.0 版本,并且在以后的更新中将删除这样做的选项。

          查看这里https://developer.android.com/r/tools/annotation-processor-error-message.html了解更多详情

          【讨论】:

            【解决方案8】:

            如果上述答案没有任何效果,请添加以下步骤,特别是 Android Studio 3.0.1 的新更新:

            Android Studio 3.0.1:

            将此添加到依赖项中的 app.gradle 文件中:

            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
            

            【讨论】:

              【解决方案9】:

              在以前版本的插件中,编译类路径的依赖项会自动添加到处理器类路径中。也就是说,您可以将注释处理器添加到编译类路径,它会按预期工作。但是,这会给处理器添加大量不必要的依赖项,从而对性能产生重大影响。

              使用Android插件3.0.0时,必须使用annotationProcessor依赖配置将注解处理器添加到处理器类路径中,如下图:

              dependencies { ... annotationProcessor 'com.google.dagger:dagger-compiler:<version-number>' implementation 'com.google.dagger:dagger-compiler:<version-number>' }

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-05-27
                • 2023-03-04
                • 2012-04-30
                • 2010-12-01
                • 1970-01-01
                相关资源
                最近更新 更多