有什么方法可以让 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 sample 在 IntelliJ IDEA 上运行
问题
IntelliJ IDEA 一直告诉我它无法解析生成的 class(在本例中为 DaggerCoffeeApp_Coffee
)。不知道写的代码是否正确有点烦人(尤其是在学习使用 Dagger 2 的时候)。
所有 java class 与 Google sample 相同。这是我的 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 + 左键单击来实现它)?
我终于成功了!
我必须添加 apt
和 idea
插件,所以现在我的 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'
}
这是对我有用的解决方案:
文件 -> 项目结构 ->(select 你的项目在模块列表下) -> 打开 'Dependencies' 选项卡
然后,单击绿色“+”号、select 'JARs or directory' 和 select 'build/classes/main' 文件夹。
另一种解决方案是使用 build.gradle:
中的 'dependencies' 块将包含构建 class 文件的 link 文件夹
您必须在 IntelliJ 中手动启用 注释处理。
来自:设置 --> 构建、执行、部署 --> 编译器 --> 注释处理器 --> 启用注释处理和从项目类路径中获取处理器
然后重建项目,你会在项目中找到生成的类。
请注意,我已在 (java) android 项目中使用此解决方案。
我找到的最简单的方法:
添加 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'
}
为 IntelliJ 启用 Annotation Processing
:转到 Settings
并搜索 Annotation Processors
,选中启用注释处理,如下图所示:
我正在使用 2017.3.3
版的 IntelliJ IDEA、0.14
版的 net.ltgt.apt
插件和 2.14.1
版的 Dagger 以及应用 [= build.gradle
文件中的 15=] 插件(如 Pelocho 的 )我发现我还必须告诉 IntelliJ 在哪里可以找到 Dagger 生成的源代码,如下所示:
apply plugin: 'idea'
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
为了让 Idea 与 Dagger2 和 gradle 一起工作,我必须这样做。
- 打开注释处理,如上面的答案所示。
将以下内容添加到 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'
}
不过,我不知道此解决方案适用的最低版本。
我也遇到了类似的问题,找了半天也没找到原因。
刚推出,结果让我很吃惊。
Intellij 创意 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'
}
上下文
我在 java 开始了一个个人项目,使用 Gradle
作为构建系统,我想使用 Dagger 2 作为 DI。这样做的主要原因是习惯该库并能够在更大的项目中轻松使用它。
我尝试了什么
我已经设法让 Google sample 在 IntelliJ IDEA 上运行
问题
IntelliJ IDEA 一直告诉我它无法解析生成的 class(在本例中为 DaggerCoffeeApp_Coffee
)。不知道写的代码是否正确有点烦人(尤其是在学习使用 Dagger 2 的时候)。
所有 java class 与 Google sample 相同。这是我的 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 + 左键单击来实现它)?
我终于成功了!
我必须添加 apt
和 idea
插件,所以现在我的 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'
}
这是对我有用的解决方案:
文件 -> 项目结构 ->(select 你的项目在模块列表下) -> 打开 'Dependencies' 选项卡
然后,单击绿色“+”号、select 'JARs or directory' 和 select 'build/classes/main' 文件夹。
另一种解决方案是使用 build.gradle:
中的 'dependencies' 块将包含构建 class 文件的 link 文件夹您必须在 IntelliJ 中手动启用 注释处理。
来自:设置 --> 构建、执行、部署 --> 编译器 --> 注释处理器 --> 启用注释处理和从项目类路径中获取处理器
然后重建项目,你会在项目中找到生成的类。
请注意,我已在 (java) android 项目中使用此解决方案。
我找到的最简单的方法:
添加
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' }
为 IntelliJ 启用
Annotation Processing
:转到Settings
并搜索Annotation Processors
,选中启用注释处理,如下图所示:
我正在使用 2017.3.3
版的 IntelliJ IDEA、0.14
版的 net.ltgt.apt
插件和 2.14.1
版的 Dagger 以及应用 [= build.gradle
文件中的 15=] 插件(如 Pelocho 的
apply plugin: 'idea'
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
为了让 Idea 与 Dagger2 和 gradle 一起工作,我必须这样做。
- 打开注释处理,如上面的答案所示。
将以下内容添加到
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'
}
不过,我不知道此解决方案适用的最低版本。
我也遇到了类似的问题,找了半天也没找到原因。
刚推出,结果让我很吃惊。
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'
}