Following is my code for custom plugin
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
});
}
}
After upgrading android gradle plugin from 4.2.1 to 7.0 above custom plugin code showing error: package com.android.annotations does not exist
How to fix this error?
As I work in the same project as alphanso and took over the AGP upgrade from him, here is what I finally found out:
The code sits in the
.buildSrcdirectory of our project and is compiled there using the Gradle pluginsjava-libraryandjava-gradle-plugin.Therefore
android.useAndroidX=truehas no effect as this works only if you use the Android plugins, eithercom.android.libraryorcom.android.application.The root cause of the suddenly missing classes (we had a few other ones missing from sub packages of
com.android.buildas well) was somewhere else:With AGP 7.0 most library dependencies of
com.android.tools.build:gradlechanged from "compile time" to "run time" as you can see here: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
This means that when switching to AGP 7.x you must add the missing dependencies manually.
In our case adding
solved the problem.