简体   繁体   English

有什么方法可以让 IntelliJ IDEA 在 Java 项目中识别 Dagger 2 生成的类?

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

Context语境

I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI.我已经在 java 中开始了一个个人项目,其中Gradle作为构建系统,我想使用 Dagger 2 作为 DI。 The main reason of doing that is to get used to that library and be able to use it easily in bigger projects.这样做的主要原因是习惯该库并能够在更大的项目中轻松使用它。

What have I tried我试过什么

I've managed to make the Google sample runs on IntelliJ IDEA我已经设法让Google 示例在 IntelliJ IDEA 上运行

Problem问题

IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee ). IntelliJ IDEA 一直告诉我它无法解析生成的 class (在本例中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).不知道编写的代码是否正确有点烦人(特别是当您学习使用 Dagger 2 时)。

All java classes are the same as the Google sample .所有 java 类都与Google 示例相同。 Here is my build.gradle file:这是我的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_Coffee as a generated class (and so make it possible to go to its implementation by `ctrl + left click)?有什么方法可以让 IntelliJ IDEA 将DaggerCoffeeApp_Coffee识别为生成的 class (因此可以通过 `ctrl + 左键单击实现 go)?

Finally I made it! 终于我做到了!

I had to add the apt and the idea plugin so right now my build.gradle file 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'
}

Simplest way I found: 我发现的最简单方法:

  1. Add idea plugin and add Dagger2 dependency like below: 添加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. Turn on Annotation Processing for IntelliJ: Go to Settings and search for Annotation Processors , check Enable annotation processing like below image: 打开IntelliJ的Annotation Processing :转到Settings并搜索“ Annotation Processors ,选中启用注释处理,如下图所示:

在此处输入图片说明

you must manually enable the annotation processing in IntelliJ. 您必须在IntelliJ中手动启用注释处理

From: Settings --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> Enable annotation processing and 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) android project. 请注意,我已经在(java) android项目中使用了此解决方案。

I'm using version 2017.3.3 of IntelliJ IDEA, version 0.14 of the net.ltgt.apt plugin and version 2.14.1 of Dagger and as well as applying the idea plugin in the build.gradle file (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.3的IntelliJ IDEA的,版本0.14中的net.ltgt.apt插件和版本2.14.1匕首和以及应用idea在插件build.gradle文件(如Pelocho的答案 )我发现我还必须告诉IntelliJ在哪里可以找到Dagger生成的源,如下所示:

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

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. 然后,单击绿色的“ +”号,选择“ JAR或目录”,然后选择“ 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

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.gradle file in order for Idea to see the generated classes as sources. 将以下内容添加到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 out directory. 另外,我必须添加以下gradle任务(到我的build.gradle文件中)以清除我的out目录。 When I moved some files around and Dagger2 regenerated the source files, the out directory wasn't being cleared out :(. I also included this task in my run configuration, so that it gets triggered before I rebuild my project. 当我移动一些文件并且Dagger2重新生成源文件时, out目录没有被清除:(。我也在运行配置中包括了此任务,以便在重建项目之前触发它。

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

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. 不过,我不知道此解决方案适用的最低版本。

I had a similar problem, I could not find out the cause for a long time. 我有一个类似的问题,我很长一段时间都找不到原因。

Just launched and the result surprised me. 刚推出,结果令我惊讶。 IDE显示错误 Intellij Idea 2018.3.6 - build.gradle : 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'
}

The following worked for me on IntelliJ 2021.3.3 (UE)以下在 IntelliJ 2021.3.3 (UE) 上为我工作

plugins {
    id 'java'
    id 'idea'
    id("com.github.johnrengelman.shadow") version "7.1.2"
}

idea {
    module {
        sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main")
        testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test")
    }
}

group 'com.codigomorsa'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor 'com.google.dagger:dagger-compiler:2.44'
    implementation 'com.google.code.gson:gson:2.9.1'
    implementation 'com.google.dagger:dagger:2.44'
    testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.44'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

test {
    useJUnitPlatform()
}

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

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