【问题标题】:Android gradle custom Plugin error after updating android gradle plugin to 7.0将android gradle插件更新到7.0后Android gradle自定义插件错误
【发布时间】:2021-12-02 20:44:40
【问题描述】:

以下是我的自定义插件代码

import com.android.annotations.NonNull;
import com.android.build.gradle.LibraryExtension;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;


public class CustomPlugin implements Plugin<Project> {

@Override
public void apply(@NonNull Project project) {
    project.getPluginManager().withPlugin("com.android.library", plugin -> {
        //code here
    });
}

}

将android gradle插件从4.2.1升级到7.0以上自定义插件代码显示error: package com.android.annotations does not exist

如何解决这个错误?

【问题讨论】:

    标签: android gradle annotations android-gradle-plugin gradle-custom-plugin


    【解决方案1】:

    由于我和 alphanso 在同一个项目中工作并从他那里接管了 AGP 升级,所以我终于发现了:

    代码位于我们项目的.buildSrc 目录中,并使用Gradle 插件java-libraryjava-gradle-plugin 在那里编译。

    因此,android.useAndroidX=true 无效,因为这仅在您使用 Android 插件(com.android.librarycom.android.application)时才有效。

    突然丢失类的根本原因(我们在com.android.build 的子包中也丢失了一些其他类)在其他地方:

    使用 AGP 7.0,com.android.tools.build:gradle 的大多数库依赖项从“编译时”更改为“运行时”,您可以在此处看到:

    AGP 4.2.2: https://mvnrepository.com/artifact/com.android.tools.build/gradle/4.2.2

    AGP 7.0: https://mvnrepository.com/artifact/com.android.tools.build/gradle/7.0.0

    这意味着当切换到 AGP 7.x 时,您必须手动添加缺少的依赖项。

    在我们的例子中添加

    implementation "com.android.tools:common:30.0.3"
    

    解决了问题。

    【讨论】:

      【解决方案2】:

      在android gradle plugin 7.0中,推荐使用AndroidX Libraries。首先,在您的 gradle.properties 文件中,通过添加启用 AndroidX

      android.useAndroidX=true
      

      也添加这些依赖项(以设置其他 androidx 库),

      implementation 'androidx.appcompat:appcompat:1.3.1'
      implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
      androidTestImplementation 'androidx.test.ext:junit:1.1.3'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
      

      同步您的 gradle 文件,然后将导入 com.android.annotations.NonNull 更改为 androidx.annotations.NonNull

      【讨论】:

        猜你喜欢
        • 2021-11-30
        • 2020-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多