×

深圳网站建设—APP开发—网站制作—小程序开发_博纳网络公司

0755 -
82538016
82560826
网站制作资讯

安卓APP开发关于AOP技术的使用详解

文章编辑:网站建设 文章来源:APP开发 浏览量:

  安卓APP开发关于AOP技术的使用详解,首先深圳APP开发公司提醒在工程里添加一个名为“liba_aspectj”的Module,包含AspectJ相关功能代码,如图28-1所示:图28-1AOPAspect.java里是AspectJ功能代码,AOPTimer.java里是计时功能代码。此Module的build.gradle文件内容如下:
import com. android. build. gradle. LibraryPlugin import org. aspectj. bridge. IMessage import org. aspectj. bridge. MessageHandler import org. aspectj. tools. ajc. Main
apply plugin: 'com. android. library' buildscript {     repositories {         jcenter()    }     dependencies {         classpath 'com. android. tools. build: gradle: 2. 1. 0'         classpath 'org. aspectj: aspectjtools: 1. 8. 9'         classpath 'org. aspectj: aspectjweaver: 1. 8. 9'    } } repositories {     mavenCentral() } dependencies {     compile 'org. aspectj: aspectjrt: 1. 8. 9'     compile 'com. android. support: appcompat- v7: 22. 2. 1' } android {     compileSdkVersion 23     buildToolsVersion '23. 0. 2'     lintOptions {
  abortOnError false    } } android. libraryVariants. all { variant ->     LibraryPlugin plugin = project. plugins. getPlugin( LibraryPlugin)     JavaCompile javaCompile = variant. javaCompile     javaCompile. doLast {         String[] args = ["-showWeaveInfo",                         "-1. 8",// 当前 使用 的 JDK 版本                         "-inpath", javaCompile. destinationDir. toString(),                         "-aspectpath", javaCompile. classpath. asPath,                         "-d", javaCompile. destinationDir. toString(),                         "-classpath", javaCompile. classpath. asPath,                         "-bootclasspath", plugin. project. android. bootClasspath. join(                 File. pathSeparator)]         MessageHandler handler = new MessageHandler( true);         new Main(). run( args, handler)         def log = project. logger         for (IMessage message : handler. getMessages( null, true)) {             switch (message. getKind()) {                 case IMessage. ABORT:
  case IMessage. ERROR:                 case IMessage. FAIL:                     log. error message. message, message. thrown                     break;                 case IMessage. WARNING:                 case IMessage. INFO:                     log. info message. message, message. thrown                     break;                 case IMessage. DEBUG:                     log. debug message. message, message. thrown                     break;            }        }    } }
如要在application模块的代码里使用AspectJ库,需要在build.gradle文件里添加如下代码:
import org. aspectj. bridge. IMessage import org. aspectj. bridge. MessageHandler import org. aspectj. tools. ajc. Main buildscript {
  repositories {         mavenCentral()    }     dependencies {         classpath 'org. aspectj: aspectjtools: 1. 8. 9'    } } apply plugin: 'com. android. application' dependencies {     compile project(': liba_ aspectj') } … final def log = project. logger final def variants = project. android. applicationVariants variants. all { variant ->     if (!variant. buildType. isDebuggable()) {         log. debug(" Skipping non- debuggable build type '${variant. buildType. name}'.")         return;    }     JavaCompile javaCompile = variant. javaCompile
  javaCompile. doLast {         String[] args = ["-showWeaveInfo",                         "-1. 8",                         "-inpath", javaCompile. destinationDir. toString(),                         "-aspectpath", javaCompile. classpath. asPath,                         "-d", javaCompile. destinationDir. toString(),                         "-classpath", javaCompile. classpath. asPath,                         "-bootclasspath", project. android. bootClasspath. join( File.                           pathSeparator)]         log. debug "ajc args: " + Arrays. toString( args)         MessageHandler handler = new MessageHandler( true);         new Main(). run( args, handler);         for (IMessage message : handler. getMessages( null, true)) {             switch (message. getKind()) {                 case IMessage. ABORT:                 case IMessage. ERROR:                 case IMessage. FAIL:                     log. error message. message, message. thrown                     break;                 case IMessage. WARNING:                     log. warn message. message, message. thrown                     break;                 case IMessage. INFO:
  log. info message. message, message. thrown                     break;                 case IMessage. DEBUG:                     log. debug message. message, message. thrown                     break;            }        }    } }
 
