How to Configure AspectJ in Android Studio
Asked Answered
C

3

12

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

Chekiang answered 11/7, 2016 at 7:53 Comment(0)
D
6

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.
Deloris answered 14/7, 2016 at 7:42 Comment(2)
I have tried to configure plugin with *.aj files and dependencies in Eclipse and generated *.jar file. Now the output from Android Studio is not as expected. Have included the output in my EDIT. Any clue where I am going wrong?Chekiang
It's hard to help just given "it doesn't work". Please include the output of IntelliJs compiler (upon Rebuild Project) and/or intellij.log for further assistance.Deloris
P
2

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.

Preparator answered 11/7, 2016 at 18:57 Comment(2)
Scratch that. I made a dumb mistake that prevented my native .aj file from being compiled properly. It's the disadvantage to not having formal support for AspectJ in Android Studio: no compile errors. I'm able to get native .aj files working properly now using the method outlined in that link.Preparator
Here it talks about one library aspect code and android app code. Which is this library. Do we need to create it? DebugTrace.java , StopWatch.java , DebugLog.java and TraceAspect.java will go where ? I Got totally confusedChekiang
W
0

IntelliJ has no tooling support for AspectJ, and Annotation Style AspectJ can work on Android studio. Just treat the aspect code as Java code.

For detail read this issue aspectj for Android studio - where is AJDT plugin

Also, you can use gradle plugin aspectjx which can weave kotlin code and demo is here

Wallboard answered 14/7, 2016 at 2:30 Comment(1)
Sorry, but this is incorrect. IntelliJ Ultimate has "native" support for AspectJ via bundled Jetbrains plugin. The AspectJ weaver plugin - which I am the author of - was created before Jetbrains added AspectJ support. It is no longer maintained, but may still work for non-Ultimate editions of IntelliJ based products, such as IntelliJ Community and Android Studio.Deloris

© 2022 - 2024 — McMap. All rights reserved.