Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AspectJ作为android库模块依赖项_Java_Android_Gradle_Aop_Aspectj - Fatal编程技术网

Java AspectJ作为android库模块依赖项

Java AspectJ作为android库模块依赖项,java,android,gradle,aop,aspectj,Java,Android,Gradle,Aop,Aspectj,我想在我的库模块依赖项中使用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 { repositor

我想在我的库模块依赖项中使用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.1'
  }
}

apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'

android {
  compileSdkVersion 23
  buildToolsVersion "23.0.1"

  defaultConfig {
    minSdkVersion 4
    targetSdkVersion 23
    versionCode 1
    versionName "1.0.0"
  }
}

dependencies {
  compile 'org.aspectj:aspectjrt:1.8.9'
  apt 'com.google.dagger:dagger-compiler:2.2'
  compile 'com.google.dagger:dagger:2.2'
  provided 'javax.annotation:jsr250-api:1.0'
  provided 'org.glassfish:javax.annotation:10.0-b28'
}

android.libraryVariants.all { variant ->
  LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)
  JavaCompile javaCompile = variant.javaCompile
  javaCompile.doLast {
    String[] args = ["-showWeaveInfo",
                     "-1.5",
                     "-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;
      }
    }
  }
}

apply from: 'https://raw.githubusercontent.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

def isReleaseBuild() {
    return version.contains("SNAPSHOT") == false
}

allprojects {
    version = VERSION_NAME
    group = GROUP

    repositories {
        mavenCentral()
    }
}
Project 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.1'
  }
}

apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'

android {
  compileSdkVersion 23
  buildToolsVersion "23.0.1"

  defaultConfig {
    minSdkVersion 4
    targetSdkVersion 23
    versionCode 1
    versionName "1.0.0"
  }
}

dependencies {
  compile 'org.aspectj:aspectjrt:1.8.9'
  apt 'com.google.dagger:dagger-compiler:2.2'
  compile 'com.google.dagger:dagger:2.2'
  provided 'javax.annotation:jsr250-api:1.0'
  provided 'org.glassfish:javax.annotation:10.0-b28'
}

android.libraryVariants.all { variant ->
  LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)
  JavaCompile javaCompile = variant.javaCompile
  javaCompile.doLast {
    String[] args = ["-showWeaveInfo",
                     "-1.5",
                     "-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;
      }
    }
  }
}

apply from: 'https://raw.githubusercontent.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

def isReleaseBuild() {
    return version.contains("SNAPSHOT") == false
}

allprojects {
    version = VERSION_NAME
    group = GROUP

    repositories {
        mavenCentral()
    }
}
还有我的方面课:

@Aspect
public class HandlerAspect {
  private static final String POINTCUT_METHOD =
      "execution(@com.henorek.discharge.annotations.HandleException * *(..))";

  private static final String POINTCUT_CONSTRUCTOR =
      "execution(@com.henorek.discharge.annotations.HandleException *.new(..))";

  @Pointcut(POINTCUT_METHOD)
  public void methodAnnotatedWithHandleException() {}

  @Pointcut(POINTCUT_CONSTRUCTOR)
  public void constructorAnnotatedHandleException() {}

  @Around("methodAnnotatedWithHandleException() || constructorAnnotatedHandleException()")
  public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    String className = methodSignature.getDeclaringType().getSimpleName();
    String methodName = methodSignature.getName();

    Log.d("DUPA", "DUPA");
    Object result = joinPoint.proceed();
    Log.d("DUPA", "DUPA");
    return result;
  }

  @AfterThrowing(value = "methodAnnotatedWithHandleException() || constructorAnnotatedHandleException()", throwing = "exception")
  public Object testJoinPoint(ProceedingJoinPoint joinPoint, Throwable exception) throws Throwable {
    Log.d("DUPA", "DUPA");
    Object result = joinPoint.proceed();
    Log.d("DUPA", "DUPA");
    System.out.println("dupa");
    return result;
  }
}
我的注释是简单的类+构造函数注释。我正在这个项目中另一个名为sample的模块中执行代码,顾名思义,它就是我创建方法的地方

@MyAnnotation
void test() {
}
我在点击按钮时调用了这个方法,但它没有记录任何内容,我错过了什么吗?我的代码看起来与上面的示例完全相同


当然,我已将我的:库模块添加到:示例模块依赖项。

您是否编辑了设置。gradle?此外,如果您查看本博客的当前源代码,则会发现bootloader的添加方式发生了变化(可能与此无关)
“-bootclasspath”,project.android.bootclasspath.join(File.pathseptor)]
请尝试,它也一贯支持应用程序和库模块。如有任何反馈,将不胜感激:)