In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to implement faceted programming in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize facet programming in Android".
Install AspectJ
ApsectJ development on Android consists of several parts, AspectJ gradle plug-ins, ApsectJ dependencies, and AspectJ compilers.
Installing the AspectJ compiler first is easy, just like installing the JAVA environment
Download link: http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.9.0.jar
At present, the latest one has been updated to 1.9.1. If your computer already has a JAVA environment, just run this jar package directly.
After installation, you need to configure the environment variables to the bin directory of aspectj, which will not be discussed here.
Export PATH= "$PATH:~/Library/Android/sdk/platform-tools" export PATH= "$PATH:/usr/local/opt/gradle/gradle-4.1/bin" export PATH= "$PATH:~/Library/Android/sdk/ndk-bundle" export PATH= "$PATH:~/Library/flutter/bin" export PATH= "$PATH:~/Library/kotlinc/bin" export PATH= "$PATH:~/Library/AspectJ/bin" if (! variant.buildType.isDebuggable ()) {log.debug ("Skipping non-debuggable" Build type'${variant.buildType.name}'. ") Return } JavaCompile javaCompile = variant.javaCompile javaCompile.doLast {String [] args = ["- showWeaveInfo", "- 1.8", "- inpath", javaCompile.destinationDir.toString (), "- aspectpath", javaCompile.classpath.asPath, "- d", javaCompile.destinationDir.toString (), "- classpath", javaCompile.classpath.asPath, "- bootclasspath" Project.android.bootClasspath.join (File.pathSeparator)] MessageHandler handler = new MessageHandler (true) New Main () .run (args, handler);}} / /-add content
This gradle script is the compilation process of adding an acj after the completion of java compilation.
MessageHandler is an object in AspectJ Tools that is used to receive parameters and then compile with acj.
Finally, add dependencies dependency and support for AspectJ.
Implementation 'org.aspectj:aspectjrt:1.9.0'
Create AspectJ code
The following part of the code will look confused, but don't worry about the specific grammatical meaning for now.
First run the environment, and then combined with the theory to slowly modify the code to feel that you can quickly start AOP.
Take a HelloWorld as an example, our MainActivity does nothing but a basic lifecycle approach.
Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);} @ Override protected void onStart () {super.onStart ();} @ Override protected void onPause () {super.onPause ();} @ Override protected void onStop () {super.onStop ();} @ Override protected void onDestroy () {super.onDestroy ();}
Now we're going to write an AspectJ class that looks a little different from a normal Java class, which can be understood as simply using annotations as a medium to let the ACJ compiler know which methods to inject.
What this class does is tell the ACJ compiler to print a line of log in front of each method in MainActivity, outputting which method is currently executed
@ Aspectpublic class AspectTest {private static final String TAG = "AspectTest"; @ Pointcut ("execution (* phoenix.com.helloaspectj.MainActivity.** (..)") Public void executeAspectJ () {} @ Before ("executeAspectJ ()") public void beforeAspectJ (JoinPoint joinPoint) throws Throwable {Log.d (TAG, "beforeAspectJ: injected->" + joinPoint.toShortString ());}}
When I first came into contact with AspectJ, I was a little confused when I saw this code. Explain the meaning of several annotations.
Aspect: tell the ACJ compiler that this is an AspectJ class
@ PointCut: PointCut is a concept in AspectJ, and another concept along with it is JoinPoint, which together describes the aspect to be injected.
@ Before: indicates the location to be injected. The common ones are Before/After/Around, which means before execution, after execution, and replacing the original method, respectively.
Here, the @ PointCut annotated parameter means to inject all the methods in MainActivity, and the parameter uses regular matching syntax.
Let's take a look at the result of this code execution.
07-26 16-04-56.611-22823-22823? D/AspectTest: beforeAspectJ: injected-> execution (MainActivity.onCreate (..)
07-26 16-04-56.661-22823-22823? D/AspectTest: beforeAspectJ: injected-> execution (MainActivity.onStart ())
We see that although we didn't write the log print statement in MainActivity, we did it through AspectJ, inserting our own log before the two lifecycles of MainActivity were executed.
Working with scen
AspectJ is just one of the means of AOP, and similarly, asm is used to modify bytecode. The reason why AOP can be understood by more and more people is that abstractly it can be decoupled very well.
Advanced ones can use AOP to achieve trackless burying points, data collection, and even modify the code that cannot be moved in SDK.
At this point, I believe you have a deeper understanding of "how to achieve faceted programming in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.