1. Introduction Are you still blindly copying and pasting boring and repetitive codes? How is this different from moving bricks? Have you ever thought: you use code to write an automated APP, but why the code itself lacks vitality? Master Android-apt, eliminate repetitive codes, and let you write code as elegantly as writing poetry. 2. What is apt? Apt means: annotation processing tool. This tool is amazing. It can automatically generate specific Java files during compilation through annotations to realize automatic code writing. Q: What’s the point? Why should I automate the code that I can write by myself? Brother, are you going to use your copy-and-paste skills again? Be patient, read this article carefully, and you will fall in love with this guy. I believe you must have heard of the two well-known open source libraries ButterKnife and Dagger2, and you should know why I mentioned them. That's right! Both of these open source libraries are based on apt. 3. After saying so much, how to use it? Don't worry, let's build the environment first (based on gradle plugin version 2.2.0 or above) 1. Create a new Java module in Android Studio to store the annotation processing logic. The name is arbitrary, I usually name it apt. The most important thing: add the annotation processing dependency to the app module: annotationProcessor project(':apt') (Explanation: Since the Android module does not contain apt-related classes, a new Java module is needed to write apt logic. What? You don't believe it? If you don't believe it, write a class that inherits AbstractProcessor and try it) 2. Create a new module (either android or java) to store annotations. The name is arbitrary. I named it anno. Add the dependency compile project (':anno') to the build.gradle file of app and apt. (Why create a new module to hold the annotation class instead of putting it in the app module or apt module: the main reason is that the app module and the apt module cannot directly depend on each other. As for why they cannot depend directly, I will not go into details. In short, if you don’t believe it, you will know it if you try it!) 3. In build.gradle of apt, add the following dependencies. At this point, our environment configuration work has come to an end. (Among them: 1. auto-service is used to automatically generate configuration files in a specific path after annotation; 2. javapoet is a tool used to conveniently generate java files with apt. I believe that this explanation is still confusing to everyone, don't worry, keep reading) 4. The environment is set up, and now it’s time to show your operation 1. First, create a new annotation class in the anno module
2. Create a new annotation processing class in the apt module, inheriting from AbstractProcessor
3. Since apt is to automatically generate java files, we need to construct a target class. Suppose we want to generate such a class
4. Operate the annotation processing class to generate the target java file
Roughly speaking the steps are: (1) Add the @AutoService(Processor.class) annotation, which will automatically generate a configuration file in the specified path: apt/build/classes/main/META-INF/services/javax.annotation.processing.Processor; (2) Add the @SupportedAnnotationTypes annotation to configure the annotation types that this class will handle. (Pass in a String type parameter in the format of package name + class name); (3) Use javapoet to write code and build logic. For specific usage, please see here; (4) The main logic of generating code blocks is: traverse all classes annotated with @Test, extract the annotation content and class name and print them out. 5. Add @Test annotation to the class. Let’s try it with MainActivity.
6. Rebuild the project and find the target java file in the path app/build/generated/source/apt/debug. The TestClass code is as follows:
5. But it’s useless? Indeed, up to this point, we have used dozens of lines of code to generate a TestClass of 5 lines of code. This operation can be described in four words: boring. However, the following operation will give you a refreshing feeling. First, we create a few test classes. Suppose we create four test classes: ActivityA, ActivityB, ActivityC, ActivityD, and add the @Test annotation to them. Then rebuild, you will find that our TestClass has changed:
Suddenly I understand! So, this is how it works! At this point, have you felt the charm of apt? Yes, it can help you get rid of duplicate codes and stop copying and pasting. |
<<: The sixth episode of Aiti Tribe Clinic: How does machine learning judge emergencies?
>>: The sixth session of the Aiti Tribe Technical Clinic
The impact of advertising materials on advertisin...
Everyone knows that the heart is one of the most ...
Audit expert: Ran Hao Well-known science writer W...
Would you like to know about the ten tips for Wei...
Software Information Name: Magic Electricity Pack...
In the world of food, there is a unique stimulati...
Before starting online promotion , you need to un...
Many people may have been scared by the "sup...
Recently, three primary school students in Ninggu...
When it comes to creating high likes on Zhihu, th...
If you want to stay away from cancer, you must st...
In an information flow advertisement , the role o...
Replay is a video editing software with similar f...
Due to the dual impact of the COVID-19 pandemic a...
The author of this article breaks down the operat...