javac can scan both source files (java) and classes looking for annotations: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#processing
This means that you can do this work if you do not have the classes created by the dagger that the Kotlin code refers to (which means the implementation of the Dagger modules)
- call the kotlin compiler (no codes of generated types in Kotlin code)
- call annotation handler (processes annotations both in java files and in files compiled by kotlin)
- invoke java compiler - has access to both dagger-generated types and Kotlin types
You can write your services in both java and kotlin, but the module must be created using the java class
Here is the corresponding pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>testkotlindagger</artifactId> <version>1.0-SNAPSHOT</version> <properties> <kotlin.version>1.0.6</kotlin.version> <dagger2.version>2.7</dagger2.version> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger</artifactId> <version>${dagger2.version}</version> </dependency> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger-compiler</artifactId> <version>${dagger2.version}</version> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> <configuration> <sourceDirs> <source>src/main/java</source> <source>src/main/kotlin</source> </sourceDirs> </configuration> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>2.2.4</version> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>compile</phase> <configuration> <outputDirectory>target/generated-sources/annotations</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <executions> <execution> <id>default-compile</id> <phase>none</phase> </execution> <execution> <id>default-testCompile</id> <phase>none</phase> </execution> <execution> <id>java-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>java-test-compile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
On the other hand, if you include types generated by the dagger in your kotlin code, you must have them available before compiling the kotlin code, which means you need the Kotlin comment handler (KAPT)
In this scenario, the problem boils down to the question: Is kapt supported in maven?
Unfortunately, the answer is no, but there is an error filed to support it: https://youtrack.jetbrains.com/issue/KT-14478
source share