How to set up AspectJ in Android Studio

I am trying to configure AspectJ in Android Studio .
But trial and error doesnโ€™t work! Surprisingly, I can make it work with the Eclipse Kepler version

The steps that I followed for Android Studio

  • Created Sample Android Project
  • File> Preferences> search for AspectJ in the plugins section Plugin for AspectJ
  • Assuming nothing needs to be done in Studio other than configuring build.gradle files
  • Added compile 'org.aspectj:aspectjrt:1.8.1' to the build.gradle file (Module: app)
  • Created by Analytics_onBackPressed.aj to detect feedback button
  • Created by Analytics_OnClick.aj to detect click events
  • Created by Analytics_onCreate.aj for oncreate event detection components
  • The necessary dependency classes have been created that the *.aj classes mentioned above will internally call
  • Added necessary permissions in the manifest
  • Starting a project does not detect any of the events (button click, oncreate or back button)
  • Following these links are ReferenceLinkOne , ReferenceLinkTwo and ReferenceLinkThree

My question is what is more necessary for AspectJ to work with Android Studio


The steps that were taken in Eclipse and got AspectJ work

  • Download version of Eclipse Kepler
  • Via Install New Software, select http://download.eclipse.org/tools/ajdt/43/update
  • Installed AspectJ Development Tools (Required) .
  • An example of an android project has been created
  • Created by Analytics_onBackPressed.aj to detect feedback button
  • Created by Analytics_OnClick.aj to detect click events
  • Created by Analytics_onCreate.aj for oncreate event detection components
  • Required permissions added in manifest file
  • Right-click in the project and convert the project to AspectJ as follows Converted Project to AspectJ
  • Configured Java build path using the AspectJ runtime library.
  • Now when I run the project, I can detect the oncreate components, a back button

Medium used

Android Studio: 2.1.2

JRE: 1.8.0

Windows 7 Enterprise

Any help is much appreciated!


EDIT: 1, the output of AspectJ is not configured correctly

According to this link , I created a jar file from eclipse, including *.aj files and the corresponding dependency. AndroidManifest.xml excluded when creating jar file and jar created

Created a project in Android Studio. Placed this * .jar file in the libs file. (apps> libs) . I turned on AspectJ, waving as shown below.

Included AspectJ waving

Now you searched for properties for AspectJ and resolved enter image description here
Now starting the project, you need to create the logs that I put in the *.aj files that are in the plugin. Unfortunately, these magazines are not printed in Android Studio magazines.

The form I am completing is AspectJ is not included in this project or there is a configuration error

+8
android android-studio aspectj aspectj-maven-plugin
source share
3 answers

Disclosure: I am the author of the unofficial AspectJ waver plugin .

As noted in the AspectJ plugin plugin, the plugin does not support compiling .aj files.

To use this plugin, you have two options:

  • Switch from .aj to annotation aspects .
  • Compile .aj files outside the IDE (IntelliJ Community / Android Studio), for example, using Maven / Gradle / Ant and package them in .jar . Include this .jar as a dependency in your project and click on the โ€œSearch for Aspects ...โ€ button as described in the documentation.
+2
source share

I had great success trying to get AspectJ to work in Android Studio. This link helped me get the part of the path (involves installing Gradle for your project). http://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/

Using a custom block of code in the build.gradle file, I was able to get the aspects defined by annotations in the .java files that compile and intertwine correctly in my project. However, the same method did not work for me when defining my own aspects using .aj files. Hope this helps.

+1
source share

IntelliJ lacks tool support for AspectJ, and AspectJ annotation style can work on Android studio. Just consider aspect code as Java code.

Read more on this aspectj article for android studio - where is the AJDT plugin

Alternatively, you can use the gradle aspectjx plugin, which can weave the kotlin code and demo here

0
source share

All Articles