繁体   English   中英

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

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

语境

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

我试过什么

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

问题

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

所有 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'
}

问题

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

终于我做到了!

我必须添加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'
}

我发现的最简单方法:

  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. 打开IntelliJ的Annotation Processing :转到Settings并搜索“ Annotation Processors ,选中启用注释处理,如下图所示:

在此处输入图片说明

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

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

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

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

我使用的版本, 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")
    }
}

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

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

然后,单击绿色的“ +”号,选择“ JAR或目录”,然后选择“ build / classes / main”文件夹。

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

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

这是我要使Idea与Dagger2和gradle一起工作所要做的。

  1. 如上面的答案所示,打开注释处理。
  2. 将以下内容添加到build.gradle文件中,以便Idea可以将生成的类视为源。

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

这是我的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

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

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

使用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'
}

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

我有一个类似的问题,我很长一段时间都找不到原因。

刚推出,结果令我惊讶。 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'
}

以下在 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