Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Dagger of andriod

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use andriod's Dagger, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use andriod's Dagger. Let's take a look at it.

When we first started learning software engineering, we often came across things like this:

The software should conform to SOLID principles.

But what does this actually mean? Let's take a look at the important meaning that each letter in SOLID represents in the architecture, such as:

S-single responsibility principle

O-opening and closing principle

L-Liskov substitution principle

I-interface separation principle

D-dependency inversion principle, which is also the core concept of dependency injection (dependency injection).

Simply put, we need to provide a class that has all the objects it needs in order to implement its functionality.

Overview

Dependency injection sounds like a term to describe something very complex, but it's actually very simple, as you can see by looking at the following example:

Class NoDependencyInjection {private Dependency d; public NoDependencyInjection () {d = new Dependency ();}} class DependencyInjection {private Dependency d; public DependencyInjection (Dependency d) {this.d = d;}}

As we have seen, in the case of *, we create a dependency object in the constructor, but in the second case, it is passed to the constructor as an argument, which is what we call dependency injection (dependency injection). This is done so that the class we write does not rely on the implementation of a particular dependency, but can use it directly.

The target of parameter passing is the constructor, which we call constructor dependency injection, or a method, which is called method dependency injection:

Class Example {private ConstructorDependency cd; private MethodDependency md; Example (ConstructorDependency cd) {this.cd = cd; / / ConstructorDependency Injection} public setMethodDependency (MethodDependency md) {this.md = md; / / MethodDependency Injection}}

If you want a general and in-depth understanding of dependency injection, take a look at the wonderful presentation by Dan Lew, which in fact inspired this overview.

On the Android platform, we have different options when we need a framework to deal with the special problem of dependency injection, the most famous of which is Dagger 2. It was first developed by some great developers at Square, and then slowly developed by Google itself. Dagger 1 was first developed, and then Big G took over the project and released the second version, with a lot of changes, such as annotation-based, to complete its task at compile time.

Import Fram

Installing Dagger is not difficult, but you need to import the android-apt plug-in by adding its dependencies to the build.gradle file in the root directory of the project:

Buildscript {... Dependencies {... Classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'}}

Then, we need to apply the android-apt plug-in to the project build.gradle file, on the next line of the sentence Android application at the top of the file:

Apply plugin: 'com.neenbedankt.android-apt'

At this point, we just need to add dependencies, and then we can use the library and its annotations (annotation):

Dependencies {... Compile 'com.google.dagger:dagger:2.6' apt' com.google.dagger:dagger-compiler:2.6' provided 'javax.annotation:jsr250-api:1.0'}

A dependency needs to be added because the @ Generated annotation is not yet available in Android, but it is a native Java annotation.

Dagger module

To inject dependencies, you first need to tell the framework what we can provide (such as context) and how specific objects should be created. To complete the injection, we annotate a particular class with the @ Module annotation (so that Dagger can recognize it), look for ways to annotate @ Provide, generate diagrams, and return the object we requested.

Looking at the example below, we have created a module that will return our ConnectivityManager, so we will pass the Context object to the module's constructor.

Module public class ApplicationModule {private final Context context; public ApplicationModule (Context context) {this.context = context;} @ Provides @ Singleton public Context providesContext () {return context;} @ Provides @ Singleton public ConnectivityManager providesConnectivityManager (Context context) {return (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);}}

An interesting point in Dagger is that simply annotating a method to provide a single instance (Singleton) can handle all the problems inherited from Java.

module

When we have a module, we need to tell Dagger where we want to inject the dependency: we do the dependency injection in a component (Component), which is a special annotation interface we created. We create different methods in this interface, and the parameters of the interface are the classes we want to inject dependencies into.

Let's give an example and tell Dagger that we want the MainActivity class to accept ConnectivityManager (or other dependent objects in the diagram). We just need to do something like the following:

@ Singleton @ Component (modules = {ApplicationModule.class}) public interface ApplicationComponent {void inject (MainActivity activity);}

As we can see, the @ Component annotation has several parameters, one of which is an array of supported modules, representing the dependencies it can provide. Here it can be either Context or ConnectivityManager because they are declared in the ApplicationModule class.

Usage

At this point, what we need to do is to create the component as soon as possible (for example, during the onCreate phase of the application) and return it, so that the class can use it to inject dependencies:

In order for the framework to generate DaggerApplicationComponent automatically, we need to build the project so that Dagger can scan our code and generate the parts we need.

In MainActivity, the two things we need to do is annotate the property we want to inject with the @ Inject commentator, and call the method we declared in the ApplicationComponent interface (note that the latter part will change depending on the type of injection we use, but we don't care about it here for simplicity), and then the dependencies are injected, and we are free to use them:

Public class MainActivity extends AppCompatActivity {@ Inject ConnectivityManager manager; @ Override protected void onCreate (Bundle savedInstanceState) {... ((App) getApplication ()). GetComponent (). Inject (this);}} this is the end of the article on "how to use the Dagger of andriod". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use andriod's Dagger". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report