You may often see in the Build.gradle file, such words, Annotationprocessor, android-apt, provided, what exactly do they do? Let's take a look here.
1. What is apt?
With the popularity of open-source annotation frameworks such as Butterknife,dagger, the APT concept is becoming more and more familiar.
The functions of annotationprocessor and android-apt are the same, they are alternative relationships, and before you know them, look at apt.
APT (Annotation processing tools) is a tool that handles annotations, detects source code files, finds Annotation, and automatically generates code based on annotations. The annotation processor can generate additional source files and other files depending on the annotation in the source file (the content of the file is determined by the writer of the annotation processor) when processing annotation, and apt compiles the generated source files and the original source files. Generate the class file together.
The processing elements of apt
Note Processor (abstractprocess) + code handling (JAVAPOET) + Processor registration (Autoservice) +apt
Using apt to process annotation processes
1. Defining annotations (such as @automain)
2. Defining the Annotation processor
3. The processing is done within the processor, typically generating Java code.
4. Registering the processor
5. Use apt to complete such work.
2, Annotationprocessor
Annotationprocessor is one of the apt tools, which is a built-in framework developed by Google and does not need to be introduced and can be used directly in the Build.gradle file, as follows
{ annotationProcessor project(‘:xx‘) annotationProcessor ‘com.jakewharton:butterknife-compiler:8.4.0‘}
3, Android-apt
ANDROID-APT is an apt framework developed by a developer himself, where the source code is hosted, and with the release of the Android Gradle plugin version 2.2, the Android Gradle plugin provides the name Annotationprocessor function to completely replace android-apt, since this android-apt author on the official website announced that the latest Android Gradle plugin now supports Annotationprocessor, and warns and or blocks android-apt, and recommend that you use the official Android plugin annotationprocessor.
But many projects still use ANDROID-APT, and if you want to replace it with annotationprocessor, you need to know how android-apt is used. Here's an introduction.
3.1. Add android-apt to Build.gradle under Project
//配置在Project下的build.gradle中buildscript { repositories { mavenCentral() } dependencies { //替换成最新的 gradle版本 ‘com.android.tools.build:gradle:1.3.0‘ //替换成最新android-apt版本 ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘ }}
3.2, the configuration of Build.gradle in module
Usually when used, use apt to declare the library file used by the annotations. Project dependencies can be divided into multiple parts. For example Dagger has two components Dagger-compiler and dagger. Dagger-commpiler is used only at compile time, the runtime must use dagger.
//配置到Module下的build.gradle中‘com.android.application‘‘com.neenbedankt.android-apt‘‘com.squareup.dagger:dagger-compiler:1.1.0‘‘com.squareup.dagger:dagger:1.1.0‘}
Basic use is above these two points, want to replace android-apt with Annotationprocessor. Delete and replace the appropriate sections
ANDROID-APT Document Translation
4, provided and annotationprocessor difference Annotationprocessor
The dependent libraries are executed only at compile time, but the libraries are not packaged into the apk at the end,
The code in the compilation library does not have the meaning of direct use, nor does it provide an open API call, and the final goal is to get the files generated in the compilation library for us to invoke.
Provided
Provided is a compile-time execution and will not be packaged in the APK, but it is fundamentally different from apt/annotationprocessor.
A 、B、C都是Library。 A依赖了C,B也依赖了C App需要同时使用A和B 那么其中A(或者B)可以修改与C的依赖关系为Provided
A the library is actually going to use C, but it knows that B there is also a C, their own to take another to appear redundant, and so on when the app starts running, a can get C through B, that is, two people common this c. So just before I meet with B, suppose you have C. If you run without C, it's going to crash.
To sum up, provided is indirectly dependent on the library, the runtime must ensure the existence of the library, otherwise it will collapse, play a role to avoid relying on duplicate resources.
5. Simple project using apt--custom annotations
5.1. Add a Java Library Module named Apt-lib, write the annotation class:
@Target(ElementType.TYPE) //作用在类上@Retention(RetentionPolicy.RUNTIME)//存活时间public @interface AutoCreate {}
5.2 . Add a Java Library Module named Apt-process and write classes to handle annotations. After using the @autocreate above, the specified Java file will be generated according to the following class
@AutoService(Processor.class) Public class testprocess extends abstractprocessor { @Override PublicSet<string>Getsupportedannotationtypes() {returnCollections.singleton (AutoCreat.class.getCanonicalName ()); }@Override Public Boolean Process(set<? extends typeelement> annotations, roundenvironment roundenv) {MethodSpec main = Methodspec.methodbuilder ("Main"). Addmodifiers (Modifier.public, modifier.static). Returns (void. Class). Addparameter (String[].class,"args"). Addstatement ("$T. Out.println ($S)", System.class,"Hello, javapoet!."). build (); TypeSpec HelloWorld = Typespec.classbuilder ("HelloWorld"). Addmodifiers (Modifier.public, modifier.final). Addmethod (Main). build (); Javafile javafile = Javafile.builder ("Com.songwenju.aptproject", HelloWorld). Build ();Try{Javafile.writeto (Processingenv.getfiler ()); }Catch(IOException e) {E.printstacktrace (); }return false; }}
5.2.1, the lib to be used
{ compile project(‘:apt-lib‘) compile ‘com.squareup:javapoet:1.8.0‘ compile ‘com.google.auto.service:auto-service:1.0-rc2‘}
At this point a simple custom annotation class is completed, just a Helloworld.java file is generated with only one main () function
Use of 5.3, custom annotation classes
Easier to use. In the Java file, use the following:
@AutoCreatpublicclass MainActivity extends AppCompatActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
Configuring the Build.gradle File
dependencies { //添加下面这句就可以了 project(":apt-lib") project(‘:apt-process‘)}
Demo Download : Https://github.com/JantHsueh/APTProject
Reference:
Android-apt
In-depth understanding of compilation annotations (ii) Annotationprocessor and ANDROID-APT
In-depth understanding of compilation annotations (iii) the difference between dependency Apt/annotationprocessor and provided
ANDROID-APT Switch to Annotationprocessor
http://code.neenbedankt.com/
Android APT (compile-time code generation) Best Practices
Android apt and easy-to-use apt-based applications
You must know apt, annotationprocessor, android-apt, provided, custom annotations