Search code examples
javaandroidandroid-studioandroid-activitydagger-hilt

Android || Inject dependencies in classes not supported by Hilt like Helper class


I am using the Hilt in my application, I am successfully injecting the dependency in the Activity class and getting the desired result, But not able to injecting the dependency in other classes like the helper class.

Please check the below code which I have tried like below the MainActivity class

package com.inn.hiltdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.inn.hiltdemo.hilt.MyFirstHelperClass;

import javax.inject.Inject;
import javax.inject.Named;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {


    @Inject
    @Named("MyHelperClass")
    MyFirstHelperClass myFirstHelperClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ////IT IS RUNNING SUCCESSFULLY
        System.out.println("HERE IS THE HELPER CLASS DATA==>>>> "+ myFirstHelperClass.getStringData());


        MySecondHelperClass mySecondHelperClass = new MySecondHelperClass();
        mySecondHelperClass.getSecondaryDataFromHelper(this);

    }
}

My MyModule class

package com.inn.hiltdemo.hilt

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Named
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class MyModule {

    @Singleton
    @Provides
    @Named("MyHelperClass")
    fun provideMyHelperClass(): MyFirstHelperClass {
        return MyFirstHelperClass()
    }
}

Please check the MyFirstHelperClass class

 package com.inn.hiltdemo.hilt;

public class MyFirstHelperClass {

    public String getStringData() {
        return "My Data";
    }
}

This is my MySecondHelperClass.java in which I am trying to get the Hilt dependency injection in it, please check the below code.

 package com.inn.hiltdemo;
    
    import android.content.Context;
    
    import com.inn.hiltdemo.hilt.MyFirstHelperClass;
    
    import javax.inject.Singleton;
    
    import dagger.hilt.EntryPoint;
    import dagger.hilt.InstallIn;
    import dagger.hilt.android.EntryPointAccessors;
    import dagger.hilt.components.SingletonComponent;
    
    @Singleton
    public class MySecondHelperClass {
    
    
        @EntryPoint
        @InstallIn(SingletonComponent.class)
        interface DeviceDiagnosticHelperEntryPoint {
            public MyFirstHelperClass provideMyFirstHelperClass();
        }
    
        public void getSecondaryDataFromHelper(Context mContext) {
    
            Context appContext = mContext.getApplicationContext();
            DeviceDiagnosticHelperEntryPoint hiltEntryPoint =
                    EntryPointAccessors.fromApplication(appContext,
                            DeviceDiagnosticHelperEntryPoint.class);
            MyFirstHelperClass myFirstHelperClass =
                    hiltEntryPoint.provideMyFirstHelperClass();
    
            System.out.println("HERE WE CAN USE==>>>> " + myFirstHelperClass.getStringData());
    
    
        }
    
    
    }

From the MySecondHelperClass.java , I want the dependency injection in my above class But getting the following exception like below

[Dagger/MissingBinding] com.inn.hiltdemo.hilt.MyFirstHelperClass cannot be provided without an @Inject constructor or an @Provides-annotated method. com.inn.hiltdemo.hilt.MyFirstHelperClass is requested at com.inn.hiltdemo.MySecondHelperClass.DeviceDiagnosticHelperEntryPoint.provideMyFirstHelperClass()


Solution

  • You missed the @Named anntotion in your second Helper:

    public class MySecondHelperClass {
    
       @EntryPoint
       @InstallIn(SingletonComponent.class)
       interface DeviceDiagnosticHelperEntryPoint {
           @Named("MyHelperClass")
           MyFirstHelperClass provideMyFirstHelperClass();
       }
    
       public void getSecondaryDataFromHelper(Context mContext) {
    
           Context appContext = mContext.getApplicationContext();
           DeviceDiagnosticHelperEntryPoint hiltEntryPoint =
                   EntryPointAccessors.fromApplication(appContext,
                           DeviceDiagnosticHelperEntryPoint.class);
           MyFirstHelperClass myFirstHelperClass =
                   hiltEntryPoint.provideMyFirstHelperClass();
    
           System.out.println("HERE WE CAN USE==>>>> " + 
           myFirstHelperClass.getStringData());
       }
    }
    

    Second option is to annotate the MyFirstHelperClass's constructor with inject and remove the Module class completely (if MyFirstHelperClass is yours)