0
votes

I'm trying to write a custom plugin that will generate some code (specifically, a Dagger module) off of some of the xml files in my android project. I've been able to get the code to generate, but when I try and use the generated module in a Dagger Component, the Dagger compiler fails:

I've put together a small demo project that demonstrates the failure. There are two modules: the plugin and the android app (FYI: I had to comment out the plugin-related code in the apps' build.gradle until I had installed the plugin locally).

When I rebuild the app, the generated module shows up right where I expect it, and the dagger component sees it in IJ. But it is apparently not available at the time when the android-apt plugin invokes the dagger compiler (or that location is not included in code the compiler is looking at).

I've made sure to have all the compile tasks depend on my "generate" task, and the failing task is compileDebugJavaWithJavac which should therefore know about my generated code.

For trying to include the source, I have:

AndroidSourceSet mainSourceSet = p.android.sourceSets.getByName('main')
LOG.info("Adding directory ${outputDir} to android source set ${mainSourceSet}")
mainSourceSet.java.srcDir(outputDir)

Despite this, I get:

Compiling with JDK Java compiler API.
C:\projects\java\android\android_plugin_demo\plugindemoapplication\src\main\java\com\bdl\plugindemoapplication\DaggerComponent.java:12: error: cannot find symbol
@Component(modules = PluginDaggerModule.class)
                     ^
  symbol: class PluginDaggerModule
C:\projects\java\android\android_plugin_demo\plugindemoapplication\src\main\java\com\bdl\plugindemoapplication\DaggerComponent.java:13: error: dagger.internal.codegen.ComponentProcessor was unable to process this interface because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public interface DaggerComponent {
       ^
2 errors

So, what magic am I missing to allow for the dagger compiler to see my generated code?

1

1 Answers

0
votes

After some more experimentation, I believe I have found the answer. I don't yet know why this works, but if I move the addition of the output directory to the AndroidSourceSet's srcDir to before the p.afterEvaluate closure, it appears to find it.

This seems odd to me as the task dependency is only added in the afterEvaluate block, and it looks like the new task was being executed prior to the compile, so even without this change, I'd have expected the srcDir add to also be executed prior to the compile attempt.

But at least I have it working now.