android 中未生成 DaggerComponents

DaggerComponents not getting generated in android

我面临的问题:

//DaggerNetComponent is not getting generated
mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this)) 
                .netModule(new NetModule("https://api.github.com"))
                .build();

网络模块

import android.app.Application;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


@Module
public class NetModule {
    String mBaseUrl;

    public NetModule(String mBaseUrl) {
        this.mBaseUrl = mBaseUrl;
    }


    @Provides
    @Singleton
    Cache provideHttpCache(Application application) {
        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(application.getCacheDir(), cacheSize);
        return cache;
    }

    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        return gsonBuilder.create();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkhttpClient(Cache cache) {
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.cache(cache);
        return client.build();
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(mBaseUrl)
                .client(okHttpClient)
                .build();
    }
}

应用模块

import android.app.Application;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;


@Module
public class AppModule {

    Application mApplication;

    public AppModule(Application mApplication) {
        this.mApplication = mApplication;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return mApplication;
    }
}

网络组件

import javax.inject.Singleton;

import dagger.Component;
import retrofit.modules.AppModule;
import retrofit.modules.NetModule;

@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
    void inject(MainActivity activity);
}

如何调试它以便我能够生成它 class

我认为这是因为 NetModule 中的 Cache provideHttpCache(Application application)

是的,您在 AppModule 内提供了 Application,但它与 NetComponent 没有任何关联。

所以,有以下三种方式:

1。只需将 Application 手动传递到 NetComponent 中,就像使用 AppComponent 一样(通过构造函数)

或(更好的方法)

2。为 AppModule 创建单独的 Component - AppComponent 应该向其他组件提供 Application

@Component(modules = {
   AppModule.class,
})
@Singleton
public interface AppComponent {
   Application application();
}

然后让你的 NetComponent 依赖于 AppComponent:

@Component(
   dependencies = AppComponent.class,
   modules = NetModule.class
)
@NetScope
public interface NetComponent {
   void inject(AddTranslationActivity activity);
}

3。按照项目 2 中的描述执行相同操作,但使用 Dagger subcomponents

如果您没有收到构建错误并且 Dagger 组件 class 不存在,则听起来 Dagger 注释处理器不是 运行。

您需要。例如:

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.11'
    annotationProcessor "com.google.dagger:dagger-compiler:2.11"
}

如果仍然无效,请尝试使缓存失效:

File>>Invalidate Caches\Restart>>Invalidate and Restart