How to fix DaggerSingletonComponent not resolved i

2019-07-26 23:48发布

问题:

I'm trying to set create the client first to test that the MQTT works without errors then I will implement the connect() method. I downloaded the latest version of HiveMQ (an open source MQTT implementation done in Java) and after importing the project properly as a Gradle build in Eclipse and using GIT I was greeted with an error message. It said "DaggerSingletonComponent cannot be resolved." My program can't run at all.

Link to the open source I downloaded: https://github.com/hivemq/hivemq-mqtt-client

I've tried manually editing the build files to see if there was some code left out for dagger in dependencies but there wasn't.

package com.hivemq.client.internal.mqtt.ioc;

import com.hivemq.client.internal.mqtt.netty.NettyEventLoopProvider;
import com.hivemq.client.internal.mqtt.netty.NettyModule;
import dagger.Component;
import org.jetbrains.annotations.NotNull;

import javax.inject.Singleton;

/**
 * Singleton component for all clients. It exists the whole application lifetime.
 *
 * @author Silvio Giebl
 */
@Component(modules = {NettyModule.class})
@Singleton  
public interface SingletonComponent {

    @NotNull SingletonComponent INSTANCE = DaggerSingletonComponent.create();

    @NotNull ClientComponent.Builder clientComponentBuilder();

    @NotNull NettyEventLoopProvider nettyEventLoopProvider();
}


__________________________
For the module: NettyModule.class


package com.hivemq.client.internal.mqtt.netty;

import dagger.Module;

import dagger.Provides;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.jetbrains.annotations.NotNull;

import javax.inject.Singleton;

/**
 * @author Silvio Giebl
 */
@Module
public abstract class NettyModule {

    @Provides
    @Singleton
    static @NotNull NettyEventLoopProvider provideNettyEventLoopProvider() {
        if (Epoll.isAvailable()) {
            return new NettyEventLoopProvider(EpollEventLoopGroup::new, EpollSocketChannel::new);
        } else {
            return new NettyEventLoopProvider(NioEventLoopGroup::new, NioSocketChannel::new);
        }
    }
}

Error Message: DaggerSingletonComponent cannot be resolved

回答1:

Dagger is a library that generates code for dependency injection at compile time. The mentioned class is one of the generated classes.

Please use gradle to build the project:

  1. Open a terminal
  2. Navigate to the project directory
  3. Execute ./gradlew build (Linux/Mac) or gradlew build (Windows)

You need to ensure that the directory build/generated/source/apt/main/ is configured as a source directory so that the IDE picks up the generated classes.

Then you should be able to use the build methods of your IDE after the first build with gradle.