Java 在 Android Studio 2.2 中启用 Annotation Processors 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38642712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:29:19  来源:igfitidea点击:

enable Annotation Processors option in Android Studio 2.2

javaandroidannotationsandroid-studio-2.2

提问by jonathanrz

I'm trying to use java 8 in my project and for that I added the Hyman compiler.

我正在尝试在我的项目中使用 java 8,为此我添加了 Hyman 编译器。

After enabling Hyman I started having problems with libraries that use Annotation Processing and looking in the web I read that I need android studio 2.2 and com.android.tools.build:gradle:2.2.0-alpha6 to compile libraries that generate code from annotations.

启用 Hyman 后,我开始遇到使用注释处理的库的问题,并在网上查看我读到我需要 android studio 2.2 和 com.android.tools.build:gradle:2.2.0-alpha6 来编译从注释生成代码的库.

I download Android Studio 2.2 preview 6 and converted my project to it. And after that I discovered that the apt gradle plugin is not supported anymore and then I needed to change every dependency that use apt to the use the new annotationProcessor option.

我下载了 Android Studio 2.2 预览版 6 并将我的项目转换为它。之后我发现 apt gradle 插件不再受支持,然后我需要将使用 apt 的每个依赖项更改为使用新的 annotationProcessor 选项。

Ex:

前任:

apt "org.projectlombok:lombok:$rootProject.lombokVersion"

to

annotationProcessor "org.projectlombok:lombok:$rootProject.lombokVersion"

Now if I use "make project" the project is compiled without problems, but if I try to execute it I have errors with the code that should be generated by the annotations.

现在,如果我使用“make project”,则该项目的编译没有问题,但是如果我尝试执行它,则注释生成的代码会出错。

Also when I open the project I receive a warning from the lombok plugin "Annotation processing seems to be disabled for the project". When I open the project settings and go to "Build -> Compiler" I can't find Annotation Processors.

此外,当我打开项目时,我收到来自 lombok 插件的警告“项目似乎禁用了注释处理”。当我打开项目设置并转到“Build -> Compiler”时,我找不到 Annotation Processors。

So, my question is: How can I enable Annotation Processors in Android Studio 2.2? This feature was disabled? If yes, how can I generate the code from annotations?

所以,我的问题是:如何在 Android Studio 2.2 中启用注释处理器?这个功能被禁用了?如果是,如何从注释生成代码?

--EDIT-- I'm making a PullRequest to change the project to compile with Java8, you can check the PR here: https://github.com/jonathanrz/myexpenses-android/pull/57

--EDIT--我正在制作一个 PullRequest 来更改项目以使用 Java8 进行编译,您可以在此处查看 PR:https: //github.com/jonathanrz/myexpenses-android/pull/57

采纳答案by Jacques Koorts

Close the project. In the "Welcome to Android Studio" dialog click "Configure" in the bottom right corner.

关闭项目。在“欢迎使用 Android Studio”对话框中点击右下角的“配置”。

Then,

然后,

Settings > Build, Execution, Deployment > Compiler > Annotation Processors. Tick 'Enable annotation processing'.

设置 > 构建、执行、部署 > 编译器 > 注释处理器。勾选“启用注释处理”。

If that does not work. Delete the project from "Welcome to Android Studio" dialog and open from new.

如果那不起作用。从“欢迎使用 Android Studio”对话框中删除项目并从新打开。

Worked for me.

为我工作。

回答by mtrakal

https://stackoverflow.com/a/38698186/4024146

https://stackoverflow.com/a/38698186/4024146

and after do: File > Invalidate Caches / Restart... > Invalidate and Restart

然后做: File > Invalidate Caches / Restart... > Invalidate and Restart

回答by Bri6ko

Adding to @Jacques Koorts and @mtrakal

添加到@Jacques Koorts 和@mtrakal

If you can't get to the "Welcome to Android Studio" screen. Try File -> Close Projectinstead of clicking the Xicon. Then you'll get the "Welcome to Android Studio" screen and you'll see the gear in the bottom right. Follow the accepted answer after that and possibly the cache invalidation.

如果您无法进入“欢迎使用 Android Studio”屏幕。尝试File -> Close Project而不是单击X图标。然后你会看到“欢迎使用 Android Studio”屏幕,你会看到右下角的齿轮。之后遵循接受的答案,可能是缓存失效。

回答by Bill

  1. Close all your AndroidStudio Project
  2. See enter image description here

  3. Click Configure-->Setting See enter image description here

  1. 关闭所有 AndroidStudio 项目
  2. 在此处输入图片说明

  3. 点击配置-->设置查看 在此处输入图片说明

回答by Neo Tran

  1. Close your project.
  2. Settings > Build, Execution, Deployment > Compiler > Annotation Processors. Check 'Enable annotation processing'.
  3. Open your project.
  4. File > Invalidate Caches / Restart... > Invalidate and Restart
  1. 关闭您的项目。
  2. 设置 > 构建、执行、部署 > 编译器 > 注释处理器。选中“启用注释处理”。
  3. 打开您的项目。
  4. 文件 > 使缓存无效/重新启动... > 使缓存无效并重新启动

Wait for the process completely, then everything will be fine.

等待过程完全,然后一切都会好起来的。

回答by Jamie Dulaney

Open compiler.xml in the .idea folder. I had the following:

打开 .idea 文件夹中的 compiler.xml。我有以下几点:

<annotationProcessing>
  <profile default="true" name="Default" enabled="false">
    <processorPath useClasspath="true" />
  </profile>
</annotationProcessing>

I simply changed enable to true and re-opened project.

我只是将 enable 更改为 true 并重新打开项目。

回答by Tudor

You can enable Annotation Processors without closingyour project in Android Studio 2.3:

您可以在Android Studio 2.3 中启用 Annotation Processors而无需关闭您的项目:

File -> Other Settings -> Default Settings

enter image description here

在此处输入图片说明

Build, Execution, Deployment -> Compiler -> Annotation Processors -> 
Enable annotation processing.

enter image description hereDon't forget to clean, build, invalidate and restart after that.
Cheers!

在此处输入图片说明之后不要忘记清理、构建、失效和重新启动。
干杯!

回答by Chulo

Stuped but worked for me, try to change the library version in my case I upgraded to 1.4.1

Stuped但对我有用,在我升级到1.4.1的情况下尝试更改库版本

回答by KingKongCoder

Sometimes the annotate option will be grayed out if the project is not integrated into version control. So goto VCS->Enable version control integration then voila you will see the annotate option and can see the author name beside the line numbers on the editor.

如果项目未集成到版本控制中,有时注释选项会变灰。所以转到 VCS->Enable version control integration 然后瞧,您将看到 annotate 选项,并且可以在编辑器的行号旁边看到作者姓名。

回答by Fazal Hussain

This Answer for those who face this problem in the future

为将来遇到此问题的人提供的答案

For Kotlin

对于科特林

Add kapt plugin

添加 kapt 插件

apply plugin: 'kotlin-kapt'
implementation 'com.google.dagger:dagger:2.21'
kapt 'com.google.dagger:dagger-compiler:2.21'

For Java

对于 Java

implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'