【问题标题】:Android AOP libraryAndroid AOP 库
【发布时间】:2020-09-03 19:41:15
【问题描述】:

我用com.archinamon.aspectj (https://github.com/Archinamon/android-gradle-aspectj) 创建了简单的AOP 跟踪项目。一切正常,我能够记录一些带有方面的 android 活动(在单模块项目中)。

问题是我需要创建独立的库以在多个项目中重用它。我用这样的类创建了新的 android 库模块(使用本教程 https://developer.android.com/studio/projects/android-library):

package com.example.android_tracker
@Aspect
class LoggingAspect {

private val logger = Logger.getLogger("Logger")

@Pointcut("execution(void *.onClick(..))")
fun onClick() {
}

@Before("onClick() && args(view)")
fun beforeButtonClick(view: View) {
    if (view is TextView) {
        val text = view.text.toString();
        logger.log(
            Level.WARNING, "LOGGER: " +
                    "Logging aspect before onClick $text"
        )
    }
}

当我运行应用程序时出现以下错误:

java.lang.NoSuchMethodError: No static method aspectOf()Lcom/example/android_tracker/LoggingAspect; in class Lcom/example/android_tracker/LoggingAspect; or its super classes (declaration of 'com.example.android_tracker.LoggingAspect' appears in /data/app/com.example.aop_poc-lD_Wk32s3ddwX0esY9bGcw==/base.apk)

配置:

- project build gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
    google()
    jcenter()
    mavenCentral()
}
dependencies {
    classpath "com.android.tools.build:gradle:4.0.1"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath("com.archinamon:android-gradle-aspectj:4.2.1") //!!!
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

-library module

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.archinamon.aspectj-provides' //!!!

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

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

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.1'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

- app module

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.archinamon.aspectj' //!!!


android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.aop_poc"
        minSdkVersion 16
        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
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
    implementation project(':android-tracker')   //!!!
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

aspectj {
    includeAspectsFromJar ':android-tracker' //!!!
}

有没有人遇到过类似的问题并知道解决办法?

【问题讨论】:

    标签: android kotlin gradle aop


    【解决方案1】:

    免责声明:我是 AspectJ + Maven 人,而不是 Gradle 用户。另外,我不开发 Android 应用程序。因此,即使是 AspectJ 专家,我对您的方案的经验也为零。所以在验证我的答案时请小心。

    在 AspectJ 中有两种使用方面库的基本方法:

    1. 使用编译时编织以便为您的应用程序创建编织字节码。为此,您的应用程序需要使用 AspectJ 编译器和切面路径上的切面库进行编译。也许这就是你想要做的,我不会说 Gradle,也不能告诉你你在那里做错了什么。但请确保稍后在您的设备上运行应用程序时,aspectjrt 库位于类路径中。如果没有 AspectJ 运行时,您可能会遇到您发布的错误。

    2. 使用加载时编织,即使用-javaagent:/path/to/aspectjweaver.jar ... -cp /path/to/my-aspect-library.jar ...。但是 AFAIK 这不是 Android 的选项,因为 Dalvik 不是支持经典 Java 代理的普通 JVM。

    因此,您可能想调查选项 1 并修复您的类路径。您可能还错误地配置了一个或两个模块中的 AspectJ 插件。我没有阅读插件说明,因为我对 Gradle 真的不感兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-03
      • 2012-10-27
      • 2011-09-22
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      相关资源
      最近更新 更多