当我将AspectJ应用于Android项目并且Android注释不起作用时

时间:2014-10-05 05:39:06

标签: 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'.

我的build.gradle

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。 但我认为这是一个日食解决方案,我无法用这个答案来解决我的问题。

我将注释处理器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

1 个答案:

答案 0 :(得分:1)

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

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

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

将插件添加到buildscript的依赖项部分:

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

应用aspectj插件:

apply plugin: 'com.archinamon.aspectj'

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