• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

meituan/RoboAspectJ: A gradle plugin that enables AOP in android.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

meituan/RoboAspectJ

开源软件地址(OpenSource Url):

https://github.com/meituan/RoboAspectJ

开源编程语言(OpenSource Language):

Groovy 100.0%

开源软件介绍(OpenSource Introduction):

RoboAspectJ

RoboAspectJ is a Gradle plugin to introduce AspectJ (Aspect-Orient Programming) to Android project. It compiles aspects(if needed) and weave them all together in FULL-PROJECT scope. This means project (or subproject) sources, external libraries and local dependencies will all be dealt with by default.

Note: This plugin may change due to the modification of transform-api. So you may keep track of RoboAspectJ to make sure you're using the most recent version.

latest version: v0.9.1

Prerequisite

Android Plugin (application) 2.1.0

Apply

Add plugin dependency in buildscript classpath:

buildscript {
    dependencies {
        classpath 'com.meituan.gradle:roboaspectj:0.9.+'
    }
}

Apply plugin:

apply plugin: 'com.meituan.roboaspectj'

Coding

There are basically 2 ways to write your aspects and weave them into production code:

As Source

write aspects in @AspectJ syntax under your project's java source directory. e.g. {$projectDir}/src/main/java/

As Library

compile and bundle your aspects independently using ajc, then make it dependency in build script. For example:

compile 'com.example.myaspects:library:1.0'

This way may be a little bit complicated. But it's suitable for those who want to maintain their aspects as an independent project.

Variant-Specific Concern

While RoboAspectJ is registered globally, we still can do our crosscutting concern under specific variant. Actually, this is already done by Android plugin.

As Source

put variant-specific aspects under corresponding folder.

For example, I want to do some performance monitoring in myflavor, so I will write aspects under ${projectDir}/src/myflavor/java/.

As Library

add variant-specific aspects dependency to corresponding configuration scope.

myflavorCompile 'com.example.myaspects:library:1.0'

Configuration

There is an extension aspectj for you to do some tweaking.

Exclude

If you want to leave some artifact untouched from AspectJ, using:

aspectj {
	exclude group: 'com.google.android', module: 'support-v4'
}

Java runtime

When applying rxjava or retrolambda, you may need jrt.jar as classpath. Configure it by:

aspectj {
	javartNeeded true
}

Disable

For debug or performance use, you can disable weaving:

aspectj {
    enable false    //by default, it's true and you don't have to add this statement.
}

alternatively, set roboaspectj.enable property false when run gradle.

$ gradle clean assembleDebug -Droboaspectj.enable=false

Maybe you want it to be smarter to disable it when it's a debug flavor, then add this to your build script:

aspectj {
    disableWhenDebug true // default is false
}

or, specify roboaspectj.disableWhenDebug property.

$ gradle clean assembleDebug -Droboaspectj.disableWhenDebug=true

Though weaving is disabled, Aspects and AspectJ compile dependencies are all still there. It's only the weaving step doesn't happen.

Note: Corresponding property has precedence over config in build script in both of these 2 cases. For instance, weaving will not take effect when your roboaspectj.enable property is false, no matter what you config in build script.

License

Code is under the Apache Licence v2.

Feedback

This plugin is currently a prototype, and it still has much to improve. Feel free to contact: [email protected]




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
saulmm/Gradle-Stuff发布时间:2022-06-11
下一篇:
ealden/gradle-androidannotations-plugin: AndroidAnnotations plugin for Gradle发布时间:2022-06-11
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap