99import org .gradle .api .artifacts .Configuration ;
1010import org .gradle .api .initialization .Settings ;
1111import org .gradle .api .internal .GradleInternal ;
12+ import org .gradle .api .invocation .Gradle ;
1213import org .gradle .api .tasks .compile .JavaCompile ;
1314
1415public class ScipGradlePlugin implements Plugin <Project > {
1516
17+ private static final List <String > ANDROID_PLUGINS =
18+ List .of (
19+ "com.android.application" ,
20+ "com.android.library" ,
21+ "com.android.test" ,
22+ "com.android.dynamic-feature" );
23+
24+ private static final List <String > ANDROID_ANNOTATION_PROCESSORS =
25+ List .of ("annotationProcessor" , "testAnnotationProcessor" , "androidTestAnnotationProcessor" );
26+
1627 @ Override
1728 public void apply (Project project ) {
1829 project .afterEvaluate (this ::configureProject );
@@ -37,18 +48,26 @@ private void configureProject(Project project) {
3748 project .getExtensions ().getExtraProperties ().getProperties ();
3849
3950 String targetRoot = requiredExtra (extraProperties , "scipTarget" ).toString ();
40- String sourceRoot = project .getRootDir ().toString ();
51+ // The directory the root build was started in, not project.getRootDir():
52+ // buildSrc and included builds have their own root directory.
53+ Gradle rootBuild = project .getGradle ();
54+ while (rootBuild .getParent () != null ) rootBuild = rootBuild .getParent ();
55+ String sourceRoot = rootBuild .getStartParameter ().getCurrentDir ().toString ();
4156
4257 // Compilation tasks we need to trigger to index all the sources we care
4358 // about. Built up as we detect the java and kotlin plugins.
4459 List <String > triggers = new ArrayList <>();
4560
46- if (project .getPlugins ().hasPlugin ("java" )) {
47- triggers .add ("compileJava" );
48- triggers .add ("compileTestJava" );
61+ boolean isAndroid = ANDROID_PLUGINS .stream ().anyMatch (id -> project .getPlugins ().hasPlugin (id ));
62+
63+ if (project .getPlugins ().hasPlugin ("java" ) || isAndroid ) {
64+ if (!isAndroid ) {
65+ triggers .add ("compileJava" );
66+ triggers .add ("compileTestJava" );
67+ }
4968
5069 Object javacPluginDep = project .files (requiredExtra (extraProperties , "javacPluginJar" ));
51- boolean pluginAdded = tryAddJavacPlugin (project , javacPluginDep );
70+ boolean pluginAdded = tryAddJavacPlugin (project , javacPluginDep , isAndroid );
5271
5372 project
5473 .getTasks ()
@@ -142,10 +161,20 @@ private static boolean hasAnnotationProcessors(Project project, String configura
142161 }
143162 }
144163
145- private static boolean tryAddJavacPlugin (Project project , Object javacPluginDep ) {
164+ private static boolean tryAddJavacPlugin (
165+ Project project , Object javacPluginDep , boolean isAndroid ) {
146166 try {
147167 project .getDependencies ().add ("compileOnly" , javacPluginDep );
148168 project .getDependencies ().add ("testCompileOnly" , javacPluginDep );
169+ if (isAndroid ) {
170+ // AGP always passes -processorpath, so javac only finds the plugin there.
171+ for (String name : ANDROID_ANNOTATION_PROCESSORS ) {
172+ if (project .getConfigurations ().findByName (name ) != null ) {
173+ project .getDependencies ().add (name , javacPluginDep );
174+ }
175+ }
176+ return true ;
177+ }
149178 if (hasAnnotationProcessors (project , "annotationProcessor" )) {
150179 project .getDependencies ().add ("annotationProcessor" , javacPluginDep );
151180 }
0 commit comments