In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "Android how to write a project based on compile-time annotations", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Android how to write a project based on compile-time annotations" this article.
I. Overview
In Android application development, we often choose some annotation-based frameworks to improve development efficiency, but due to the loss of running efficiency caused by reflection, we prefer compile-time annotated frameworks, such as:
Butterknife eliminates the need to write code for initialization of View and injection of events.
EventBus3 makes it easy for us to realize inter-component communication.
Fragmentargs easily adds parameter information to fragment and provides a method to create it.
ParcelableGenerator can automatically convert any object to Parcelable type, which is convenient for object transmission.
There are a lot of similar libraries, most of which are designed to help us automatically complete the parts of our daily coding that need to be rewritten (for example, View in every Activity needs to be initialized, and every object that implements the Parcelable interface needs to write a lot of fixed code).
This is not to say that the above framework does not necessarily use reflection, in fact, some of the internal implementation of the above framework still depends on reflection, but rarely and generally do cache processing, so relatively speaking, the efficiency impact is very small.
However, when using this kind of project, sometimes errors will be difficult to debug, the main reason is that many users do not understand the internal principle of this kind of framework, so they will spend a lot of time troubleshooting when they encounter problems.
So, being reasonable, when the compile-time annotation framework is so hot, we have reason to learn: how to write an opportunity compile-time annotated project
First of all, it is to understand its principle, so that when we encounter problems with similar frameworks, we can find the right way to troubleshoot problems; secondly, if we have good ideas and find that some code needs to be created repeatedly, we can also write a framework to facilitate our daily coding and improve coding efficiency; * it can also be regarded as the improvement of our own technology.
Note: the following use IDE for Android Studio.
This article takes writing a framework for View injection as a clue, detailing the steps for writing such a framework.
Second, preparation before writing
When writing such a framework, you generally need to set up multiple module, such as the example to be implemented in this article:
Ioc-annotation is used to store notes, Java module, etc.
Ioc-compiler is used to write annotation processor, Java module
Ioc-api is used to provide users with API. This example is the Andriod module.
Ioc-sample example, this example is an Andriod module
So in addition to the example, you can consider the general establishment of three module,module names. The above gives a simple reference. Of course, if conditions permit, some developers like to merge the two module of storing annotations and API into a single module.
For inter-module dependencies, because writing an annotation processor depends on related annotations, so:
Ioc-compiler depends on ioc-annotation
In the process of using, we will use annotations and related API
So ioc-sample depends on ioc-api;ioc-api, depends on ioc-annotation.
Third, the realization of the annotation module
The annotation module is mainly used to store some annotation classes. In this example, the template butterknife implements View injection, so only one annotation class is needed in this example:
Retention (RetentionPolicy.CLASS) @ Target (ElementType.FIELD) public @ interface BindView {int value ();}
We set the retention policy to Class and annotations are used on Field. Here, we need to input an id when using it, and set it directly in the form of value.
When you write, analyze how many annotation classes you need, and set @ Target and @ Retention correctly.
Fourth, the implementation of annotation processor
Once you have defined the annotations, you can write the annotation processor, which is a bit complicated, but there are rules to follow.
For this module, we generally rely on the annotation module, and we can use an auto-service library
The dependencies of build.gradle are as follows:
Dependencies {compile 'com.google.auto.service:auto-service:1.0-rc2' compile project (': ioc-annotation')}
The auto-service library can help us generate information such as META-INF.
(1) basic code
Annotation processors generally inherit from AbstractProcessor. Just now we said that there are rules to follow because some of the code is basically fixed, as follows:
@ AutoService (Processor.class) public class IocProcessor extends AbstractProcessor {private Filer mFileUtils; private Elements mElementUtils; private Messager mMessager; @ Override public synchronized void init (ProcessingEnvironment processingEnv) {super.init (processingEnv); mFileUtils = processingEnv.getFiler (); mElementUtils = processingEnv.getElementUtils (); mMessager = processingEnv.getMessager ();} @ Override public Set getSupportedAnnotationTypes () {Set annotationTypes = new LinkedHashSet () AnnotationTypes.add (BindView.class.getCanonicalName ()); return annotationTypes;} @ Override public SourceVersion getSupportedSourceVersion () {return SourceVersion.latestSupported ();} @ Override public boolean process (Set
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.