【发布时间】: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' //!!!
}
有没有人遇到过类似的问题并知道解决办法?
【问题讨论】: