Dagger Dependency Injection

repository·master·Indexed 12 days ago

https://github.com/google/dagger

A fast, compile-time dependency injection framework for Java and Android that generates plain Java source code to avoid reflection and runtime bytecode generation. Includes Hilt for simplified Android integration and support for gRPC servers via dagger-grpc-server. Provides installation guides for Bazel, Maven, and Gradle.

Tokens
2.1K
Snippets
5
Records
7
Agent score
47%

What's inside Dagger

  1. Use Hilt for Android dependency injection

    master
    Hilt is a library that provides a standard way to incorporate Dagger dependency injection into an Android application. It simplifies the setup of Dagger in Android by providing predefined components and managing their lifecycles according to Android framework classes (like Activities, Fragments, and ViewModels).
  2. Install Dagger Android using Bazel

    master

    To add Dagger Android support in Bazel:

    1. Load Android artifacts: Use DAGGER_ANDROID_ARTIFACTS and DAGGER_ANDROID_REPOSITORIES from @dagger//:workspace_defs.bzl in your maven_install call.
    2. Setup Android rules: Call dagger_android_rules() in your top-level BUILD file.

    This provides the following targets:

    • :dagger-android
    • :dagger-android-support
    # Top-level WORKSPACE file
    
    load(
        "@dagger//:workspace_defs.bzl",
        "DAGGER_ANDROID_ARTIFACTS",
        "DAGGER_ANDROID_REPOSITORIES"
    )
    
    maven_install(
        artifacts = DAGGER_ANDROID_ARTIFACTS + [...],
        repositories = DAGGER_ANDROID_REPOSITORIES + [...],
    )
    
    # Top-level BUILD file
    
    load("@dagger//:workspace_defs.bzl", "dagger_android_rules")
    
    dagger_android_rules()
  3. Install Hilt Android using Bazel

    master

    To add Hilt Android support in Bazel:

    1. Load Hilt artifacts: Use HILT_ANDROID_ARTIFACTS and HILT_ANDROID_REPOSITORIES from @dagger//:workspace_defs.bzl in your maven_install call.
    2. Setup Hilt rules: Call hilt_android_rules() in your top-level BUILD file.

    This provides the following targets:

    • :hilt-android
    • :hilt-android-testing
    # Top-level WORKSPACE file
    
    load(
        "@dagger//:workspace_defs.bzl",
        "HILT_ANDROID_ARTIFACTS",
        "HILT_ANDROID_REPOSITORIES"
    )
    
    maven_install(
        artifacts = HILT_ANDROID_ARTIFACTS + [...],
        repositories = HILT_ANDROID_REPOSITORIES + [...],
    )
    
    # Top-level BUILD file
    
    load("@dagger//:workspace_defs.bzl", "hilt_android_rules")
    
    hilt_android_rules()
  4. Install Dagger via Gradle

    master

    Add the following to your dependencies block:

    For standard Dagger:

    • implementation 'com.google.dagger:dagger:2.x'
    • annotationProcessor 'com.google.dagger:dagger-compiler:2.x'

    For Dagger Android:

    • implementation 'com.google.dagger:dagger-android:2.x'
    • implementation 'com.google.dagger:dagger-android-support:2.x'
    • annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'

    Note:

    • Use implementation instead of api for better compilation performance.
    • For Kotlin projects, use kapt instead of annotationProcessor.
    • If using Android Databinding, you may need to increase javac error limits to prevent compilation halts.
    // Standard Dagger
    dependencies {
      implementation 'com.google.dagger:dagger:2.x'
      annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
    }
    
    // Dagger Android
    dependencies {
      implementation 'com.google.dagger:dagger-android:2.x'
      implementation 'com.google.dagger:dagger-android-support:2.x'
      annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
    }
    
    // Increase error limit for Databinding compatibility
    gradle.projectsEvaluated {
      tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "500"
      }
    }
  5. Install Dagger via Maven

    master

    For Maven projects, include dagger as a runtime dependency and dagger-compiler as an annotationProcessorPath in the maven-compiler-plugin to enable code generation.

    If using maven-compiler-plugin version < 3.5, add dagger-compiler with provided scope.

    To use the dagger-producers extension, add dagger-producers as a dependency.

    <dependencies>
      <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>2.x</version>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.6.1</version>
          <configuration>
            <annotationProcessorPaths>
              <path>
                <groupId>com.google.dagger</groupId>
                <artifactId>dagger-compiler</artifactId>
                <version>2.x</version>
              </path>
            </annotationProcessorPaths>
          </configuration>
        </plugin>
      </plugins>
    </build>
  6. Install Dagger using Bazel

    master

    To use Dagger in a Bazel project, follow these steps:

    1. Import the Dagger repository: Add an http_archive rule to your top-level WORKSPACE file pointing to a tagged release.
    2. Load artifacts: Use DAGGER_ARTIFACTS and DAGGER_REPOSITORIES from @dagger//:workspace_defs.bzl within your maven_install configuration.
    3. Setup build rules: Call dagger_rules() in your top-level BUILD file to expose the necessary Dagger targets.
    # Top-level WORKSPACE file
    
    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    
    DAGGER_TAG = "2.60.1"
    DAGGER_SHA = "7ecce51ca45c551182df10fe4ea48fcdaabacaa282832a4244c04934e87fbc47"
    http_archive(
        name = "dagger",
        strip_prefix = "dagger-dagger-%s" % DAGGER_TAG,
        sha256 = DAGGER_SHA,
        urls = ["https://github.com/google/dagger/archive/dagger-%s.zip" % DAGGER_TAG],
    )
    
    load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES")
    
    maven_install(
        artifacts = DAGGER_ARTIFACTS + [...],
        repositories = DAGGER_REPOSITORIES + [...],
    )
    
    # Top-level BUILD file
    
    load("@dagger//:workspace_defs.bzl", "dagger_rules")
    
    dagger_rules()