前言:本文章就不详细说明AspectJ是什么玩意了,查到这篇文章的都极有可能是在Android Studio中配置AspectJ出现问题,本文就是解决问题的详细方案,目标Gradle版本7.3.3, 不过本文应该适用于7.x.x的版本
!!!!本配置不需要降低gradle版本
文章内容
一.配置成功的详细步骤
1.增加classpath引用
- 由于Gradle7的语法变动和仓库的管理变动,最外层的build.gradle(项目级的build.gradle) 缺失了如下代码:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://jitpack.io"
}
}
}
以上代码变成了项目setting.gradle里面的:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
// repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
}
}
导致大家不知道classpath如何引入,其实只需要在项目级build.gradle里面添加:
buildscript {
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
}
注意:一定要在plugins{}模块上面写,这是gradle配置的基本格式
- 还有就是修改插件的版本,将7.2.1改成7.1.3,否则不兼容
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
}
2.修改setting.gradle配置
- 增加阿里云的jcenter镜像(因为aspectjx存于jcenter中)
我是直接将阿里云的多个镜像直接导入的,读者可以试试只导入这个,按理说没问题 (如果缺少上面圈出来的阿里镜像库就会找不到aspectjx) - 修改repositoriesMode的参数,改为RepositoriesMode.PREFER_SETTINGS(原本的参数为RepositoriesMode.FAIL_ON_PROJECT_REPOS)
三.在app模块的build.gradle里面增加插件引用
apply plugin: 'android-aspectjx'
注意:一定要是application加才有效,library加了插件引用是无效的。 在组件化中,哪个组件需要用到AspectJx就加这个插件引用。
- 接着在app模块的build.gradle中添加:
aspectjx {
exclude 'androidx', 'com.google', 'com.squareup', 'com.alipay', 'com.taobao',
'org.apache',
'org.jetbrains.kotlin',
"module-info", 'versions.9'
}
注意:aspectjx{}块与android{}同级
四.最后再建立一个android library,并在此build.gradle里面添加依赖
dependencies {
api "org.aspectj:aspectjrt:1.9.5"
}
api或者implementation看自己需求就行了,都可以的
- 注意:引入这个"org.aspectj:aspectjrt:1.9.5"只是想用里面的注解,api等,所以哪个模块需要使用AspectJ就在哪个项目引入就可以了。
严格按以上配置就可以解决问题了。下面我给出所需要的全部代码:
- 全局的build.gradle(项目级)
buildscript {
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
}
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
}
- setting.gradle
pluginManagement {
repositories {
// maven {
// url 'https://maven.aliyun.com/repository/jcenter'
//阿里云的jcenter仓库废弃了
// }
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url 'https://maven.aliyun.com/repository/mapr-public'
}
maven {
url 'https://jitpack.io'
}
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
// maven {
// url 'https://maven.aliyun.com/repository/jcenter'
//阿里云的jcenter仓库废弃了
// }
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url 'https://maven.aliyun.com/repository/mapr-public'
}
maven {
url 'https://jitpack.io'
}
google()
mavenCentral()
}
}
- app模块的build.gradle
plugins {
id 'com.android.application'
}
apply plugin: 'android-aspectjx'
android {
defaultConfig {
}
}
aspectjx {
exclude 'androidx', 'com.google', 'com.squareup', 'com.alipay', 'com.taobao',
'org.apache',
'org.jetbrains.kotlin',
"module-info", 'versions.9'
}
- android library下的build.gradle
plugins {
id 'com.android.library'
}
android {
}
dependencies {
api "org.aspectj:aspectjrt:1.9.5"
}
二.遇到的问题
1.Execution failed for task ‘:app:dexBuilderDebug’.java.util.zip.ZipException: zip file is empty
- 解决:一定要添加aspectjx代码块
aspectjx {
// 排除一些第三方库的包名(Gson、 LeakCanary 和 AOP 有冲突)
// 否则就会起冲突:ClassNotFoundException: Didn't find class on path: DexPathList
//我遇到的 Execution failed for task ':app:dexBuilderDebug'.
//> java.util.zip.ZipException: zip file is empty
exclude 'androidx', 'com.google', 'com.squareup', 'com.alipay', 'com.taobao',
'org.apache',
'org.jetbrains.kotlin',
"module-info", 'versions.9'
}
2.Failed to apply plugin ‘android-aspectjx’. No such property: FD_INTERMEDIATES for class: com.android.builder.model.AndroidProject
- 解决:插件版本一定要改成7.1.3
plugins {
//下面俩个插件要是为7.2.1会导致在引入 apply plugin 'android-aspectjx' 不兼容,报错
//> Failed to apply plugin 'android-aspectjx'.
// > No such property: FD_INTERMEDIATES for class: com.android.builder.model.AndroidProject
//亲测改成7.1.3就没问题
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
}
3. Failed to apply plugin ‘android-aspectjx’. Build was configured to prefer settings repositories over project repositories but repository ‘MavenLocal’ was added by plugin ‘android-aspectjx’
- 解决:修改setting.gradle的repositoriesMode
//> Failed to apply plugin 'android-aspectjx'.
// > Build was configured to prefer settings repositories over project repositories but repository 'MavenLocal' was added by plugin 'android-aspectjx'
// repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 必须换成这个,否则会报上面这个错
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
4.A problem occurred evaluating project ‘:app’.Plugin with id ‘android-aspectjx’ not found.
- 解决:在全局build.gradle中添加classpath依赖
buildscript {
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
}