https://github.com/serso/aspecta
AspectJ for Android
https://github.com/serso/aspecta
Last synced: 12 months ago
JSON representation
AspectJ for Android
- Host: GitHub
- URL: https://github.com/serso/aspecta
- Owner: serso
- License: apache-2.0
- Created: 2018-10-22T17:25:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-01T12:50:09.000Z (over 7 years ago)
- Last Synced: 2025-01-07T17:43:31.148Z (over 1 year ago)
- Language: Java
- Size: 140 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Description
AspectJ for Android.
# Installation
In `build.gradle`:
```groovy
buildscript {
// might be not needed depending on your project's setup
repositories {
google()
jcenter()
}
dependencies {
classpath 'org.solovyev.android.aspecta:plugin:1.0.0'
}
}
apply plugin: 'com.android.application' // or 'com.android.library'
apply plugin: 'org.solovyev.android.aspecta'
aspecta {
// enable AspectA only in debug builds
enabled { BaseVariant variant -> variant.buildType.debuggable}
}
dependencies {
implementation 'org.aspectj:aspectjrt:1.9.1'
}
```
# Usage
Define an annotation:
```java
package org.example.android;
// retention must be class
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
public @interface MyAnnotation {
}
```
Define your aspect:
```java
package org.example.android;
@Aspect
public class MyAspect {
@Pointcut("execution(@org.example.android.MyAnnotation * *(..))")
public void method() {
}
@Before("method()")
public void execute(@NonNull JoinPoint jp) {
// define action
}
}
```
Use the annotation. The code will be automatically woven by the library:
```java
package org.example.android;
@Aspect
public class MainActivity extends Activity {
@MyAnnotation
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
```