代码之家  ›  专栏  ›  技术社区  ›  TheIcemanCometh

在AndroidStudio中使用AspectJ.aj文件,编织似乎不会发生

  •  0
  • TheIcemanCometh  · 技术社区  · 9 年前

    我是AspectJ的新手,我们正在努力将最初使用Eclipse编写的第三方应用程序迁移到使用AndroidStudio 1.1.0和Gradle。我们已经获取了这个应用程序需要的外部库,并在项目中创建了一个库模块,这个库有一个AspectJ .aj文件 我们需要编译的文件,并与主应用程序一起使用,以获得字段级的Observable模式。使用找到的插件 here ,我已经能够 .aj文件 要编译为的文件 文件,通过查看 intermediates 文件夹

    问题出现在“编织”步骤中,该步骤中的代码应该被注入到必要类的字节码中。这似乎并没有发生,因为当字段更改时应该通知的侦听器并没有发生。以下是我的构建文件。

    项目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')
    }
    

    “blashblah”库模块构建.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;
                }
            }
        }
    }
    

    我错过了什么?

    1 回复  |  直到 9 年前
        1
  •  0
  •   TheIcemanCometh    9 年前

    经过更多的研究,我已经能够使一切按预期进行。以下是最终的build.gradle文件:

    项目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;
                }
            }
        }
    
    }
    

    “blashblah”库模块构建.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
        }
    }