如要在library模块的代码里使用AspectJ库,需要在build.gradle文件里添加如下代码:
import com. android. build. gradle. LibraryPlugin import org. aspectj. bridge. IMessage import org. aspectj. bridge. MessageHandler import org. aspectj. tools. ajc. Main buildscript {     repositories {         mavenCentral()    }     dependencies {         classpath 'org. aspectj: aspectjtools: 1. 8. 9'
 } } apply plugin: 'com. android. library' … dependencies {         compile project(': liba_ aspectj') } android. libraryVariants. all { variant ->     LibraryPlugin plugin = project. plugins. getPlugin( LibraryPlugin)     JavaCompile javaCompile = variant. javaCompile     javaCompile. doLast {         String[] args = ["-showWeaveInfo",                         "-1. 8",                         "-inpath", javaCompile. destinationDir. toString(),                         "-aspectpath", javaCompile. classpath. asPath,                         "-d", javaCompile. destinationDir. toString(),                         "-classpath", javaCompile. classpath. asPath,                         "-bootclasspath", plugin. project. android. bootClasspath. join(                 File. pathSeparator)]         MessageHandler handler = new MessageHandler( true);         new Main(). run( args, handler)
  def log = project. logger         for (IMessage message : handler. getMessages( null, true)) {             switch (message. getKind()) {                 case IMessage. ABORT:                 case IMessage. ERROR:                 case IMessage. FAIL:                     log. error message. message, message. thrown                     break;                 case IMessage. WARNING:                 case IMessage. INFO:                     log. info message. message, message. thrown                     break;                 case IMessage. DEBUG:                     log. debug message. message, message. thrown                     break;            }        }    } }
如要注入代码,需要先定义Pointcut,常用的指令格式如下所示:
execution( modifiers- pattern? ret- type- pattern declaring- type- pattern?
name- pattern( param- pattern) throws- pattern?)
modifiers-pattern:修饰符ret-type-pattern:方法的返回值类型declaring-type-pattern:方法所属的包名或类名name-pattern:方法名param-pattern:方法参数throws-pattern:方法抛出的异常类型注意:带?表示参数是可选项。使用的最多的返回类型模式是*,表示匹配任意的返回类型。方法参数的具体表示:()匹配了一个不接受任何参数的方法(..)匹配了一个接受任意数量参数的方法(零或者更多)(*)匹配了一个接受一个任何类型的参数的方法
(*,String)匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是String类型。如工程中包含如图28-2所示的包和类:MainActivity.java中有如下代码:
@Override protected void onCreate( Bundle savedInstanceState) {     super. onCreate( savedInstanceState);     setContentView( R. layout. activity_ main);     Toolbar toolbar = (Toolbar) findViewById( R. id. toolbar);     setSupportActionBar( toolbar);     initVariables();
initViews();     loadData(); } public void initVariables() {    … } public void initViews () {    … } public void loadData () {    … } public void logIn() {     Intent intent = new Intent( MainActivity. this, LoginActivity. class);     startActivity( intent); }
在LoginActivity.java中定义了以下方法:
@Override
protected void onCreate( Bundle savedInstanceState) {     super. onCreate( savedInstanceState);     setContentView( R. layout. activity_ login); } @Override public void finish(){     super. finish(); } @Override public void onBackPressed(){     super. onBackPressed(); }
想要输出com.example.aop包里所有onCreate方法的执行时间,按如下方式定义Pointcut:private static final String POINT_ METHOD = "execution(* com. example. aop.*. onCreate(..))";
之前提到,注入代码的方式有before、after和around3种,表示在目标方法执行之前、执行后和执行前后同时注入代码。好了,APP开发公司本文关于“安卓APP开发关于AOP技术的使用详解”的APP制作知识就分享到这里,谢谢关注,本站下期会继续就这个问题与各位进行分享,博纳网络编辑整理。
 

当前文章链接:/construction/appkaifa/2842.html
如果您觉得案例还不错请帮忙分享:

[声明]本网转载网络媒体稿件是为了传播更多的信息,此类稿件不代表本网观点,本网不承担此类稿件侵权行为的连带责任。故此,如果您发现本网站的内容侵犯了您的版权,请您的相关内容发至此邮箱【qin@198bona.com 】,我们在确认后,会立即删除,保证您的版权。