Generated Java code doesn't compile with Kotlin
See original GitHub issueI’m using AssistedInject library to generate factories. Build fails when I map factory interface to its generated implementation via @Binds
in a @Module
. How can I make it work?
ViewModel:
class FeedDetailsViewModel @AssistedInject constructor(
@Assisted private val feedId: FeedId,
private val getFeedUseCase: GetFeedUseCase
) : BaseViewModel() {
...
@AssistedInject.Factory
interface Factory {
fun create(feedId: FeedId): FeedDetailsViewModel
}
}
Generated factory:
public final class FeedDetailsViewModel_AssistedFactory implements FeedDetailsViewModel.Factory {
private final Provider<GetFeedUseCase> getFeedUseCase;
@Inject
public FeedDetailsViewModel_AssistedFactory(Provider<GetFeedUseCase> getFeedUseCase) {
this.getFeedUseCase = getFeedUseCase;
}
@Override
public FeedDetailsViewModel create(FeedId feedId) {
return new FeedDetailsViewModel(
feedId,
getFeedUseCase.get());
}
}
Module:
@Module
interface FeedDetailsActivityModuleKotlin {
@Binds
fun viewModelFactory(factory: FeedDetailsViewModel_AssistedFactory): FeedDetailsViewModel.Factory
}
Build fails with the following error:
e: /app/build/tmp/kapt3/stubs/debug/../FeedDetailsActivityModuleKotlin.java:11: error: @Binds methods must have only one parameter whose type is assignable to the return type
public abstract com.nikolaykul.sebastian.presentation.feed.details.FeedDetailsViewModel.Factory viewModelFactory(@org.jetbrains.annotations.NotNull()
A few notes:
- Java version of the same Module builds fine:
@Module
public interface FeedDetailsActivityModuleJava {
@Binds
FeedDetailsViewModel.Factory viewModuleFactory(FeedDetailsViewModel_AssistedFactory factory);
}
- Using
@AssistedModule
(generated module with all mappings) builds fine as well. I guess thats because it generates java-code:
@Module(includes = [AssistedInject_ViewModelModule::class])
interface ViewModelModule
@Module
public abstract class AssistedInject_ViewModelModule {
private AssistedInject_ViewModelModule() {
}
@Binds
abstract FeedDetailsViewModel.Factory bind_com_nikolaykul_sebastian_presentation_feed_details_FeedDetailsViewModel(
FeedDetailsViewModel_AssistedFactory factory);
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
gradle fails to compile java classes using kotlin class
This is a known issue of the java-library plugin: when used in a project with another JVM language (Kotlin, Scala, Groovy etc.) ...
Read more >Kapt: Compiler can't find generated code unless building a ...
When doing so using Kotlin 0.12.613, things work as they should. When using 0.12.1218, the Java compiler complains that it cannot find the...
Read more >Protoc generates lite Kotlin/Java that doesn't compile #8697
Protoc generates lite Kotlin/Java that doesn't compile #8697 ... Try to compile the code with protobuf-javalite as a dependency; See error.
Read more >Calling Java from Kotlin
Existing Java code can be called from Kotlin in a natural way, and Kotlin ... in Kotlin because Kotlin doesn't support set-only properties....
Read more >Building Java & JVM projects - Gradle User Manual
Groovy Kotlin ... A javadoc task that generates Javadoc for the main classes ... By default, Gradle will compile Java code to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this is probably an issue with needed to set correctErrorTypes in kapt.
Going to close this assuming that is the fix, but can reopen if it doesn’t work.
Several line of code will do
Source: https://github.com/nlgtuankiet/dagger_kotlin_java_issue Run kaptDebugKotlin