有没有办法让 IntelliJ IDEA 在 Java 项目中识别 Dagger 2 生成的类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33444356/
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-11-02 21:40:43  来源:igfitidea点击:

Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?

javaintellij-ideagradledagger-2

提问by Pelocho

Context

语境

I have started a personal project in java with Gradleas the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects.

我已经用 Java 启动了一个个人项目Gradle作为构建系统,我想使用 Dagger 2 作为 DI。这样做的主要原因是习惯该库并能够在更大的项目中轻松使用它。

What have I tried

我试过什么

I've managed to make the Google sampleruns on IntelliJ IDEA

我已经设法让Google 示例在 IntelliJ IDEA 上运行

Problem

问题

IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee). It's a bit annoying not to know if the written code is correct (specially when you are learning to use Dagger 2).

IntelliJ IDEA 一直告诉我它无法解析生成的类(在这种情况下DaggerCoffeeApp_Coffee)。不知道编写的代码是否正确有点烦人(特别是在您学习使用 Dagger 2 时)。

All java classes are the same as the Google sample. Here is my build.gradlefile:

所有 java 类都与Google 示例相同。这是我的build.gradle文件:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'com.google.dagger:dagger:2.0.1'
    compile 'com.google.dagger:dagger-compiler:2.0.1'
}

Question

问题

Is there any way to make IntelliJ IDEA recognize DaggerCoffeeApp_Coffeeas a generated class (and so make it possible to go to its implementation by `ctrl + left click)?

有什么方法可以让 IntelliJ IDEA 识别DaggerCoffeeApp_Coffee为生成的类(因此可以通过`ctrl + 左键单击来实现它)?

采纳答案by Pelocho

Finally I made it!

我终于成功了!

I had to add the aptand the ideaplugin so right now my build.gradlefile look like this:

我必须添加aptidea插件,所以现在我的build.gradle文件看起来像这样:

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.4"
    }
}

apply plugin: "net.ltgt.apt"
apply plugin: 'java'
apply plugin: 'idea'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
}

回答by Hani

Simplest way I found:

我发现的最简单的方法:

  1. Add ideaplugin and add Dagger2 dependency like below:

    plugins {
        id "net.ltgt.apt" version "0.10"
    }
    
    apply plugin: 'java'
    apply plugin: 'idea'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    
        compile 'com.google.dagger:dagger:2.11'
        apt 'com.google.dagger:dagger-compiler:2.11'
    }
    
  2. Turn on Annotation Processingfor IntelliJ: Go to Settingsand search for Annotation Processors, check Enable annotation processing like below image:

  1. 添加idea插件并添加 Dagger2 依赖项,如下所示:

    plugins {
        id "net.ltgt.apt" version "0.10"
    }
    
    apply plugin: 'java'
    apply plugin: 'idea'
    
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    
        compile 'com.google.dagger:dagger:2.11'
        apt 'com.google.dagger:dagger-compiler:2.11'
    }
    
  2. 打开Annotation Processing的IntelliJ:去Settings和搜索Annotation Processors,选中启用注释像下面的图像处理:

enter image description here

在此处输入图片说明

回答by db80

you must manually enable the annotation processingin IntelliJ.

您必须在 IntelliJ 中手动启用注释处理

From: Settings --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> Enable annotation processingand Obtain processors from project classpath

从:设置 --> 构建、执行、部署 --> 编译器 --> 注释处理器 -->启用注释处理从项目类路径中获取处理器

then rebuild the project and you will find the generated classes in the project.

然后重建项目,你会在项目中找到生成的类。

Please note that I have used this solution in a (java) androidproject.

请注意,我在 (java) android项目中使用了此解决方案。

回答by Adil Hussain

I'm using version 2017.3.3of IntelliJ IDEA, version 0.14of the net.ltgt.aptplugin and version 2.14.1of Dagger and as well as applying the ideaplugin in the build.gradlefile (as in Pelocho's answer) I found I also had to tell IntelliJ where it can find the sources generated by Dagger, as follows:

