When I apply AspectJ to Android project with Androidannotations not work

依然范特西╮ 提交于 2020-01-03 17:17:24

问题


I'm using Androidstudio 0.8.9 and build with gradle.

I was using Android-Annotations and works well. And I want use AspectJ also, so apply plugin(https://github.com/uPhyca/gradle-android-aspectj-plugin).

But compile fail, and throw some error message

: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}"
}

So, I tested aspectj plugin work alone(without android-annotations), and works well. And I find some answer (Android Annotations and MonkeyTalk?), FAQ Page of AA. But it was eclipse solution I think, and I can't solve my problem with this answer.

I changed annotation processor android-apt to ajc. But now, It can't find AndroidManifest.xml file. (because apt configuration is not available)

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}"
}

Now,


: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 is below.


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:


Instead of AspectJ plugin - com.uphyca.gradle:gradle-android-aspectj-plugin' try to use https://github.com/Archinamon/GradleAspectJ-Android for dagger+aspectj plugin integration.

First add a maven repo link into your repositories block of module build file:

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

Add the plugin to your buildscript's dependencies section:

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

Apply the aspectj plugin:

apply plugin: 'com.archinamon.aspectj'

Now build your project and run.



来源:https://stackoverflow.com/questions/26199972/when-i-apply-aspectj-to-android-project-with-androidannotations-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!