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