26

Gradle プロジェクトがあり、dagger 2.0 を使用したいと考えています。IntelliJ と gradle を構成してファイルを生成し、IntelliJ にそれらを見つけさせる方法がわかりません。

私のbuild.gradleファイルは次のようになります:

apply plugin: 'java'
apply plugin: 'idea'

version = '1.0'

repositories {
    mavenCentral()
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.12'
    compile 'org.slf4j:slf4j-simple:1.7.12'
    compile 'commons-configuration:commons-configuration:1.10'
    compile 'commons-collections:commons-collections:3.2.1'
    compile 'com.google.dagger:dagger:2.0'
    compile 'com.google.dagger:dagger-compiler:2.0:jar-with-dependencies'
    compile 'com.pi4j:pi4j-distribution:1.1-SNAPSHOT'
}

私のアプリケーションのビルド ディレクトリにDaggerXmlConfigurationComponentは、Dagger が作成するコンポーネントであるファイルが存在します。しかし、クラスが見つからないため、IntelliJ では使用できません。

これは Android アプリケーションではなく、Raspberry Pi 用のアプリケーションです。

4

8 に答える 8

17

IntelliJ の注釈処理を手動で有効にする必要があります。[設定…] → [ビルド、実行、展開] → [コンパイラ] → [注釈プロセッサ] で、[注釈処理を有効にする] と [プロジェクト クラスパスからプロセッサを取得する] をオンにします。

于 2015-09-29T20:42:29.413 に答える
8

私は解決策を見つけました。

https://github.com/tbroyer/gradle-apt-plugin

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

apply plugin: "net.ltgt.apt"

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

さらに、Intellij を使用している場合は、次の構成が推奨されます。

ただし、IntelliJ IDEA で Gradle 統合を使用する場合は、アイデア タスクではなく、注釈処理を手動で有効にする必要があります。設定… → ビルド、実行、デプロイ → コンパイラ → 注釈プロセッサで、注釈処理を有効にし、プロジェクトからプロセッサを取得するにチェックを入れます。クラスパス。Gradle の動作と生成されたファイルの動作を模倣するには、運用ソース ディレクトリとテスト ソース ディレクトリをそれぞれ build/generated/source/apt/main と build/generated/source/apt/test に構成し、生成されたソースを次の基準で保存するように選択できます。コンテンツルート。また、ビルド ディレクトリ全体から Exclude を削除し、 generated/source/apt/main ディレクトリをソースとしてマークする必要がありました。

于 2015-11-10T14:41:01.193 に答える
5

私が知っている最も簡単な方法は、apt-idea プラグインを使用することです

build.gradleファイルでプラグインを有効にするだけです。

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

annotationProcessor次に、注釈プロセッサを構成に追加します。

final DAGGER_VER = '2.16'
dependencies {
    implementation "com.google.dagger:dagger:${DAGGER_VER}"
    annotationProcessor"com.google.dagger:dagger-compiler:${DAGGER_VER}"
}

GitHub で非常に単純なテスト プロジェクトを作成しました: ex.dagger
(IntelliJ 2018.1.4、Gradle 4.7 を使用)

于 2018-05-27T10:57:46.193 に答える
3

私もプラグインを動作させることができなかったので、ステファンの応答に基づいて、動作する次のことを行いましたが、IntelliJ は以前にはなかったグループ モジュールを作成するようです。誰かがこれを引き起こしている原因を知っているなら、私は本当にこれを修正したいと思っています.

apply plugin: 'java'
apply plugin: 'idea'

configurations {
    compileDagger
}

def genPath = new File(buildDir,"generated/source/apt/main" )

task createGenPath << {
    if(!genPath.exists()){
        genPath.mkdirs()
    }
}

compileJava.dependsOn(createGenPath)

compileJava {
    source += genPath
    classpath += configurations.compileDagger
    options.compilerArgs += ['-s', genPath]
}

idea.module {
    sourceDirs += genPath
}

dependencies {
    compileDagger "com.google.dagger:dagger-compiler:${dagger2Version}"
    compile "com.google.dagger:dagger:${dagger2Version}"
}
于 2016-05-12T16:48:47.313 に答える
1

私の場合、問題は短剣で生成されたファイル用に別のモジュールを作成する IDEA でした。モジュールに移動しFile -> Project Structure -> Modulesて削除しprojectname_dagger(赤いマイナスをクリックして)、生成されたソースフォルダーをprojectname_mainクリックAdd Content Rootして選択してモジュールに追加する必要がありました。

プロジェクト内の重複ファイルに関するエラーが発生していたため、何らかの理由で Dagger のファイルを削除し、IDEA に再生成させる必要がありました。

アノテーション プロセッサがオフになっているイベントが動作するようになりました (主に Android プロジェクトにとって重要であると思われます)。

于 2016-11-24T11:11:58.570 に答える