Giter Club home page Giter Club logo

dagger2-example's Introduction

dagger2-example with Gradle build

Dagger2 site

See branch for ewerk plugin version

Since 2.0 release:- Dagger generates components prefixed with just "Dagger" instead of "_"

Truth Assert

Currently uses Truth.assertThat(...)


  • @Component
    • @Module
      • @Provides

The TerrestrialPlanetsModule, for example, provides a singleton named "Mercury" etc.

@Module
public class TerrestrialPlanetsModule {

    @Provides
    @Singleton
    @Named("Mercury")
    Planet first() {
        return new Mercury();
    }

    @Provides
    @Singleton
    @Named("Venus")
    Planet second() {
        return new Venus();
    }

    @Provides
    @Singleton
    @Named("Earth")
    Planet third() {
        return new Earth();
    }

    @Provides
    @Singleton
    @Named("Mars")
    Planet forth() {
        return new Mars();
    }

}
  • Outer Planets module

    • Jupiter
    • Saturn
    • Uranus
    • Neptune
  • Dwarf Planets module

    • Ceres
    • Pluto
    • Haumea
    • MakeMake
    • Eris

Gradle build

  • The dagger compiler (com.google.dagger:dagger-compiler) annotation processor is only added to the java compiler task classpath using the configuration "compileDagger".

  • Application only runs with (com.google.dagger:dagger, javax.inject and com.google.guava as used in the application)

  • The sourceSet for generated code is under 'src/dagger/java'

./gradlew run
plugins {
  id 'java'
  id 'application'
  id "com.github.johnrengelman.shadow" version "1.2.3"
}

project.ext.sourceCompatibility = JavaVersion.VERSION_1_8
project.ext.targetCompatibility = JavaVersion.VERSION_1_8

repositories {
  jcenter()
}

sourceSets {
  dagger {
    java {
      srcDirs = ['src/dagger/java']
    }
  }
}

configurations {
  compileDagger
}

compileJava {
  description = "dagger annotation processor is loaded automatically from classpath"
  sourceSets.dagger.java.srcDirs*.mkdirs()
  classpath += configurations.compileDagger
  options.compilerArgs += [
      '-s', sourceSets.dagger.java.srcDirs.iterator().next()
  ]
}

mainClassName = "griffio.MainApplication"

clean {
  description = "delete files in generated source directory tree"
  delete fileTree(dir: sourceSets.dagger.java.srcDirs.iterator().next())
}

dependencies {

  compile(
      "com.google.dagger:dagger:2.8",
      "com.google.guava:guava:20.0")

  compileDagger(
      "com.google.dagger:dagger-compiler:2.8")

  testCompile(
      "junit:junit:4.12",
      "com.google.truth:truth:0.30"
  )

}

task wrapper(type: Wrapper) {
  gradleVersion = '2.3.1'
}

dagger2-example's People

Contributors

griffio avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.