このページでは、アノテーション プロセッサをプロジェクトの依存関係として追加して構成する方法について詳しく説明します。アノテーション プロセッサの詳細については、依存関係を構成するのエントリをご覧ください。
アノテーション プロセッサをコンパイル クラスパスに追加すると、次のようなエラー メッセージが表示されます。
Error: Annotation processors must be explicitly declared now.
このエラーを解決するには、annotationProcessor
を使用して依存関係を構成することにより、プロジェクトにアノテーション プロセッサを追加します。次に例を示します。
dependencies {
// Adds libraries defining annotations to only the compile classpath.
compileOnly("com.google.dagger:dagger:version-number ")
// Adds the annotation processor dependency to the annotation processor classpath.
annotationProcessor("com.google.dagger:dagger-compiler:version-number ")
}
dependencies {
// Adds libraries defining annotations to only the compile classpath.
compileOnly 'com.google.dagger:dagger:version-number '
// Adds the annotation processor dependency to the annotation processor classpath.
annotationProcessor 'com.google.dagger:dagger-compiler:version-number '
}
注: Android Plugin for Gradle 3.0.0 以降、android-apt
プラグインはサポートが終了しました。
アノテーション プロセッサに引数を渡す
アノテーション プロセッサに引数を渡す必要がある場合は、モジュールのビルド構成で AnnotationProcessorOptions
ブロックを使用します。たとえば、プリミティブ データ型を Key-Value ペアとして渡す場合は、次のように argument
プロパティを使用できます。
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments += mapOf("key1" to "value1",
"key2" to "value2")
}
}
}
}
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
argument 'key1', 'value1'
argument 'key2', 'value2'
}
}
}
}
ただし、Android Gradle プラグイン 3.2.0 以降を使用している場合は、Gradle の CommandLineArgumentProvider
インターフェースを使用して、ファイルまたはディレクトリを表すプロセッサ引数を渡す必要があります。
アプリ デベロッパーやアノテーション プロセッサの作成者は CommandLineArgumentProvider
を使用することで、増分ビルド プロパティ型のアノテーションを各引数に適用して、増分ビルドとキャッシュに保存されたクリーンビルドの正確性やパフォーマンスを改善できます。
たとえば、次のクラスは CommandLineArgumentProvider
を実装し、プロセッサの各引数にアノテーションを追加しています。
class MyArgsProvider(
// Annotates each directory as either an input or output for the
// annotation processor.
@get:InputFiles
// Using this annotation helps Gradle determine which part of the file path
// should be considered during up-to-date checks.
@get:PathSensitive(PathSensitivity.RELATIVE)
val inputDir: FileCollection,
@get:OutputDirectory
val outputDir: File
) : CommandLineArgumentProvider {
// Specifies each directory as a command line argument for the processor.
// The Android plugin uses this method to pass the arguments to the
// annotation processor.
override fun asArguments(): Iterable<String> {
// Use the form '-Akey[=value]' to pass your options to the Java compiler.
return listOf("-AinputDir=${inputDir.singleFile.absolutePath}",
"-AoutputDir=${outputDir.absolutePath}")
}
}
android {...}
class MyArgsProvider implements CommandLineArgumentProvider {
// Annotates each directory as either an input or output for the
// annotation processor.
@InputFiles
// Using this annotation helps Gradle determine which part of the file path
// should be considered during up-to-date checks.
@PathSensitive(PathSensitivity.RELATIVE)
FileCollection inputDir
@OutputDirectory
File outputDir
// The class constructor sets the paths for the input and output directories.
MyArgsProvider(FileCollection input, File output) {
inputDir = input
outputDir = output
}
// Specifies each directory as a command line argument for the processor.
// The Android plugin uses this method to pass the arguments to the
// annotation processor.
@Override
Iterable<String> asArguments() {
// Use the form '-Akey[=value]' to pass your options to the Java compiler.
["-AinputDir=${inputDir.singleFile.absolutePath}",
"-AoutputDir=${outputDir.absolutePath}"]
}
}
android {...}
CommandLineArgumentProvider
を実装するクラスを定義したら、インスタンスを作成して、annotationProcessorOptions.compilerArgumentProvider
メソッドで Android プラグインに渡す必要があります。次に例を示します。
// This is in your module's build.gradle file.
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
// Creates a new MyArgsProvider object, specifies the input and
// output paths for the constructor, and passes the object
// to the Android plugin.
compilerArgumentProvider(MyArgsProvider(files("input/path"),
file("output/path")))
}
}
}
}
// This is in your module's build.gradle file.
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
// Creates a new MyArgsProvider object, specifies the input and
// output paths for the constructor, and passes the object
// to the Android plugin.
compilerArgumentProvider new MyArgsProvider(files("input/path"),
new File("output/path"))
}
}
}
}
CommandLineArgumentProvider
の実装を通じてビルドのパフォーマンスを改善する方法について詳しくは、Java プロジェクトのキャッシュをご覧ください。
アノテーション プロセッサによるエラーチェックを無効にする
コンパイル クラスパスの依存関係に不要なアノテーション プロセッサが含まれている場合は、build.gradle.kts
ファイルに次の内容を追加することでエラーチェックを無効にできます。コンパイル クラスパスに追加するアノテーション プロセッサは、まだプロセッサ クラスパスに追加されていないことにご注意ください。
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
argument("includeCompileClasspath", "false")
}
}
}
}
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false
}
}
}
}
Kotlin と kapt を使用する場合:
android {
...
defaultConfig {
...
kapt {
includeCompileClasspath = false
}
}
}
android {
...
defaultConfig {
...
kapt {
includeCompileClasspath false
}
}
}
プロジェクトのアノテーション プロセッサをプロセッサ クラスパスに移行した後で問題が発生する場合は、includeCompileClasspath
を true
に設定して、コンパイル クラスパス上のアノテーション プロセッサを許可できます。ただし、このプロパティを true
に設定することは推奨されません。Android プラグインの将来のアップデートで、この設定は使用できなくなります。