Giter Site home page Giter Site logo

jdvp / androidaspectexample Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 4.0 470 KB

Project to show examples of leveraging Aspect-Oriented Programming techniques

Home Page: https://jdvp.me/articles/AOP-in-Android

License: MIT License

Kotlin 100.00%
android aspect-oriented-programming aspectj aop android-aspectj

androidaspectexample's Introduction

AndroidAspectExample

Project to show examples of leveraging Aspect-Oriented Programming (AOP) techniques

Aspects are defined in AspectLogging.java and affect MainActivity.java. In order to see that the code has been injected, build the app and check the app/build directory for the ajc-transform.log file which will show which advices have been added to the project during the build process.

If you run the app, the aspects added will simply log when a button is clicked or onCreate happens.

Created as a sample for my article "Aspect-Oriented Programming in Android".

Releases

Please see the GitHub Releases Page for this example if you want to see details for a specific Android Gradle Plugin (AGP) version. Each release version for this sample project will correspond to the same AGP version.

Notes

Plugin Change

As of v4.1.0, this sample project leverages the following Gradle Plugin for weaving aspects:

gradle-aspectj-pipeline-plugin

Previously, this project was leveraging android-gradle-aspectj. For information on why I made the switch, you might be interested in reading my article "Switching AspectJ Plugins in Android".

Code required using either plugin is the same since they both use the same aspectjrt library under the hood. The only difference is the required plugin setup in our gradle files.

Kotlin 1.5.0 Support

This is more of an issue with AspectJ than this project or even the gradle-aspectj-pipeline-plugin project, but Kotlin code compiled with Kotlin version 1.5.0 now uses invokedynamic to to compile Single Abstract Method functions (see here for information in the documentation). This means that lambdas in Kotlin using this version or above will not be weaved as expected as the compiled bytecode will not be able to be matched by AspectJ pointcuts.

To get around this for now, enable the following in project-level build.gradle files:

kotlinOptions {
    freeCompilerArgs = ["-Xsam-conversions=class"]
}

You can read more about the issue here if interested.

Using newer versions of AspectJ

If you are using AspectJ for some other reason and so want to leverage a different version of the library other than the one suggested by the Ibotta Plugin (1.9.6), which is used to compile the plugin, you can generally do so freely up until version 1.9.21 of the AspectJ Plugin (org.aspectj:aspectjrt).

If you have a minSdk of 25 or lower, 1.9.22 will likely give you problems. My testing seems to indicate that this can be fixed by desugaring.

First, make sure the requisite compile option is set

compileOptions {
    coreLibraryDesugaringEnabled true
}

Then, include the desugaring dependency

coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4")

Make sure to update this dependency to latest as required.

Why I don't recommend AOP in 2023

In late January 2023, the AOP plugin that this project uses, gradle-aspectj-pipeline-plugin, indicated that the project would no longer be maintained due to issues with the upcoming AGP 8.0.0 updates removing required APIs. In response, I wrote "Why I Don't Recommend Aspect-Oriented Programming in Android in 2023", an article that explains why I would be hesitant to use AOP in an enterprise application in 2023.

I did do some research and came to the conclusion that the plugin would not be broken by AGP 8.0.0 and as such I will continue updating this sample project (playing both sides??) until the plugin begins to stop working with AGP or Kotlin updates.

That being said, even though I am going to continue updating this sample app for the foreseeable future, I would recommend reading "Why I Don't Recommend Aspect-Oriented Programming in Android in 2023" and really thinking about if you actually need to leverage AOP in your project before continuing on and playing around with the sample project.

androidaspectexample's People

Contributors

jdvp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

androidaspectexample's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.