Introduction and configuration of Dagger2

foreword

        

         Introduction and configuration of Dagger2

        The use of Dagger2 basics

        Dagger2 Advanced - Compiling and Generating Source Code Interpretation

        Dagger2 Advanced - Scope Control (Scope and Singleton)

        Dagger2 Advanced-Scope Source Code Exploration

        Project source code portal


        Dagger2, the Chinese translation of "dagger", is a deadly weapon developed by Android. As an indispensable weapon in the development of large projects, I hope everyone can patiently read the articles and practice them.

Introduction to Dagger2:

        Dagger2 is used for dependency injection . Usually we understand it as a replacement for us to generate a bunch of objects for us to use, for example: we want objects such as ImageManager, NetWorkManager, DbManaer, etc. We can get the objects directly through simple annotations after configuring Dagger2, and do not need to consider the objects such as ImageManger The parameters that the construction method needs to rely on, the framework directly helps us configure it, we only need to pay attention to the outermost object. A relatively simple understanding is that Dagger2 was born to generate objects to reduce our new work, and was created to replace the factory class. Of course, the principle of Dagger2 also uses the factory class, which we will talk about later.

AndroidStudio environment configuration

    There are two ways to configure Dagger2 in As: apt and annotationProcessor respectively. It is strongly recommended to use annotationProcessor, because apt has been suspended by Google after as2.2. AnnotationProcessor supports jack and javac compilation, while apt only supports javac. When using Apt will have some invisible problems such as conflicts with other annotation frameworks.

APT configuration

    Step 1: Add the apt plugin to as in the project's build.gradle file

    

dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

   Step 2: Talk about the introduction of the apt plugin into the build.gradle of the Module project

    

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

dependencies {
compile 'com.google.dagger:dagger:2.7'
    apt 'com.google.dagger:dagger-compiler:2.7'
    compile 'org.glassfish:javax.annotation:10.0-b28'
}


Finally, you can use dagger2 after compiling.

    Explain here that classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8', apply plugin: 'com.neenbedankt.android-apt', compile 'org.glassfish:javax.annotation:10.0-b28' is to introduce The Apt plugin is really different from other annotation frameworks is the compile 'com.google.dagger:dagger:2.7' dagger package.

AnnotationProcessor

    The annotationProcessor is very simple to use, just need to rely on the code in the build.gradle of each module

compile 'com.google.dagger:dagger:2.7'
annotationProcessor 'com.google.dagger:dagger-compiler:2.7'

Here 'com.google.dagger:dagger:2.7' is also a dagger package, which contains annotations and other tool classes specific to dagger2. annotationProcessor is a tool for Module to generate code based on annotations.

common problem

    1: When there are multiple modules, many children say that only the main module is valid, and the sub-modules are not valid, because each module should add

  annotationProcessor 'com.google.dagger:dagger-compiler:2.7'

    Adding modules that are not added cannot automatically generate code.


finally

    Finally, we hope that everyone can have their own gains after reading this article. Later, we will gradually update the magic operation of dagger2. I hope you can continue to pay attention. There are too few problems at the end of the article. Problems encountered in the message. I will update it to the article after I solve it, I wish everyone happy.

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529991&siteId=291194637