【问题标题】:DaggerComponents not getting generated in androidDaggerComponents没有在android中生成
【发布时间】:2017-08-11 11:30:41
【问题描述】:

我面临的问题

//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);
}

如何调试这个以便我能够生成这个类

【问题讨论】:

  • 你能把 NetComponent 代码连同 AppModule 和 NetModule 一起粘贴到这里吗?
  • @AlexanderBilchuk ...我已添加代码
  • 您是否尝试重建您的项目?并注意到任何构建错误?
  • 没有构建错误...我重新构建了代码

标签: android dagger-2 dagger


【解决方案1】:

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

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

所以,有三种方式:

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

或(更好的方法)

2。为 AppModule - AppComponent 创建单独的 Component,这应该将 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

【讨论】:

    【解决方案2】:

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

    您需要add the annotation processors to your gradle dependencies。例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 2014-10-10
      • 2021-08-19
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多