📜 ⬆️ ⬇️

Aspect oriented programming in Android

Hearing about aspect-oriented programming, I, as an Android developer, immediately thought that it would hardly work on Android, however, I decided to try. I was very surprised when, after 5 minutes, the application using AspectJ was successfully launched on the phone.

I will not convince of the need to use aspects in Android and give examples of "uses cases", just give an example of how to add the ability to use aspects to your application. What are aspects and what to use them are beautifully described, for example, on the wiki .

As an IDE for development, I have long since switched to Android Studio, and therefore I use gradle as a build system. For her, and I will give an example of configuration.

The entire AspectJ connection process consists of the following steps:
')
  1. Connect the plugin for the assembly:
    In the buildscript section of the buildscript script of your project, add the following line to the dependencies :
     classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+' 

  2. Add to the build.gradle the application module:
     apply plugin: "android-aspectj" 

  3. ... and depending on the module we add:
     dependencies { compile 'org.aspectj:aspectjrt:1.8.+' } 

  4. We write the aspect itself.
    Aspects can be written using a special syntax in files with the as extension, or using java + a series of annotations. Since Android Studio does not include the AspectJ plugin, I preferred the second option (For Ultimate owners, IDEA versions are both available):
     @Aspect public class MainActivityAspect { private static final String TAG = "helloaspectj"; @Pointcut("execution(* ru.hoticecream.helloaspectj.ui.MainActivity.onCreate(..))") public void onCreateMethod(){} @Before("onCreateMethod()") public void doBeforeOnCreate(JoinPoint joinPoint) { Log.i(TAG, "AspectJ was here"); } } 

    This aspect is built in before calling the MainActivity.onCreate method, displaying the corresponding message in the log.


Now, starting the application and opening the MainActivity, we will see in the log:
... I/helloaspectj﹕ AspectJ was here

Let me give you another example of the use of aspect.
To do this, add a method to the MainActivity that sets a message to a specific text field:
 private TextView mGreetingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGreetingText = (TextView) findViewById(R.id.greeting_message); setGreetingMessage("Hello Android"); } private void setGreetingMessage(String message) { mGreetingText.setText(message); } 

As you can see, the field takes the value “Hello Android”, by launching the application you can make sure of it;)

Now add the following methods to the same aspect class:
 @Pointcut("execution(* ru.hoticecream.helloaspectj.ui.MainActivity.setGreetingMessage(..))") public void setGreetingMessageMethod(){} @Around("setGreetingMessageMethod()") public void makeNasty(final ProceedingJoinPoint pjp) throws Throwable { Log.d(TAG, pjp.getSignature().toLongString()); pjp.proceed(new Object[] { "Hello AspectJ" }); } 

Now this aspect is still looking for the setGreetingMessage method and replaces its call with the contents of the makeNasty method, using the @Around @Around for this @Around

Having started the application, we will already see “Hello AspectJ”. At the same MainActivity.java we did not change the MainActivity.java .

That's all. I hope the article was useful to you.

Finally, a couple of links:

Source: https://habr.com/ru/post/231287/


All Articles