Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

CustomizeAnnotationProcessing

Csaba Kozák edited this page Mar 30, 2020 · 18 revisions

You have the possibility to customize annotation processing to fit your needs if you have some special use cases. To do so, you can declare options in your configuration (see below to see how to configure them according to your environment).

Available options

Trace

Type: boolean

trace is used to enable or disable @Trace annotation processing. This annotation is used to trace the execution of a method by writing log entries.

androidManifestFile

Type: string, path

By default, AndroidAnnotations try to find the AndroidManifest.xml file by looking recursively in parent folders. In case you have a specific project structure, you can specify the path to AndroidManifest.xml file by using androidManifestFile option.

resourcePackageName

Type: string

By default, AndroidAnnotations try to find the R class by extracting application package from AndroidManifest.xml file. But in some cases you may want to specify a custom package to look for the R class. This is why we added resourcePackageName option.

useR2

Type: boolean

In case of library projects, the R.*.* fields are not final, so they cannot be used in annotation parameters. You can use ButterKnife Gradle plugin to generate an R2 class which contains final fields. With this option enabled, AndroidAnnotations will allow R2 instead of the traditional R class.

logFile

Type: string, path

Since 3.0, AndroidAnnotations uses a custom logger to write logs in a file during code generation. It'll help everyone to debug and fix configuration in case of issues.

By default, the framework will write logs in {outputFolder}/androidannotations.log file. And to resolve {outputFolder}, we're looking for one of the following folders in this order :

  • target
  • build
  • bin
  • root project folder

If the output folder can't be resolve you can specify an output file by using logFile option. Note that you can also use {outputFolder} placeholder in this value if you just want to change the filename.

logLevel

Type: string, enum(trace, debug, info, warn, error)

The default log level is set to WARN. In case of issues, it could be useful to change it to TRACE with logLevel option.

logAppenderConsole

Type: boolean

By default, AA uses two appenders : FileAppender and MessagerAppender. The former will write in the log file and the latter will show log messages in IDE messages. But, there is also a ConsoleAppender which could be activated by setting true to logAppenderConsole option.

logAppenderFile

Type: boolean

The FileAppender is used by default, since it is useful to see historically what happened if the processing crashed or bad code got generated. Unfortunately in some cases this file can cause file locking problems, so this parameter offers a way to disable it.

threadControl

Type: boolean

threadControl is used to enable or disable @SupposeUiThread and @SupposeBackground annotation processing. These annotations can ensure that a method is called from the good thread. It is enabled (true) by default.

classSuffix

Type: String

classSuffix is used to change the suffix of enhanced classes like Activities with @EActivity. The default value is _.

generateFinalClasses

Type: boolean

generateFinalClasses is used to set if generated classes should be final or not. The default value is true.

library

Type: boolean

Indicates that the project is an Android library project. The default value is false.

encoding

Type: String

Controls which encoding is used for generated source files. By default UTF-8 is used. The encoding must be specified in a format what Charset.forName() accepts.

instantAppFeature

Type: boolean

Indicates that the module is an Android InstantApp Feature project. The default value is false.
Only necessary on the module having apply plugin: 'com.android.feature'.

incremental

Type: boolean

Enables Gradle-specific incremental annotation processing. Turn this off if you experience any weird build issues.

How to use options

You should first check the configuration page to verify your configuration is fine according to your build tool and IDE. Then, please follow one of these instructions to customize annotation processing.

Eclipse

  1. Right-click on your project, choose "Properties"
  2. Go to Java Compiler > Annotation Processing
  3. Add your options in processor options list

IntelliJ

  1. Go to IntelliJ's preferences
  2. Then go to Compiler > Annotation Processors and select your module
  3. Add your options in Annotation processor options list

Maven

To pass annotation processing argument with maven you MUST use the maven-compiler-plugin. Since version 3.1 of this plugin, you can use compilerArgs tag to easily pass multiple arguments. Here is how to use it:

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.1</version>
	<configuration>
		<encoding>UTF-8</encoding>
		<source>1.6</source>
		<target>1.6</target>
		<compilerArgs>
			<arg>-Atrace=true</arg>
			<arg>-AlogLevel=trace</arg>
			<arg>-AlogAppenderConsole=true</arg>
		</compilerArgs>
	</configuration>
</plugin>

Gradle

As of Android Plugin 2.3.0 we're able to set annotation processing arguments with the javaCompileOptionsblock inside the defaultConfig block. Here is how to use it:

dependencies {
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

android {
        // other config...

    defaultConfig {
        // other config...

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["logLevel": "TRACE",
                             "logAppenderConsole": "true"]
            }
        }
    }
}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally