2017-12-20 16 views
-5

DaggerNetComponentはがシンボルを解決できません 'DaggerNetComponent'

public class MyApp extends Application 
     { 
      NetComponent netComponent; 

      @Override 
      public void onCreate() { 
       super.onCreate(); 
       netComponent = DaggerNetComponent.builder().netModule(new NetModule("https://demourl")).appModule(new AppModule(this)).build(); 
      } 

      public NetComponent provideNetComponent() { 
       return netComponent; 
      } 

     } 

を解決することができず、

以下のようにApplicationModuleとNetModuleを作成AppModule

@Module 
    public class AppModule implements AppModuleInterface 
    { 
    Application application; 
    SharedPreferences sharedPreferences; 

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

    @Provides 
    @Singleton 
    @Override 
    public Application provideApplication() 
    { 
     return application; 
    } 



} 

NetModule

@Module 
public class NetModule implements NetModuleInterface { 

    String mBaseUrl; 

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

@Singleton 
@Provides 
    @Override 
    public SharedPreferences provideSharedPreferences(Application application) { 
     return null; 
    } 

    @Singleton 
    @Provides 
    @Override 
    public Cache provideOkHttpCache(Application application) { 
     /* 10 MB cache size */ 
     int cacheSize=10*1024*1024; 

     Cache cache=new Cache(application.getCacheDir(),cacheSize); 
     return cache; 
    } 

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

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


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

およびNetComponent

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

そしてMainActivityに私はSharedPreferencesのための注射をしようとしていますが、OkHttpClientとレトロフィットは、すべての短剣、注釈、Gson、OkHttp、レトロフィット

public class MainActivity extends AppCompatActivity 
{ 
    @Inject OkHttpClient okHttpClient; 
    @Inject SharedPreferences sharedPreferences; 
    @Inject Retrofit retrofit; 

    private TextView injectionStatusTvid; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     injectionStatusTvid=findViewById(R.id.injectionStatusTvid); 
     ((MyApp) getApplication()).provideNetComponent().inject(this); 

     boolean injectionStatus=(retrofit==null?false:true); 
     injectionStatusTvid.setText("Dependency injection status:"+injectionStatus); 


    } 
} 

アプリケーションのGradle

dependencies { 
     implementation fileTree(dir: 'libs', include: ['*.jar']) 
     implementation 'com.android.support:appcompat-v7:26.1.0' 
     implementation 'com.android.support.constraint:constraint-layout:1.0.2' 
     testImplementation 'junit:junit:4.12' 
     androidTestImplementation 'com.android.support.test:runner:1.0.1' 
     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 


     /*dagger*/ 
     compile 'com.google.dagger:dagger:2.14' 
     compile 'com.google.dagger:dagger-android:2.14' 
     annotationProcessor 'com.google.dagger:dagger-android-processor:2.14' 
     annotationProcessor 'com.google.dagger:dagger-compiler:2.14' 

     /*Retrofit and gson*/ 
     compile 'com.google.code.gson:gson:2.6.2' 
     compile 'com.squareup.retrofit2:retrofit:2.0.2' 
     compile 'com.squareup.retrofit2:converter-gson:2.0.2' 

     /*okHttp*/ 
     implementation 'com.squareup.okhttp3:okhttp:3.9.1' 



    } 

オブジェクトライブラリがグラデルファイルに追加されました

どうすればこの問題を解決できますか?ダガーに新しいです。はまった。

ありがとうございました!

+0

おそらくダガーに間違いがあります。プロジェクトをビルドし、ログのエラーをチェックします。私はどこにでもSharedPreferencesを提供しているとは思わない。 –

+0

私はNetModuleに共有の設定コードを書いています。 Preferenceオブジェクトを提供するように修正しました。私はgithubからデモプロジェクトをダウンロードし、Androidスタジオでそれを実行しようとしました。同じエラーが発生しています。返信DaggerComponent – PrashanthYN

答えて

0

DaggerNetComponentファイルは、build > generatedパスその後、

のMyApp(Applicationクラス) `で次に

import DaggeNetComponentと下のファイルを作成しますrebuild/Make Projectはあなたが行ってもいいですしてみてください作成されていない可能性があります!

+0

応答に感謝します。私はその時があるかもしれない。私はIDEコンパイラセクションではアノテーションプロセッサを有効にしていますが、使用しません。 – PrashanthYN

0

最終的には機能しました!

Daggerライブラリに問題がありました。私はこの問題を解決するために別のライブラリバージョンを使用しました。

compile 'com.google.dagger:dagger:2.8' 
    annotationProcessor 'com.google.dagger:dagger-compiler:2.8' 
    provided 'javax.annotation:jsr250-api:1.0' 

開発者は適切なライブラリバージョンを確認する必要があります。

関連する問題