Skip to content

Tutorial

Alexandru Dochioiu edited this page Feb 16, 2019 · 1 revision

Step 1: Create a normal dagger2 module

@Module
public class MyModule {
    @Provides
    Object myObject() {
        return new Object();
    }
}

Note: As you can see, no scope has yet been given to the object. This will be addressed in the following steps.

Step 2: Creating the Application (Sharp) Component

@SharpComponent(
        modules = MyModule.class // array of multiple modules can be provided as {A.class, B.class, ...}
)
public class MyApplication extends Application {
}

Step 2.1: Build the project

After building the project, the following two files will have been automatically generated by DaggerSharpener:

SharpMyApplicationComponent.java -- (Name is formed as: Sharp + Class Name + Component)

@Component
@SharpMyApplicationScope
public interface SharpMyApplicationComponent {
  MyApplication inject(MyApplication thisClass);
}

SharpMyApplicationScope.java -- (Name is formed as: Sharp + Class Name + Scope)

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface SharpMyApplicationScope {}

Step 2.2 : Go back to the module and add the generated scope for the provided object

@Module
public class MyModule {
    @Provides
    @SharpMyApplicationScope
    Object myObject() {
        return new Object();
    }
}

Step 2.3 : Inject the dependencies in MyApplication

@SharpComponent(
        modules = MyModule.class
)
public class MyApplication extends Application {

    private SharpMyApplicationComponent sharpComponent;
    
    @Inject
    Object injectedObject;

    @Override
    public void onCreate() {
        super.onCreate();
        
        // Name is: Dagger + Sharp + Class Name + Component
        sharpComponent = DaggerSharpMyApplicationComponent.builder().build();
        sharpComponent.inject(this);

        Log.d("MyApplication", "onCreate: " + injectedObject.toString());
    }

    public SharpMyApplicationComponent getSharpComponent() {
        return sharpComponent;
    }
}

Note: By running the code and checking logcat, you will see that the injectedObject is non-null. This indicates our object got successfully injected.

Step 3: Creating the Activity (Sharp) Component

@SharpComponent(
        dependencies = SharpMyApplicationComponent.class
)
public class MainActivity extends AppCompatActivity {

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

Note: The new Sharp Activity Component depends on SharpMyApplicationComponent

Step 3.1: Build the project

After building, two new files are generated:

SharpMainActivityComponent.java

@Component
@SharpMainActivityScope
public interface SharpMainActivityComponent {
  MainActivity inject(MainActivity thisClass);
}

SharpMainActivityScope.java

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface SharpMainActivityScope {}

Step 3.2: Expose dependencies from MyApplication to its dependants

@SharpComponent(
        modules = MyModule.class,
        provides = Object.class // Array of dependencies can be provided as {A.class, B.class, etc.}
)
public class MyApplication extends Application {
  ...
}

Step 3.3 : Inject the dependencies in MainActivity

@SharpComponent(
        sharpDependencies = MyApplication.class // The class(es) annotated with @SharpComponent
)
public class MainActivity extends AppCompatActivity {

    @Inject
    Object myObject;

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

        DaggerSharpMainActivityComponent.builder()
                .sharpMyApplicationComponent(((MyApplication) getApplication()).getSharpComponent())
                .build()
                .inject(this);

        Log.d("MainActivity", "onCreate: " + myObject.toString());
    }
}

Note: By running the code and checking logcat, you will see that injectedObject from MyApplication and myObject from MainActivity are indeed the same instance (as long as you did the step 2.2). If you did not do step 2.2, the instance will be different for the two objects.

Step 4: Enjoy

Using DaggerSharpener will still allow you to use @Inject on constructors. Also, the generated scopes can be safely used on the classes which inject constructors.

For some docs, keep on reading below!