繁体   English   中英

设置显式注释处理器

[英]Setting Explict Annotation Processor

我正在尝试将 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 不太熟悉,所以任何帮助将不胜感激。

要修复错误,只需更改这些依赖项的配置以使用 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

包含相关片段以确保完整性。

使用注解处理器依赖配置

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

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

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

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

我也有类似的错误。 我按照 Google 链接上的说明进行操作,并且可以正常工作。

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

defaultConfig {
    applicationId "com.yourdomain.yourapp"
    minSdkVersion 17
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"

    javaCompileOptions {
        annotationProcessorOptions {
            includeCompileClasspath = false
        }
    }
}

注意 -> includeCompileClasspath false

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

defaultConfig{
    javaCompileOptions {
        annotationProcessorOptions {
            includeCompileClasspath true
        }
    }
}

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

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

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

我希望它对你有帮助

defaultConfig添加这个

android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true

在 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'
 }

如果您对包含不需要的注释处理器的编译类路径有依赖性,则可以通过将以下内容添加到 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

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

安卓工作室 3.0.1:

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM