android - 当我将 AspectJ 应用于带有 Androidannotations 的 Android 项目时不起作用

标签 android gradle aspectj android-annotations

我正在使用 Androidstudio 0.8.9 并使用 gradle 构建。

我使用的是 Android-Annotations,效果很好。我也想使用 AspectJ,所以应用插件(https://github.com/uPhyca/gradle-android-aspectj-plugin)。

但是编译失败,并抛出一些错误信息

:app:compileDebugJava
Internal compiler error: java.lang.IllegalStateException: java.lang.IllegalArgumentException: Unknown location : SOURCE_OUTPUT at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchAnnotationProcessorManager.discoverNextProcessor(BatchAnnotationProcessorManager.java:183)
:app:compileDebugAspectj FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugAspectj'.
> Internal compiler error: java.lang.IllegalStateException: java.lang.IllegalArgumentException: Unknown location : SOURCE_OUTPUT at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchAnnotationProcessorManager.discoverNextProcessor(BatchAnnotationProcessorManager.java:183)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 13.616 secs
Unknown location : SOURCE_OUTPUT at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchAnnotationProcessorManager.discoverNextProcessor(BatchAnnotationProcessorManager.java:183)
2:04:32: External task execution finished 'build'.

My build.gradle is

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
        classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'
    }
}

def androidAnnotationsVersion = '3.1';
def daggerVersion = '1.0.0';

apply plugin: 'com.android.application'
apply plugin: 'android-aspectj'
apply plugin: 'android-apt'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.flask.aspectjtest"
        minSdkVersion 16
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

configurations {
    apt
    ajc
    aspects
    ajInpath
}

apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
        resourcePackageName "com.flask.aspectjtest"
    }
}

ext.aspectjVersion = '1.8.2'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.0.0'

    apt "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
    apt "com.squareup.dagger:dagger-compiler:${daggerVersion}"
    compile "com.squareup.dagger:dagger:${daggerVersion}"
}

所以,我测试了 aspectj 插件单独工作(没有 android-annotations),并且运行良好。 我找到了一些答案(Android Annotations and MonkeyTalk?),FAQ Page of AA . 但我认为这是 eclipse 解决方案,我无法用这个答案解决我的问题。

我将注释处理器 android-apt 更改为 ajc。但是现在,它找不到 AndroidManifest.xml 文件。 (因为apt配置不可用)

repositories {
    mavenCentral()
    maven { url 'http://repo.spring.io/snapshot' }
}

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'
        classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'
    }
}

def androidAnnotationsVersion = '3.1';
def daggerVersion = '1.2.2';

apply plugin: 'com.android.application'
apply plugin: 'android-aspectj'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.flask.aspectjtest"
        minSdkVersion 16
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java', 'build/generated/source/apt/${variant.dirName}']
            resources.srcDirs = ['src/main/resources']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
    }
}

configurations {
    ajc
    aspects
    ajInpath
}

ext.aspectjVersion = '1.8.2'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.0.0'

    ajc "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "org.androidannotations:androidannotations:${androidAnnotationsVersion}"
    ajc "com.squareup.dagger:dagger-compiler:${daggerVersion}"
    compile "com.squareup.dagger:dagger:${daggerVersion}"
}

现在,


:app:generateDebugSources UP-TO-DATE
Note: Resolve log file to /Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/androidannotations.log
Note: Initialize AndroidAnnotations 3.1 with options {}
Note: Start processing for 2 annotations on 13 elements
error: Could not find the AndroidManifest.xml file, going up from path [/Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/debug] found using dummy file [] (max atempts: file:///Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/debug/dummy1412644771646)
Note: Time measurements: [Whole Processing = 10 ms], [Extract Annotations = 7 ms], [Extract Manifest = 2 ms], 
Note: Finish processing
Note: Start processing for 0 annotations on 0 elements
Note: Time measurements: [Whole Processing = 0 ms], 
Note: Finish processing
1 error
:app:compileDebugJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.596 secs
Compilation failed; see the compiler error output for details.
10:19:31: External task execution finished 'build'.

androidannotations.log 如下。


10:17:30.890 [Daemon Thread 2] INFO  o.a.AndroidAnnotationProcessor:84 - Initialize AndroidAnnotations 3.1 with options {}
10:17:31.58 [Daemon Thread 2] INFO  o.a.AndroidAnnotationProcessor:108 - Start processing for 2 annotations on 13 elements
10:17:31.77 [Daemon Thread 2] ERROR o.a.h.AndroidManifestFinder:134 - Could not find the AndroidManifest.xml file, going up from path [/Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/debug] found using dummy file [] (max atempts: file:///Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/debug/dummy1412644651076)
10:17:31.78 [Daemon Thread 2] INFO  o.a.p.TimeStats:81 - Time measurements: [Whole Processing = 19 ms], [Extract Annotations = 15 ms], [Extract Manifest = 2 ms],
10:17:31.78 [Daemon Thread 2] INFO  o.a.AndroidAnnotationProcessor:122 - Finish processing
10:19:31.568 [Daemon Thread 3] INFO  o.a.AndroidAnnotationProcessor:84 - Initialize AndroidAnnotations 3.1 with options {}
10:19:31.637 [Daemon Thread 3] INFO  o.a.AndroidAnnotationProcessor:108 - Start processing for 2 annotations on 13 elements
10:19:31.647 [Daemon Thread 3] ERROR o.a.h.AndroidManifestFinder:134 - Could not find the AndroidManifest.xml file, going up from path [/Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/debug] found using dummy file [] (max atempts: file:///Users/flask/Documents/workspace_android/AspectJTest/app/build/intermediates/classes/debug/dummy1412644771646)
10:19:31.648 [Daemon Thread 3] INFO  o.a.p.TimeStats:81 - Time measurements: [Whole Processing = 10 ms], [Extract Annotations = 7 ms], [Extract Manifest = 2 ms],
10:19:31.648 [Daemon Thread 3] INFO  o.a.AndroidAnnotationProcessor:122 - Finish processing

最佳答案

而不是 AspectJ 插件 - com.uphyca.gradle:gradle-android-aspectj-plugin' 尝试使用 https://github.com/Archinamon/GradleAspectJ-Android用于 dagger+aspectj 插件集成。

首先将 Maven 存储库链接添加到模块构建文件的存储库 block 中:

maven { url 'https://github.com/Archinamon/GradleAspectJ-Android/raw/master' }

将插件添加到构建脚本的依赖项部分:

classpath 'com.archinamon:AspectJ-gradle:1.0.15'

应用aspectj插件:

apply plugin: 'com.archinamon.aspectj'

现在构建您的项目并运行。

关于android - 当我将 AspectJ 应用于带有 Androidannotations 的 Android 项目时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199972/

相关文章:

android - 如何解决错误 :(5) No resource identifier found for attribute 'iconTint' in package 'android' ?

android - 修复下载完成后出现的 ProgressDialog

android - 添加 uCrop 和 Cometchat 库时 Gradle "Duplicate class"错误

android - 如何以一致的版本控制发布多个依赖的android库?

安卓 Lint : How to disable "NewerVersionAvailable" check for testCompile dependencies?

java - 方面是否在自己的线程中执行?

java - 使用 AspectJ LTW 时的 Spring 缓存问题

android - 如何使用 GUI 显示多个过滤器来过滤 logcat?[Android Studio]

android - 如何使用 RecyclerView android 只选择一个 RadioButton?

annotations - spectj - 如何查找带注释的类的方法正在调用同一类的另一个方法。即嵌套调用