Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Configure AspectJ in Android Studio

I am trying to configure AspectJ in Android Studio.
But after all trial and error its not working ! Surprisingly I am able to make it work with Eclipse Kepler version

The steps I followed for Android Studio

  1. Created sample Android Project
  2. File > Settings > searched for AspectJ in Plugin sectionPlugin for AspectJ
  3. Assuming nothing more to be done in Studio , except configuration of build.gradle files
  4. Added compile 'org.aspectj:aspectjrt:1.8.1' to the to the build.gradle (Module:app)
  5. Created Analytics_onBackPressed.aj for back button press detection
  6. Created Analytics_OnClick.aj for click events detection
  7. Created Analytics_onCreate.aj for components oncreate event detection
  8. Created necessary dependency classes which the above mentioned *.aj classes will internally call
  9. Added required permissions in Manifest
  10. Running the project is not detecting any of the events (button click, oncreate or back button click)
  11. Followed these links ReferenceLinkOne , ReferenceLinkTwo and ReferenceLinkThree

My Question is what is more required to make AspectJ working with Android Studio


Steps followed in Eclipse and got AspectJ working

  1. Downloaded Eclipse Kepler version
  2. Through Install New Software option searched for http://download.eclipse.org/tools/ajdt/43/update
  3. Installed AspectJ Development Tools (Required) .
  4. Created sample android project
  5. Created Analytics_onBackPressed.aj for back button press detection
  6. Created Analytics_OnClick.aj for click events detection
  7. Created Analytics_onCreate.aj for components oncreate event detection
  8. Added required permissions in Manifest File
  9. Right clicked in project and converted project to AspectJ by the following Converted project to AspectJ
  10. Configured Java build path with AspectJ runtime library
  11. Now while running the project, I am able to detect components oncreate, back button press

Environment Used

Android Studio : 2.1.2

JRE : 1.8.0

Windows 7 Enterprise

Any help is highly appreciated!


EDIT : 1 , From the output , still AspectJ is not properly configured

As per this link , I created jar file from eclipse including *.aj files and corresponding dependency. Excluded AndroidManifest.xml while creating jar file and created jar

Created Project in Android Studio. Placed this *.jar file in libs file. (apps> libs). I have enabled AspectJ waving as shown below

Enabled AspectJ waving

Now searched the properties for AspectJ and enabled
enter image description here
Now running the project, should be creating logs I had put in *.aj files which is in plugin . Unfortunately these logs are not printed in Android Studio logs.

Form which I am concluding still AspectJ is not enabled in this project OR there is a configuration error

like image 623
Sreehari Avatar asked Jul 11 '16 07:07

Sreehari


2 Answers

Disclosure: I am the author of the inofficial AspectJ weaver plugin.

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

To use this plugin, you have two options:

  • Switch from .aj to annotation style aspects.
  • Compile the .aj files outside the IDE (IntelliJ Community / Android Studio), for example using Maven/Gradle/Ant, and package them into a .jar. Include this .jar as a dependency in your project and click the Search for aspects... button, as per the documentation.
like image 74
Mattias Jiderhamn Avatar answered Nov 20 '22 05:11

Mattias Jiderhamn


I have had various success trying to get AspectJ working in Android Studio. This link helped me get part of the way there (assumes Gradle build setup for your project). http://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/

Using the custom block of code in the build.gradle file, I was able to get aspects defined using annotations in .java files compiling and weaving properly in my project. However, this same method did not work for me when defining native aspects using .aj files. I hope it helps.

like image 31
Justin Hong Avatar answered Nov 20 '22 05:11

Justin Hong