使用AspectJ .aj文件与Android工作室,编织似乎不可能发生 [英] Using AspectJ .aj file with Android Studio, weaving appears to not be happening

查看:595
本文介绍了使用AspectJ .aj文件与Android工作室,编织似乎不可能发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的AspectJ,我们是在迁移使用Eclipse使用Android 1.1.0工作室和摇篮,而不是最初编写的第三方应用程序的工作。我们已经采取的外部库这个应用程序的需求,并在项目中创建一个库模块,而这个库有一个AspectJ的 .aj 文件,我们需要编译并工作,为现场的主要应用水平观测模式。使用插件发现这里,我已经能够得到在 .aj 文件编译成的.class 文件,通过查看中间体文件夹验证。

I am new to AspectJ, and we are working on migrating a third-party application originally written using Eclipse to use Android Studio 1.1.0 and Gradle instead. We have taken an external library this app needs and created a library module in the project, and this library has an AspectJ .aj file that we need to compile and have work with the main app for a field-level Observable pattern. Using the plugin found here, I have been able to get the .aj file to compile into a .class file, verified by looking in the intermediates folder.

问题是在编织的步骤,在此code被认为能注入必要的类的字节code到来。这并不出现以上问题,因为当一个场的变化,不应被通知应该侦听器。下面是我的构建文件。

The problem is coming in the 'weaving' step, where this code is supposed to be injected into the bytecode of the necessary classes. This does not appear to be happening, as the listeners that are supposed to be notified when a field changes, aren't. Below are my build files.

项目的build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.12'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

应用模块的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 14
        targetSdkVersion 19
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':blahblah')
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.google.android.gms:play-services:6.1.11'
    compile files('libs/commons-lang3-3.3.2.jar')
}

'blahblah'库模块的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'
apply plugin: 'android-aspectj'

def gsonVersion = '2.2.4'
def aspectjVersion = '1.8.5'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "com.google.code.gson:gson:${gsonVersion}"
    compile "org.aspectj:aspectjrt:${aspectjVersion}"
}

android {
    compileSdkVersion 19
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

android.libraryVariants.all { variant ->

    LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)

    variant.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;
            }
        }
    }
}

我是什么失踪?

推荐答案

更​​多的研究后,我已经能够得到的一切按预期工作。下面是最终的build.gradle文件:

After more research, I have been able to get everything working as expected. Here are the final build.gradle files:

项目的build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

应用模块的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.1'
    }

}

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

def gsonVersion = '2.2.4'
def aspectJVersion = '1.8.1'

dependencies {
    compile project(':blahblah')
    compile "org.aspectj:aspectjrt:${aspectJVersion}"
    compile 'com.android.support:support-v4:19.1.0'
    compile "com.google.code.gson:gson:${gsonVersion}"
    compile 'com.google.android.gms:play-services:6.1.11'
    compile files('libs/commons-lang3-3.3.2.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 14
        targetSdkVersion 19
    }

    lintOptions {
        abortOnError true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

// The section below is what performs the AOP "weaving" (injecting the AOP code into
// the appropriate classes).  Please do not remove.

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.5",
                         "-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;
            }
        }
    }

}

'blahblah'库模块的build.gradle:

def gsonVersion = '2.2.4'

buildscript {

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:1.1.0"
        classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.12'
    }

}

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

repositories {
    mavenCentral()
}

dependencies {
    compile "com.google.code.gson:gson:${gsonVersion}"
}

android {
    compileSdkVersion 19
    buildToolsVersion "22.0.1"
    lintOptions {
        abortOnError true
    }
}

这篇关于使用AspectJ .aj文件与Android工作室,编织似乎不可能发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