title: Dagger2 - Project Rebuild Error - Field Injection - Android
body: <p>I have been trying to implement Dagger2.</p>
  
  <p><strong>Problem:</strong> When I use constructor injection, it works fine but when I use field injection, it throws an Error like below:</p>
  
  <pre><code>Error:(6, 48) error: cannot find symbol class DaggerApplicationComponent
  /home/moderator/Downloads/Maulik/Sample Codes/Made/Dagger2Demo/app/src/main/java/com/dagger2demo/dagger2demo/di/component/ApplicationComponent.java
  Error:(18, 10) error: com.dagger2demo.dagger2demo.mvp.HomePresenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. This type supports members injection but cannot be implicitly provided.
  com.dagger2demo.dagger2demo.mvp.HomePresenter is injected at
  com.dagger2demo.dagger2demo.mvp.BaseActivity.homePresenter
  com.dagger2demo.dagger2demo.mvp.BaseActivity is injected at
  com.dagger2demo.dagger2demo.di.component.ApplicationComponent.inject(baseActivity)
  Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
  &gt; Compilation failed; see the compiler error output for details.
  </code></pre>
  
  <p><strong>Dagger2 - My Understanding:</strong> You have to create a Module class where you will create methods. These methods will give you respective object of your needed class like Retrofit, ApplicationContext etc.. You will create an component interface in which you will define where to inject module class's dependencies.</p>
  
  <p><strong>I'm using:</strong> Retrofit, RxJava - RaxAndroid, Dagger2 &amp; MVP.</p>
  
  <p>Code is below:</p>
  
  <p><strong>build.gradle(app)</strong></p>
  
  <pre><code>// Retrofit Dependency
  compile 'com.squareup.retrofit2:retrofit:2.3.0'
  
  // Gson Converter Factory Dependency
  compile 'com.squareup.retrofit2:converter-gson:2.3.0'
  
  // RxJava2 Adapter Dependency for Retrofit2
  compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
  
  // ButterKnife Dependencies
  compile 'com.jakewharton:butterknife:8.8.1'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
  
  // RxJava &amp; RxAndroid Dependencies
  compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.8'
  compile group: 'io.reactivex.rxjava2', name: 'rxandroid', version: '2.0.1'
  
  // Dagger2 Dependency
  compile 'com.google.dagger:dagger:2.14.1'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
  </code></pre>
  
  <p><strong>Dagger2DemoApplication.java</strong></p>
  
  <pre><code>    public class Dagger2DemoApplication extends Application {
  
      private ApplicationComponent mApplicationComponent;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          mApplicationComponent = DaggerApplicationComponent.builder()
                  .applicationModule(new ApplicationModule())
                  .build();
      }
  
      public ApplicationComponent getmApplicationComponent() {
          return mApplicationComponent;
      }
  }
  </code></pre>
  
  <p><strong>ApplicationModule.java</strong></p>
  
  <pre><code>    @Module
  public class ApplicationModule {
  
      @Provides
      @Singleton
      public APIEndPoints provideAPIEndPoints() {
  
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl("https://reqres.in/")
                  .addConverterFactory(GsonConverterFactory.create())
                  .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                  .build();
  
          APIEndPoints apiEndPoints = retrofit.create(APIEndPoints.class);
  
          return apiEndPoints;
      }
  }
  </code></pre>
  
  <p><strong>ApplicationComponent.java</strong></p>
  
  <pre><code>    @Singleton
  @Component(modules = {ApplicationModule.class})
  public interface ApplicationComponent {
  
      void inject(BaseActivity baseActivity);
  }
  </code></pre>
  
  <p><strong>BaseActivity.java</strong></p>
  
  <pre><code>    public class BaseActivity extends AppCompatActivity {
  
      // Variables
      public ProgressDialog mProgressDialog;
  
      @Inject
      HomePresenter homePresenter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // For Dagger2 i.e Creating instance of all provide methods defined in ApplicationModule
          ((Dagger2DemoApplication) getApplication()).getmApplicationComponent().inject(this);
  
          setupProgressBar();
      }
  
      private void setupProgressBar() {
          mProgressDialog = new ProgressDialog(this);
          mProgressDialog.setTitle(getString(R.string.str_progress_dialog_title));
          mProgressDialog.setMessage(getString(R.string.str_progress_dialog_desc));
          mProgressDialog.setCancelable(false);
      }
  }
  </code></pre>
  
  <p><strong>BaseView.java</strong></p>
  
  <pre><code>    public interface BaseView extends View {
  
      void handleResponse(Object obj);
  
      void showMessage(String msg);
  }
  </code></pre>
  
  <p><strong>View.java</strong></p>
  
  <pre><code>    public interface View {
  
  }
  </code></pre>
  
  <p><strong>BasePresenter.java</strong></p>
  
  <pre><code>public interface BasePresenter {
  
      void attachView(View view);
  
      void callAPI();
  }
  </code></pre>
  
  <p><strong>HomePresenter.java</strong></p>
  
  <pre><code>    public class HomePresenter implements BasePresenter {
  
      private BaseView mBaseView;
  
      @Inject
      APIEndPoints mApiEndPoints;
  
      /*@Inject
      public HomePresenter(APIEndPoints apiEndPoints) {
          this.mApiEndPoints = apiEndPoints;
      }*/
  
      @Override
      public void attachView(View view) {
          mBaseView = (BaseView) view;
      }
  
      @Override
      public void callAPI() {
  
          // Actually calling API here with observable object - Start
          Observable&lt;Users&gt; usersObservable = mApiEndPoints.getUsers();
  
          usersObservable
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(this::onSuccess, this::onError);
          // Actually calling API here with observable object - End
      }
  
      private void onSuccess(Users users) {
          mBaseView.handleResponse(users);
      }
  
      private void onError(Throwable throwable) {
          mBaseView.showMessage(throwable.toString());
      }
  }
  </code></pre>
  
  <p><strong>HomeActivity.java</strong></p>
  
  <pre><code>    public class HomeActivity extends BaseActivity implements BaseView {
  
      // Widgets
      @BindView(R.id.rv_users)
      RecyclerView rv_users;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // For ButterKnife
          ButterKnife.bind(this);
  
          // Initializing Presenter
          homePresenter.attachView(this);
      }
  
      public void getDataFromServer(View view) {
          mProgressDialog.show();
          homePresenter.callAPI();
      }
  
      // BaseView Methods
      @Override
      public void handleResponse(Object obj) {
          Users users;
          if (obj instanceof Users) {
              users = (Users) obj;
              if (users != null) {
                  mProgressDialog.dismiss();
                  rv_users.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
                  rv_users.setAdapter(new RVAdapter(users.getData()));
              }
          }
      }
  
      @Override
      public void showMessage(String msg) {
          if (msg != null) {
              mProgressDialog.dismiss();
              Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
          }
      }
  }
  </code></pre>
  
  <p>As you can see I commented Constructor Injection in HomePresenter. I'm having Field Injection there instead. But I'm not able to build the project as I'm getting error like mentioned above.</p>
  
  <p>Any help will be appreciated. Let me know if any other things related to code is required.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT:</strong>
  PS: I know the answer but I just can't understand why Field Injection i.e  <strong>@Inject
      APIEndPoints mApiEndPoints;</strong> is not working in HomePresenter. Please someone explain me.</p>