1# Dagger 2 3[![Maven Central][mavenbadge-svg]][mavencentral] 4 5A fast dependency injector for Java and Android. 6 7Dagger is a compile-time framework for dependency injection. It uses no 8reflection or runtime bytecode generation, does all its analysis at 9compile-time, and generates plain Java source code. 10 11Dagger is actively maintained by the same team that works on [Guava]. Snapshot 12releases are auto-deployed to Sonatype's central Maven repository on every clean 13build with the version `HEAD-SNAPSHOT`. The current version builds upon previous 14work done at [Square][square]. 15 16## Documentation 17 18You can [find the dagger documentation here][website] which has extended usage 19instructions and other useful information. More detailed information can be 20found in the [API documentation][latestapi]. 21 22You can also learn more from [the original proposal][proposal], 23[this talk by Greg Kick][gaktalk], and on the dagger-discuss@googlegroups.com 24mailing list. 25 26## Installation 27 28### Bazel 29 30First, import the Dagger repository into your `WORKSPACE` file using 31[`http_archive`][bazel-external-deps]. 32 33Note: The `http_archive` must point to a tagged release of Dagger, not just any 34commit. The version of the Dagger artifacts will match the version of the tagged 35release. 36 37```python 38# Top-level WORKSPACE file 39 40load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 41 42DAGGER_TAG = "2.28.1" 43DAGGER_SHA = "9e69ab2f9a47e0f74e71fe49098bea908c528aa02fa0c5995334447b310d0cdd" 44http_archive( 45 name = "dagger", 46 strip_prefix = "dagger-dagger-%s" % DAGGER_TAG, 47 sha256 = DAGGER_SHA, 48 urls = ["https://github.com/google/dagger/archive/dagger-%s.zip" % DAGGER_TAG], 49) 50``` 51 52Next you will need to setup targets that export the proper dependencies 53and plugins. Follow the sections below to setup the dependencies you need. 54 55#### Dagger Setup 56 57First, load the Dagger artifacts and repositories, and add them to your list of 58[`maven_install`] artifacts. 59 60```python 61# Top-level WORKSPACE file 62 63load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES") 64 65maven_install( 66 artifacts = DAGGER_ARTIFACTS + [...], 67 repositories = DAGGER_REPOSITORIES + [...], 68) 69``` 70 71Next, load and call [`dagger_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl) 72in your top-level `BUILD` file: 73 74```python 75# Top-level BUILD file 76 77load("@dagger//:workspace_defs.bzl", "dagger_rules") 78 79dagger_rules() 80``` 81 82This will add the following Dagger build targets: 83(Note that these targets already export all of the dependencies and processors 84they need). 85 86```python 87deps = [ 88 ":dagger", # For Dagger 89 ":dagger-spi", # For Dagger SPI 90 ":dagger-producers", # For Dagger Producers 91] 92``` 93 94#### Dagger Android Setup 95 96First, load the Dagger Android artifacts and repositories, and add them to your 97list of [`maven_install`] artifacts. 98 99```python 100# Top-level WORKSPACE file 101 102load( 103 "@dagger//:workspace_defs.bzl", 104 "DAGGER_ANDROID_ARTIFACTS", 105 "DAGGER_ANDROID_REPOSITORIES" 106) 107 108maven_install( 109 artifacts = DAGGER_ANDROID_ARTIFACTS + [...], 110 repositories = DAGGER_ANDROID_REPOSITORIES + [...], 111) 112``` 113 114Next, load and call [`dagger_android_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl) 115in your top-level `BUILD` file: 116 117```python 118# Top-level BUILD file 119 120load("@dagger//:workspace_defs.bzl", "dagger_android_rules") 121 122dagger_android_rules() 123``` 124 125This will add the following Dagger Android build targets: 126(Note that these targets already export all of the dependencies and processors 127they need). 128 129```python 130deps = [ 131 ":dagger-android", # For Dagger Android 132 ":dagger-android-support", # For Dagger Android (Support) 133] 134``` 135 136#### Hilt Android Setup 137 138First, load the Hilt Android artifacts and repositories, and add them to your 139list of [`maven_install`] artifacts. 140 141```python 142# Top-level WORKSPACE file 143 144load( 145 "@dagger//:workspace_defs.bzl", 146 "HILT_ANDROID_ARTIFACTS", 147 "HILT_ANDROID_REPOSITORIES" 148) 149 150maven_install( 151 artifacts = HILT_ANDROID_ARTIFACTS + [...], 152 repositories = HILT_ANDROID_REPOSITORIES + [...], 153) 154``` 155 156Next, load and call [`hilt_android_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl) 157in your top-level `BUILD` file: 158 159```python 160# Top-level BUILD file 161 162load("@dagger//:workspace_defs.bzl", "hilt_android_rules") 163 164hilt_android_rules() 165``` 166 167This will add the following Hilt Android build targets: 168(Note that these targets already export all of the dependencies and processors 169they need). 170 171```python 172deps = [ 173 ":hilt-android", # For Hilt Android 174 ":hilt-android-testing", # For Hilt Android Testing 175] 176``` 177 178### Other build systems 179 180You will need to include the `dagger-2.x.jar` in your application's runtime. 181In order to activate code generation and generate implementations to manage 182your graph you will need to include `dagger-compiler-2.x.jar` in your build 183at compile time. 184 185#### Maven 186 187In a Maven project, include the `dagger` artifact in the dependencies section 188of your `pom.xml` and the `dagger-compiler` artifact as an 189`annotationProcessorPaths` value of the `maven-compiler-plugin`: 190 191```xml 192<dependencies> 193 <dependency> 194 <groupId>com.google.dagger</groupId> 195 <artifactId>dagger</artifactId> 196 <version>2.x</version> 197 </dependency> 198</dependencies> 199<build> 200 <plugins> 201 <plugin> 202 <groupId>org.apache.maven.plugins</groupId> 203 <artifactId>maven-compiler-plugin</artifactId> 204 <version>3.6.1</version> 205 <configuration> 206 <annotationProcessorPaths> 207 <path> 208 <groupId>com.google.dagger</groupId> 209 <artifactId>dagger-compiler</artifactId> 210 <version>2.x</version> 211 </path> 212 </annotationProcessorPaths> 213 </configuration> 214 </plugin> 215 </plugins> 216</build> 217``` 218 219If you are using a version of the `maven-compiler-plugin` lower than `3.5`, add 220the `dagger-compiler` artifact with the `provided` scope: 221 222```xml 223<dependencies> 224 <dependency> 225 <groupId>com.google.dagger</groupId> 226 <artifactId>dagger</artifactId> 227 <version>2.x</version> 228 </dependency> 229 <dependency> 230 <groupId>com.google.dagger</groupId> 231 <artifactId>dagger-compiler</artifactId> 232 <version>2.x</version> 233 <scope>provided</scope> 234 </dependency> 235</dependencies> 236``` 237 238If you use the beta `dagger-producers` extension (which supplies 239parallelizable execution graphs), then add this to your maven configuration: 240 241```xml 242<dependencies> 243 <dependency> 244 <groupId>com.google.dagger</groupId> 245 <artifactId>dagger-producers</artifactId> 246 <version>2.x</version> 247 </dependency> 248</dependencies> 249``` 250 251#### Gradle 252```groovy 253// Add Dagger dependencies 254dependencies { 255 implementation 'com.google.dagger:dagger:2.x' 256 annotationProcessor 'com.google.dagger:dagger-compiler:2.x' 257} 258``` 259 260If you're using classes in `dagger.android` you'll also want to include: 261 262```groovy 263implementation 'com.google.dagger:dagger-android:2.x' 264implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries 265annotationProcessor 'com.google.dagger:dagger-android-processor:2.x' 266``` 267 268Notes: 269 270- We use `implementation` instead of `api` for better compilation performance. 271 - See the [Gradle documentation][gradle-api-implementation] for more 272 information on how to select appropriately, and the [Android Gradle 273 plugin documentation][gradle-api-implementation-android] for Android 274 projects. 275- For Kotlin projects, use [`kapt`] in place of `annotationProcessor`. 276 277If you're using the [Android Databinding library][databinding], you may want to 278increase the number of errors that `javac` will print. When Dagger prints an 279error, databinding compilation will halt and sometimes print more than 100 280errors, which is the default amount for `javac`. For more information, see 281[Issue 306](https://github.com/google/dagger/issues/306). 282 283```groovy 284gradle.projectsEvaluated { 285 tasks.withType(JavaCompile) { 286 options.compilerArgs << "-Xmaxerrs" << "500" // or whatever number you want 287 } 288} 289``` 290 291### Resources 292 293* [Documentation][website] 294* [Javadocs][latestapi] 295* [GitHub Issues] 296 297 298If you do not use maven, gradle, ivy, or other build systems that consume 299maven-style binary artifacts, they can be downloaded directly via the 300[Maven Central Repository][mavencentral]. 301 302Developer snapshots are available from Sonatype's 303[snapshot repository][dagger-snap], and are built on a clean build of 304the GitHub project's master branch. 305 306## Building Dagger 307 308See [the CONTRIBUTING.md docs][Building Dagger]. 309 310## License 311 312 Copyright 2012 The Dagger Authors 313 314 Licensed under the Apache License, Version 2.0 (the "License"); 315 you may not use this file except in compliance with the License. 316 You may obtain a copy of the License at 317 318 http://www.apache.org/licenses/LICENSE-2.0 319 320 Unless required by applicable law or agreed to in writing, software 321 distributed under the License is distributed on an "AS IS" BASIS, 322 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 323 See the License for the specific language governing permissions and 324 limitations under the License. 325 326[`bazel`]: https://bazel.build 327[bazel-external-deps]: https://docs.bazel.build/versions/master/external.html#depending-on-other-bazel-projects 328[`maven_install`]: https://github.com/bazelbuild/rules_jvm_external#exporting-and-consuming-artifacts-from-external-repositories 329[Building Dagger]: CONTRIBUTING.md#building-dagger 330[dagger-snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/dagger/ 331[databinding]: https://developer.android.com/topic/libraries/data-binding/ 332[gaktalk]: https://www.youtube.com/watch?v=oK_XtfXPkqw 333[GitHub Issues]: https://github.com/google/dagger/issues 334[gradle-api-implementation]: https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation 335[gradle-api-implementation-android]: https://developer.android.com/studio/build/dependencies#dependency_configurations 336[Guava]: https://github.com/google/guava 337[`kapt`]: https://kotlinlang.org/docs/reference/kapt.html 338[latestapi]: https://dagger.dev/api/latest/ 339[mavenbadge-svg]: https://maven-badges.herokuapp.com/maven-central/com.google.dagger/dagger/badge.svg 340[mavencentral]: https://search.maven.org/artifact/com.google.dagger/dagger 341[project]: http://github.com/google/dagger/ 342[proposal]: https://github.com/square/dagger/issues/366 343[square]: http://github.com/square/dagger/ 344[website]: https://dagger.dev 345