• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

AOP 面向切面编程Android Studio 配置 AspectJ ( 下载并配置AS jar 包 | 配置 Gradle 和 Gradle 插件版本 | 配置 Gradle 构建脚本 )

武飞扬头像
韩曙亮
帮助1





一、AspectJ 下载


首先 , 参考 【AOP 面向切面编程】AOP 简介 ( AspectJ 简介 | AspectJ 下载 ) 三、AspectJ 下载 博客 , 下载 AspectJ ;

下载地址为 https://www.eclipse.org/aspectj/downloads.php , 这里下载 AspectJ 1.8.10, Released 12 Dec 2016 版本 , 下载链接为 https://ftp.jaist.ac.jp/pub/eclipse/tools/aspectj/aspectj-1.8.10.jar ;

下载下来的 aspectj-1.8.10.jar 文件有 16744 16744 16744 KB , 拷贝到 D:\AspectJ 目录中 , 解压该文件 :

学新通

进入 D:\AspectJ\aspectj-1.8.10\lib 目录 , aspectjrt.jar 是 AspectJ 的核心 jar 包 ;

学新通





二、拷贝 aspectjrt.jar 到 Android Studio


将上述 aspectjrt.jar 文件 , 拷贝到 Android Studio 工程的 AOP_Demo\app\libs 目录中 , 其中 AOP_Demo 是 AS 工程根目录 ;

学新通





三、配置 Gradle 和 Gradle 插件版本


参考 【错误记录】Android Studio 配置 AspectJ 报错 ( Failed to create Jar file C:\xxx\aspectjtools-1.8.10.jar. ) 博客 , 配置 Gradle 和 Gradle 插件版本 ;

Gradle 版本 : 在 \gradle\wrapper\gradle-wrapper.properties 中配置 5.6.4 5.6.4 5.6.4 版本的 Gradle ;

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip

Gradle 插件版本 : 3.6.1 3.6.1 3.6.1 版本的 Gradle 插件 ;

buildscript {
    dependencies {
        classpath "com.android.tools.build:gradle:3.6.1"
    }
}

学新通





四、配置 Gradle 构建脚本


配置 AspectJ 依赖 : implementation files('libs/aspectjrt.jar')

配置 AspectJ 编译选项 :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.aspectj:aspectjtools:1.8.10'
        classpath 'org.aspectj:aspectjweaver:1.8.10'
    }
}

import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

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.8",
                         "-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;
            }
        }
    }
}
学新通

完整的 build.gradle 配置脚本 :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.aspectj:aspectjtools:1.8.10'
        classpath 'org.aspectj:aspectjweaver:1.8.10'
    }
}

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.aop_demo"
        minSdkVersion 18
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

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

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.谷歌.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4. '
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation files('libs/aspectjrt.jar')
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfgeac
系列文章
更多 icon
同类精品
更多 icon
继续加载