question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated Java code doesn't compile with Kotlin

See original GitHub issue

I’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:

  1. Java version of the same Module builds fine:
@Module
public interface FeedDetailsActivityModuleJava {
    @Binds
    FeedDetailsViewModel.Factory viewModuleFactory(FeedDetailsViewModel_AssistedFactory factory);
}
  1. 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Chang-Ericcommented, Dec 4, 2020

I think this is probably an issue with needed to set correctErrorTypes in kapt.

kapt {
 correctErrorTypes true
}

Going to close this assuming that is the fix, but can reopen if it doesn’t work.

0reactions
nlgtuankietcommented, Apr 3, 2019

Several line of code will do

class Bar @Inject constructor()

class Foo @AssistedInject constructor(
    @Assisted bar: Bar,
    bar2: Bar
) {
    @AssistedInject.Factory
    interface Factory {
        fun create(bar: Bar): Foo
    }
}

@Module
interface Binding {
    @Binds
    @IntoSet
    fun bind(factory: Foo_AssistedFactory): Foo.Factory
}

@Component(modules = [Binding::class])
interface Component

Source: https://github.com/nlgtuankiet/dagger_kotlin_java_issue Run kaptDebugKotlin

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found