Preface: This article will not explain in detail what AspectJ is. Anyone who finds this article is very likely to have a problem configuring AspectJ in Android Studio. This article is a detailed solution to the problem. The target Gradle version is 7.3.3, but This article should apply to version 7.xx
! ! ! ! This configuration does not need to reduce the gradle version
Article content
1. Detailed explanation of successful configuration Step
2. Problems encountered
1. Detailed steps for successful configuration
1. Add classpath reference
- Due to Gradle7 syntax changes and warehouse management changes, the outermost build.gradle (project-level build.gradle) is missing the following code:
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"
}
}
}
The above code becomes inside the project setting.gradle:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
// repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
}
}
As a result, everyone does not know how to introduce the classpath. In fact, you only need to add in the project-level build.gradle:
buildscript {
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
}
Note: Be sure to write on the plugins{} module, which is the basic format of the gradle configuration
- Another thing is to modify the version of the plug-in, change 7.2.1 to 7.1.3, otherwise it is not compatible
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
}
2. Modify the setting.gradle configuration
- Add the jcenter image of Alibaba Cloud (because aspectjx is stored in jcenter).
I directly imported multiple images of Alibaba Cloud. Readers can try to import only this. It is reasonable to say that there is no problem (if the Alibaba image library circled above is missing aspectjx will not be found) - Modify the parameters of repositoriesMode to RepositoriesMode.PREFER_SETTINGS (the original parameter is RepositoriesMode.FAIL_ON_PROJECT_REPOS)
3. Add plugin references in the build.gradle of the app module
apply plugin: 'android-aspectjx'
Note: It is valid only if it is added to the application, and it is invalid to add a plug-in reference to the library. In componentization, which component needs to use AspectJx, add this plug-in reference.
- Then add in the build.gradle of the app module:
aspectjx {
exclude 'androidx', 'com.google', 'com.squareup', 'com.alipay', 'com.taobao',
'org.apache',
'org.jetbrains.kotlin',
"module-info", 'versions.9'
}
Note: aspectjx{} blocks are at the same level as android{}
4. Finally, build an android library and add dependencies to this build.gradle
dependencies {
api "org.aspectj:aspectjrt:1.9.5"
}
The api or implementation depends on your own needs, it’s okay
- Note: Introducing this "org.aspectj:aspectjrt:1.9.5" just wants to use the annotations, api, etc. inside, so which module needs to use AspectJ can be imported in which project.
Strictly follow the above configuration to solve the problem. Below I give all the code needed:
- Global build.gradle (project level)
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()
}
}
- build.gradle of the app module
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'
}
- build.gradle under the android library
plugins {
id 'com.android.library'
}
android {
}
dependencies {
api "org.aspectj:aspectjrt:1.9.5"
}
2. Problems encountered
1.Execution failed for task ‘:app:dexBuilderDebug’.java.util.zip.ZipException: zip file is empty
- Solution: Be sure to add aspectjx code block
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
- Solution: The plug-in version must be changed to 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’
- Solution: Modify the repositoriesMode of setting.gradle
//> 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.
- Solution: Add classpath dependency in global build.gradle
buildscript {
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
}
This is the end of the article. If readers encounter other problems, please leave a comment and discuss together.
Reference to successful solutions:
Short book solution 1
Short book solution 2
Other possible solutions:
Brief description of solution three