我正在使用2017.3.3IntelliJ IDEA 版本0.14net.ltgt.apt插件版本2.14.1和 Dagger版本以及ideabuild.gradle文件中应用插件(如 Pelocho 的回答)我发现我还必须告诉 IntelliJ 在哪里可以找到 Dagger 生成的源, 如下:

apply plugin: 'idea'
idea {
    module {
        sourceDirs += file("$buildDir/generated/source/apt/main")
        testSourceDirs += file("$buildDir/generated/source/apt/test")
    }
}

回答by nazmul idris

This is what I had to do in order to get Idea to work with Dagger2 and gradle.

为了让 Idea 与 Dagger2 和 gradle 一起工作,我必须这样做。

  1. Turn on annotation processing as shown in the answers above.
  2. Add the following to the build.gradlefile in order for Idea to see the generated classes as sources.

    sourceDirs += file("$projectDir/out/production/classes/generated/")
    
  1. 打开注释处理,如上面的答案所示。
  2. 将以下内容添加到build.gradle文件中,以便 Idea 将生成的类视为源。

    sourceDirs += file("$projectDir/out/production/classes/generated/")
    

Here's the full listing of my build.gradle

这是我的完整列表 build.gradle

plugins {
    id 'java'
    id 'idea'
    id "net.ltgt.apt" version "0.10"
}

idea {
    module {
        sourceDirs += file("$projectDir/out/production/classes/generated/")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.dagger:dagger:2.16'
    apt 'com.google.dagger:dagger-compiler:2.16'
}

sourceCompatibility = 1.8

Also, I had to add the following gradle task (to my build.gradle file) to clear out my outdirectory. When I moved some files around and Dagger2 regenerated the source files, the outdirectory wasn't being cleared out :(. I also included this task in my run configuration, so that it gets triggered before I rebuild my project.

此外,我必须添加以下 gradle 任务(到我的 build.gradle 文件)来清除我的out目录。当我移动一些文件并且 Dagger2 重新生成源文件时,该out目录没有被清除:(。我还在我的运行配置中包含了这个任务,以便在我重建我的项目之前触发它。

task clearOutFolder(type: Delete) {
    delete 'out'
}

回答by Luka

Here's the solution that worked for me:

这是对我有用的解决方案:

File -> Project Structure -> (select your project under list of modules) -> Open 'Dependencies' tab

文件 -> 项目结构 ->(在模块列表下选择您的项目)-> 打开“依赖项”选项卡

Then, click on green '+' sign, select 'JARs or directory' and select 'build/classes/main' folder.

然后,点击绿色的“+”号,选择“JARs or directory”并选择“build/classes/main”文件夹。

Another solution would be to link folder with build class files using 'dependencies' block inside build.gradle:

另一种解决方案是使用 build.gradle 中的“dependencies”块将文件夹与构建类文件链接起来:

https://stackoverflow.com/a/22769015/5761849

https://stackoverflow.com/a/22769015/5761849

回答by Franko Leon Tokali?

Using IntelliJ IDEA 2019.1 and Gradle 5.4.1, this seems to be enough:

使用 IntelliJ IDEA 2019.1 和 Gradle 5.4.1,这似乎就足够了:

plugins {
    id 'java'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'

    implementation 'com.google.dagger:dagger:2.23.1'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
}

I don't know the minimal versions for which this solution works, though.

不过,我不知道此解决方案适用的最小版本。

回答by Aska

I had a similar problem, I could not find out the cause for a long time.

我也遇到了类似的问题,找了半天也没找到原因。

Just launched and the result surprised me. IDE displays an errorIntellij Idea 2018.3.6 - build.gradle:

刚刚启动,结果让我感到惊讶。 IDE 显示错误Intellij Idea 2018.3.6 - build.gradle

plugins {
    id "java"
}
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'com.google.dagger:dagger:2.11'
    apt 'com.google.dagger:dagger-compiler:2.11'
}