diff --git a/java/dagger/internal/codegen/binding/ModuleDescriptor.java b/java/dagger/internal/codegen/binding/ModuleDescriptor.java index 62cfa7f108e..400bf7bfecf 100644 --- a/java/dagger/internal/codegen/binding/ModuleDescriptor.java +++ b/java/dagger/internal/codegen/binding/ModuleDescriptor.java @@ -79,6 +79,9 @@ public abstract class ModuleDescriptor { /** The kind of the module. */ public abstract ModuleKind kind(); + /** Whether the module is implicitly included rather than directly referenced in annotation. */ + public abstract Boolean isImplicitlyIncluded(); + /** Returns all of the bindings declared in this module. */ @Memoized public ImmutableSet allBindingDeclarations() { @@ -107,6 +110,7 @@ public static final class Factory implements ClearableCache { private final OptionalBindingDeclaration.Factory optionalBindingDeclarationFactory; private final DaggerSuperficialValidation superficialValidation; private final Map cache = new HashMap<>(); + private final Set implicitlyIncludedModules = new LinkedHashSet<>(); @Inject Factory( @@ -174,7 +178,8 @@ public ModuleDescriptor createUncached(XTypeElement moduleElement) { subcomponentDeclarationFactory.forModule(moduleElement), delegates.build(), optionalDeclarations.build(), - ModuleKind.forAnnotatedElement(moduleElement).get()); + ModuleKind.forAnnotatedElement(moduleElement).get(), + implicitlyIncludedModules.contains(moduleElement)); } private void collectCompanionModuleBindings( @@ -231,7 +236,10 @@ private Set collectIncludedModules( .ifPresent( moduleAnnotation -> { includedModules.addAll(moduleAnnotation.includes()); - includedModules.addAll(implicitlyIncludedModules(moduleElement)); + ImmutableSet daggerAndroidModules = + implicitlyIncludedModules(moduleElement); + includedModules.addAll(daggerAndroidModules); + implicitlyIncludedModules.addAll(daggerAndroidModules); }); return includedModules; } diff --git a/java/dagger/internal/codegen/writing/ComponentImplementation.java b/java/dagger/internal/codegen/writing/ComponentImplementation.java index a41b1c7935f..84dcaf4d3ef 100644 --- a/java/dagger/internal/codegen/writing/ComponentImplementation.java +++ b/java/dagger/internal/codegen/writing/ComponentImplementation.java @@ -42,6 +42,7 @@ import static javax.lang.model.element.Modifier.STATIC; import static javax.tools.Diagnostic.Kind.ERROR; +import androidx.room.compiler.processing.JavaPoetExtKt; import androidx.room.compiler.processing.XExecutableParameterElement; import androidx.room.compiler.processing.XMessager; import androidx.room.compiler.processing.XMethodElement; @@ -80,6 +81,7 @@ import dagger.internal.codegen.binding.ComponentRequirement; import dagger.internal.codegen.binding.KeyVariableNamer; import dagger.internal.codegen.binding.MethodSignature; +import dagger.internal.codegen.binding.ModuleDescriptor; import dagger.internal.codegen.compileroption.CompilerOptions; import dagger.internal.codegen.javapoet.CodeBlocks; import dagger.internal.codegen.javapoet.TypeNames; @@ -706,6 +708,15 @@ public void claimMethodName(CharSequence name) { public TypeSpec generate() { TypeSpec.Builder builder = classBuilder(name); + // Ksp requires explicitly associating input classes that are generated with the output class, + // otherwise, the cached generated classes won't be discoverable in an incremental build. + if (processingEnv.getBackend() == XProcessingEnv.Backend.KSP) { + graph.componentDescriptor().modules().stream() + .filter(ModuleDescriptor::isImplicitlyIncluded) + .forEach( + module -> JavaPoetExtKt.addOriginatingElement(builder, module.moduleElement())); + } + if (isComponentShard()) { TypeSpecs.addSupertype(builder, graph.componentTypeElement()); addCreator();